fread问题() [英] Problem with fread()

查看:96
本文介绍了fread问题()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helas,


我得到的东西应该是一个小问题,但无论如何它现在不是我的b $ b。


一个小代码片段:

fread(& file_qn,x,1,fp_q);


相应的文字文件看起来像这样:

456 5 1.txt%&''

我已经用fseek跳过了''456'部分,请阅读' '5''与scanf,

file_qn现在应该得到''1.txt''部分,x是该字符串的长度,

即5。 br />
提前感谢您的帮助!


Alain。

Helas,

I got something that should be a minor problem, but anyhow it isn''t to me
right now.

A little code fragment:
fread(&file_qn, x, 1, fp_q);

The corresponding text file looks like this:
456 5 1.txt%&''
I''ve already skipped the ''456'' part with fseek, read the ''5'' with scanf,
file_qn should now get the ''1.txt'' part and x is the length of that string,
which is 5.
Thanks for any help in advance !

Alain.

推荐答案

On星期四,2004年1月29日05:35:36 +0100,Alain Lafon < PR *** @ gmx.at>在comp.lang.c写了


On Thu, 29 Jan 2004 05:35:36 +0100, "Alain Lafon" <pr***@gmx.at> wrote
in comp.lang.c:
Helas,

我得到的东西应该是一个小问题,但无论如何现在不是我。

一个小代码片段:
fread(& file_qn,x,1,fp_q);
456 5 1.txt%&''
我已经用fseek跳过了''456'部分,读了''5' 'with scanf,
file_qn现在应该得到''1.txt''部分,x是该字符串的长度,
是5.

感谢您的帮助提前!

Alain。
Helas,

I got something that should be a minor problem, but anyhow it isn''t to me
right now.

A little code fragment:
fread(&file_qn, x, 1, fp_q);

The corresponding text file looks like this:
456 5 1.txt%&''
I''ve already skipped the ''456'' part with fseek, read the ''5'' with scanf,
file_qn should now get the ''1.txt'' part and x is the length of that string,
which is 5.
Thanks for any help in advance !

Alain.




这是我的建议:再次发布并解释你的小问题

是。发布一个完整的可编辑代码片段,我们如何知道
应该知道file_qn或x的类型?如果有编译器

错误消息,请复制文本并粘贴到邮件正文中。


您还没有提供足够的信息来解决您的问题任何人都可以提供任何其他建议。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题

comp.lang.c http ://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp .lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Here''s my advice: Post again and explain what your "minor problem"
is. Post a complete compilable code snippet, how the heck are we
supposed to know the type of file_qn or x? If there are compiler
error messages, copy the text and paste into the body of your message.

You haven''t provided enough information on your problem for anyone to
offer any other advice.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


我是对不起。


这是我的进一步信息:

代码编译正常。问题是该程序只是关闭时

它应该执行fread()部分。

这里是代码片段:


void abhaengen(FILE * fp_q,int key)

{

int i;

const char * file_qn;

-其他变量 -

-snip-

fscanf(fp_q,"%i",& x); //获取字符串的大小

由fread读取()

fread(& file_qn,x,1,fp_q); //这正是程序只是

关闭而没有其他消息的地方。

-snip-

}

要读取的文件如下所示:

456 5 1.txt%&''

我很抱歉打扰了你的帖子不完整。但是早上5:52已经好了,我有点困惑* g *


再次感谢,

Alain。
I''m sorry.

Here''s my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here''s the code fragment:

void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
fread(&file_qn, x, 1, fp_q); //this is exactly where the program simply
closes with no further messages.
-snip-
}

The file that is to be read looks like this:
456 5 1.txt%&''
I''m sorry to have bothered you with an incomplete posting. But it''s already
5:52am and I''m getting a little confused *g*

Thanks again,
Alain.


Alain Lafon写道:
Alain Lafon wrote:
对不起。

这里这是我的进一步信息:
代码编译得很好。问题是程序只是在它应该执行fread()部分时关闭。
这里是代码片段:

void abhaengen(FILE * fp_q,int key)
{
int i;
const char * file_qn;


为什么是const?这个声明说file_qn是一个指向const char的指针,

意味着你无法改变file_qn指向的内容。因为这正是你想要做的,所以输掉了const:

char * file_qn;


然而,你仍然没有把file_qn指向任何有用的东西。一种方式

这样做是通过使用malloc分配一些内存并使file_qn

指向那个。

-some其他变量 - < brs> -snip-
fscanf(fp_q,"%i",& x); //获取由fread()读取的字符串的大小


现在你甚至知道你需要多少内存(检查fscanf的返回值

以确保你得到一个有效的整数):

file_qn = malloc(sizeof * file_qn * x); / *检查结果为NULL * /

fread(& file_qn,x,1,fp_q); //这正是程序
的地方那是完全错误的。 fread将读取每个大小字节大的nmemb项目

ptr指向的区域:


fread(file_qn,1,x,fp_q);


其中说,从fp_q读取大小为1的x项到

file_qn指向的区域。但是请注意,fscanf仍会在整数和输入流中的文件名之间留下任何空格,这意味着简单的

fread将获得前导空白和部分文件名。这样做你可能会更好:

fscanf(fp_q,"%* [\ n]"); / *扔掉任何空白* /

fread(& file_qn,x,1,fp_q); / *现在读取文件名* /

只需
关闭,没有其他消息。


可能是因为通过该小额通话访问您不拥有的内存的结果。

人们希望一个体面的操作系统会给你一些指示为什么

计划被杀了。


HTH,

-nrk。

-snip-
}

要读取的文件如下所示:
456 5 1.txt%&''

我是很抱歉打扰了你一个不完整的帖子。但它已经是凌晨5点52分了,我有点困惑了* g *

再次感谢,Alain。
I''m sorry.

Here''s my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here''s the code fragment:

void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
Why const? This declaration says file_qn is a pointer to const char, which
means you cannot change what file_qn points to. Since this is precisely
what you want to do, lose the const:
char *file_qn;

However, you still haven''t made file_qn point to anything useful. One way
of doing this is by using malloc to allocate some memory and make file_qn
point to that.
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
Now you even know how much memory you need (check the return value of fscanf
to make sure you got a valid integer):
file_qn = malloc(sizeof *file_qn * x); /* check the result for NULL */
fread(&file_qn, x, 1, fp_q); //this is exactly where the program That''s totally wrong. fread will read nmemb items each size bytes big into
the area pointed to by ptr:

fread(file_qn, 1, x, fp_q);

Which says, read x items of size 1 from fp_q into the area pointed to by
file_qn. However, note that the fscanf will still leave any spaces between
the integer and the filename in the input stream, which means that simple
fread will get leading blank(s) and part of the filename. You''re probably
better off doing this:

fscanf(fp_q, "%*[\n]"); /* throw away any blanks */
fread(&file_qn, x, 1, fp_q); /* now read the filename */
simply
closes with no further messages.
Probably a result of accessing memory you don''t own through that fread call.
One would hope a decent OS will give you some indication as to why the
program was killed.

HTH,
-nrk.
-snip-
}

The file that is to be read looks like this:
456 5 1.txt%&''
I''m sorry to have bothered you with an incomplete posting. But it''s
already 5:52am and I''m getting a little confused *g*

Thanks again,
Alain.




-

删除电子邮件的devnull



--
Remove devnull for email


这篇关于fread问题()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆