473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C File I/O... working with array structures

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.

Nov 30 '06 #1
10 1674
pa******@gmail.com said:

<snip>
>
FILE *ptr, *ptr1;
ptr = fopen(argv[1], "r"); //argv[1] is the txt file with al the
people/weight
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", people[i].name, &people[i].weight)) == 1
)
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.
Nov 30 '06 #2
pa******@gmail.com writes:
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.
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;
You don't use ptr1.
ptr = fopen(argv[1], "r"); //argv[1] is the txt file with al the
people/weight
fopen() can fail. You don't check the result.
while( (fscanf(ptr, "%s %f", people[i].name, &people[i].weight)) == 1
)
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;
There's nothing wrong with "i += 1;", but "i++;" is more idiomatic.
}
Why doesn't this work? Nothing gets printed out.
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.
Nov 30 '06 #3

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 said:

<snip>
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 //
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", people[i].name, &people[i].weight)) == 1
)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/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 30 '06 #4
pa******@gmail.com wrote:
Please don't top-post. Also don't quote the sigs unless your commenting
on them.
On Nov 29, 9:08 pm, Richard Heathfield <r...@see.sig.invalidwrote:
padh....@gmail.com said:

<snip>
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 //
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.
Hi, I've sent you an e-mail of my code and supporting information.
Thanks :)
Why? What's wrong with posting to the group and letting others give
their opinions?

Nov 30 '06 #5
On Thu, 30 Nov 2006 02:08:42 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>pa******@gmail.com said:

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

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.
It's trivial to handle this: if your C90-conforming implementation
does not support // comments, just upgrade to their C99-conforming
implementation :^)

--
jay
I use // comments and I've never had to upgrade!
Nov 30 '06 #6
jaysome said:

<snip>
>
It's trivial to handle this: if your C90-conforming implementation
does not support // comments, just upgrade to their C99-conforming
implementation :^)
My C90-conforming implementation has no C99-conforming equivalent.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 30 '06 #7
Richard Heathfield <rj*@see.sig.invalidwrites:
jaysome said:

<snip>
>>
It's trivial to handle this: if your C90-conforming implementation
does not support // comments, just upgrade to their C99-conforming
implementation :^)

My C90-conforming implementation has no C99-conforming equivalent.
And even if it did, it wouldn't handle // comments that had been
broken by line-wrapping.

--
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.
Nov 30 '06 #8
pa******@gmail.com said:
>
Hi, I've sent you an e-mail of my code and supporting information.
Presumably, then, you have a follow-up question, but it's not easy to see
what that is. Here are the results of my compilation:

bio1.c: In function `main':
bio1.c:53: warning: double format, different type arg (arg 2)
bio1.c:14: warning: unused parameter `argc'
bio1.c:56: warning: control reaches end of non-void function

The principal line it is complaining about is this:

printf("%f %s\n", acids[i].count, acids[i].name);

where you try to print an int value as if it were a double. The format
specifier for double is %f, but acids[i].count has type int. What is the
correct printf format specifier for int? (Use one side of the paper only. 3
marks. The use of calculators is not permitted.)

The other problems it is complaining about are these: you define, but don't
use, a parameter called argc. The solution is to use it, and there's an
excellent reason why you should: if argc is 2 or less, then your second
fopen call will fail. If it's 1 or less, your first fopen call will fail.
So it's a good idea to check argc before dooming your program to disaster.

Also, note that you have - perfectly correctly - defined main to return int,
as is right and proper and well-defined (unlike any other return type for
main). When we say a function returns a value of a given type, it's good to
return a value of that type from that function. So, just after your final
printf("\n"); but before your closing brace, why not add this line?

return 0;

to indicate (perhaps a trifle optimistically) that your program succeeded in
executing correctly and producing the desired results?

Once you have made these corrections, however, you will notice that your
program continues to produce incorrect results. Just because a program
compiles cleanly (at a decent warning level), that doesn't mean it's
correct! The most obvious problem is that you carefully count (using the
object known as i) how much data you read in - how many records, how many
lines, whatever you want to call it - but then you discard that
information, re-using i as a counter in the next loop. So your program
forgets how many of the 100 available array elements it actually used, and
instead tries to print all 100, most of which are likely to contain
indeterminate values.

After modifying your program to store that value (the number of lines read)
instead of discarding it, and using it in the conditional expression of
subsequent loops, I was able to run your program with what look like
correct results. I suggest, therefore, that you do the same.

One last thing: in future, if you wish to use me as a private consultant, it
might be wiser to find out my fee scale in advance.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 30 '06 #9
Richard Heathfield wrote:
pa******@gmail.com said:
>Hi, I've sent you an e-mail of my code and supporting information.
.... snip ...
>
One last thing: in future, if you wish to use me as a private
consultant, it might be wiser to find out my fee scale in advance.
At a guess, you spent less than a half day on it, so a minimum
charge (for such a half day) would be reasonable. Padh.ayo should
probably forward you about 400 pounds Sterling. I suggest he use
Paypal.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 30 '06 #10
jaysome wrote:
Richard Heathfield <rj*@see.sig.invalidwrote:
>pa******@gmail.com said:

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

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.

It's trivial to handle this: if your C90-conforming implementation
does not support // comments, just upgrade to their C99-conforming
implementation :^)
How did you manage to compile "people/weight"? They seem to be
undefined. I am omitting any discussion as to the availability of
that C99-conforming implementation.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 30 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Brian | last post by:
Hello, I have a text file I'm attempting to parse. There are about 50 fixed width fields in each line / row. For example (shortened for brevity): W1234Somebody East 101110001111010101...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
7
by: eriwik | last post by:
Hi, I'm working on a small application which processes PNG-images and need to read parts of them into structures and/or variables and I was wondering what assumptions one can make about how the...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
1
by: Galen Somerville | last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated. As you can see in the code below, my structures use fixed length strings and known array sizes. Consequently I can save...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
7
by: pereges | last post by:
I've to store an array of structures: typedef struct { double origin; double e_field_at_origin_real, e_field_at_origin_imag; double direction; double pathlength; int depth; }ray;
2
by: triphoppa | last post by:
I'm having a hard time discovering how to fill an array of structures from a file. This is what I'm trying to do. I am going to open a file from the command line. In the file are a bunch of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.