C文件I / O ...使用数组结构 [英] C File I/O... working with array structures

查看:89
本文介绍了C文件I / O ...使用数组结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为people的数组结构


它被正确初始化为100个数组结构元素。现在,我是从命令行读取一个txt文件的
,我让他们正确打开

。在这个.txt文件中,有一个名字和一个重量。


Josh 123.32

Jim 212.123

Steph 213.2


....等


现在,我有这个代码:


FILE * ptr ,* ptr1;

ptr = fopen(argv [1]," r"); // argv [1]是txt文件,其中包含

人/重量


while((fscanf(ptr,"%s%f" ,人[i] .name,& people [i] .weight))== 1



{

printf(" ;%s%f",people [i] .name,people [i] .weight);

i + = 1;

}

为什么这不起作用?什么都没打印出来。

Hi, I have a array structure called people

It is properly initialized to 100 array structure elements. Now, I''m
reading in from the command line a txt file, and I get them to open
correctly. In this .txt file, there is a name and a weight.

Josh 123.32
Jim 212.123
Steph 213.2

.... etc.

Now, I have this code:

FILE *ptr, *ptr1;
ptr = fopen(argv[1], "r"); //argv[1] is the txt file with al the
people/weight

while( (fscanf(ptr, "%s %f", people[i].name, &people[i].weight)) == 1
)
{
printf("%s %f", people[i].name, people[i].weight);
i += 1;
}
Why doesn''t this work? Nothing gets printed out.

推荐答案

pa******@gmail.com 说:


< snip>
pa******@gmail.com said:

<snip>

>

FILE * ptr,* ptr1;

ptr = fopen(argv [1], " R"); // argv [1]是带文件的txt文件

人/重量
>
FILE *ptr, *ptr1;
ptr = fopen(argv[1], "r"); //argv[1] is the txt file with al the
people/weight



这次没什么大不了的,但一般来说,明智的做法是避免在Usenet文章中发表评论 - 他们倾向于换行,这使得其他人很难编译你的代码。此外,他们从符合C90的实施中挑起诊断信息


It''s no big deal on this occasion, but in general it is wise to avoid //
comments in Usenet articles - they tend to wrap, making it difficult for
others to compile your code. Furthermore, they provoke diagnostic messages
from C90-conforming implementations.


>

while((fscanf (ptr,%s%f,人[i] .name,& people [i] .weight))== 1


>
while( (fscanf(ptr, "%s %f", people[i].name, &people[i].weight)) == 1
)



这里有两个问题。首先,你说你的结构中有100个元素

数组。如果我变成100,会发生什么?


第二个问题是你要求fscanf转换两个输入字段,

不是一个,所以你应该将它的返回值与2进行比较,而不是1.并且

这就是为什么你没有得到任何输出 - 循环失败的第一个

fscanf电话。这样做:


while(i< 100&

fscanf(ptr,

"%s) %f",

人[i] .name,

& people [i] .weight)== 2)


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Two problems here. Firstly, you said you have 100 elements in your struct
array. What happens if i ever becomes 100?

The second problem is that you''re asking fscanf to convert TWO input fields,
not one, so you should be comparing its return value against 2, not 1. And
that''s why you''re not getting any output - the loop is failing on the first
fscanf call. Do this instead:

while(i < 100 &&
fscanf(ptr,
"%s %f",
people[i].name,
&people[i].weight) == 2)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


pa ****** @ gmail.com 写道:

我有一个名为people的数组结构
Hi, I have a array structure called people



什么是数组结构?我认为你的意思是一个结构数组

(即一个元素是结构的数组),但是你应该这样说。

What is an "array structure"? I think you mean an array of structures
(i.e., an array whose elements are structures), but you should say so.


它被正确初始化为100个数组结构元素。现在,我是从命令行读取一个txt文件的
,我让他们正确打开

。在这个.txt文件中,有一个名字和一个重量。


Josh 123.32

Jim 212.123

Steph 213.2


...等


现在,我有这段代码:


FILE * ptr, * ptr1的;
It is properly initialized to 100 array structure elements. Now, I''m
reading in from the command line a txt file, and I get them to open
correctly. In this .txt file, there is a name and a weight.

Josh 123.32
Jim 212.123
Steph 213.2

... etc.

Now, I have this code:

FILE *ptr, *ptr1;



你不能使用ptr1。

You don''t use ptr1.


ptr = fopen(argv [1]," ; R"); // argv [1]是txt文件,其中包含

人/权重
ptr = fopen(argv[1], "r"); //argv[1] is the txt file with al the
people/weight



fopen()可能会失败。你不能查看结果。

fopen() can fail. You don''t check the result.


while((fscanf(ptr,"%s%f",people [i] .name,& people [i] .weight))== 1


while( (fscanf(ptr, "%s %f", people[i].name, &people[i].weight)) == 1
)



fscanf()返回分配的输入项目数或EOF(a

负值)如果在任何转换之前发生故障。如果这个

调用成功读取名称和权重,它将返回2,并且

你的while循环永远不会执行。

fscanf() returns the number of input items assigned, or EOF (a
negative value) if a failure occurs before any conversion. If this
call successfully reads the name and the weight, it will return 2, and
your while loop will never execute.


{

printf("%s%f",people [i] .name,people [i] .weight);

i + = 1;
{
printf("%s %f", people[i].name, people[i].weight);
i += 1;



i + = 1;并没有错,但是i ++;是更惯用的。

There''s nothing wrong with "i += 1;", but "i++;" is more idiomatic.


}


为什么这不起作用?什么都没打印出来。
}
Why doesn''t this work? Nothing gets printed out.



见上文。


另外,fscanf()并不能很好地处理输入错误。它不是以b $ b为线的;它在读取

项之前跳过空格(在大多数情况下),并且可以包含一个或多个换行符。 %s是指%s。格式是

危险;它读取一个以空格分隔的单个字符串(例如,给定

John Smith它只会读取John),但它指定没有限制

on字符串的大小。如果你已经为

人[i] .name分配了20个字符,但是输入的字符串是25个字符,那么你可以随意删除

相邻内存结果不好。


是名称成员一个字符数组或指针?如果它是一个

指针,你需要明确地为它分配内存,然后才能读入一个值。无论哪种方式,你都可以并且应该告诉fscanf不要使用
读取更多的字符(参见

语法的文档)。


另一种选择是使用fgets()一次读取一行,然后使用

sscanf()来解析它。 fgets()在长行中有一些问题;

如果这是一个问题,有很多解决方法。 (例如,参见

CBFalconer'的ggets()。)


-

Keith Thompson(The_Other_Keith)< a href =mailto:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

See above.

Also, fscanf() doesn''t deal well with input errors. It isn''t
line-oriented; it skips whitespace (in most cases) before reading an
item, and that can include one or more newlines. The "%s" format is
dangerous; it reads a single whitespace-delimited string (e.g., given
"John Smith" it will only read the "John"), but it specifies no limit
on the size of the string. If you''ve allocated 20 characters for
people[i].name, but the input string is 25 characters, you''ll clobber
adjacent memory, with arbitrarily bad results.

Is the "name" member a character array or a pointer? If it''s a
pointer, you need to allocate memory for it explicitly before you read
a value into it. Either way, you can and should tell fscanf not to
read more characters than it can hold (see the documentation for the
syntax).

Another option is to read a line at a time using fgets() and then use
sscanf() to parse it. fgets() has some problems with long lines;
there are a number of workarounds if that''s an issue. (See
CBFalconer''s ggets(), for example.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




您好,我已向您发送了我的代码和支持信息的电子邮件。< br $> b $ b谢谢:)


11月29日晚上9点08分,Richard Heathfield< r ... @ see.sig.invalidwrote:

Hi, I''ve sent you an e-mail of my code and supporting information.
Thanks :)

On Nov 29, 9:08 pm, Richard Heathfield <r...@see.sig.invalidwrote:

padh .... @ gmail.com说:


< snip>
padh....@gmail.com said:

<snip>

FILE * ptr,* ptr1;

ptr = fopen(argv [1]," r"); // argv [1]是带有al /

people / weightIt的txt文件,在这种情况下没什么大不了的,但总的来说避免使用//
FILE *ptr, *ptr1;
ptr = fopen(argv[1], "r"); //argv[1] is the txt file with al the
people/weightIt''s no big deal on this occasion, but in general it is wise to avoid //



在Usenet文章中的评论 - 它们倾向于换行,这使得其他人很难编译你的代码。此外,他们从符合C90的实施中挑起诊断信息


comments in Usenet articles - they tend to wrap, making it difficult for
others to compile your code. Furthermore, they provoke diagnostic messages
from C90-conforming implementations.


while((fscanf(ptr,"%s%f" ,人[i] .name,& people [i] .weight))== 1

)这里有两个问题。首先,你说你的struct
while( (fscanf(ptr, "%s %f", people[i].name, &people[i].weight)) == 1
)Two problems here. Firstly, you said you have 100 elements in your struct



数组中有100个元素。如果我变成100,会发生什么?


第二个问题是你要求fscanf转换两个输入字段,

不是一个,所以你应该将它的返回值与2进行比较,而不是1.并且

这就是为什么你没有得到任何输出 - 循环失败的第一个

fscanf电话。这样做:


while(i< 100&

fscanf(ptr,

"%s) %f",

人[i] .name,

& people [i] .weight)== 2)


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7 / 1999http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

array. What happens if i ever becomes 100?

The second problem is that you''re asking fscanf to convert TWO input fields,
not one, so you should be comparing its return value against 2, not 1. And
that''s why you''re not getting any output - the loop is failing on the first
fscanf call. Do this instead:

while(i < 100 &&
fscanf(ptr,
"%s %f",
people[i].name,
&people[i].weight) == 2)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于C文件I / O ...使用数组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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