Help with putting data into an array

This is a discussion on Help with putting data into an array within the C Programming forums, part of the General Programming Boards category; Hi guys and gals,
I'm new hear and to programming. I'm currently taking a class but I need a little ...

Help with putting data into an array

Hi guys and gals,

I'm new hear and to programming. I'm currently taking a class but I need a little help getting data from a txt file and putting it into array for me to be able to work with it. Here is sample of the code the teacher provides. The bottom section is the data that would be on the txt file.

It's a bit of a understanding issue. I'm confused with getting the data from the txt file and moving it into an array so i can do calucations of averages. I just need help with getting the data into the array.

Had I been able to take the 15BG class as well this quarter (being broke and jobless sucks), given the way Perry presented the sample data to use, my starting point would be in chapter 10 on page 639. Assuming you're using the same book. If you study Perry's sample data and figure 10-22 closely enough, you'll see what I mean. The trick, I suppose, would be to adapt the basic concept of the example program that follows to use fscanf to read the stuff from the file, rather than scanf from the keyboard.

1. I've NEVER seen fclose() fail [aside from when passed a NULL pointer - in which case it crashed badly].
2. For good measure, if it happens that fclose DOES fail, exiting might not be the BEST idea ever - there may be other files that you wish to close, etc. Just printing an error message if it happens is probably not a bad idea, but exiting may be a bit crude.

If you created a structure, with an array in the structure to store each line, you can then use fgets() to read each line into that array. You can then process each line in that array using sscanf() and break it up and store the item in the other elements of the structure.

Had I been able to take the 15BG class as well this quarter (being broke and jobless sucks), given the way Perry presented the sample data to use, my starting point would be in chapter 10 on page 639. Assuming you're using the same book. If you study Perry's sample data and figure 10-22 closely enough, you'll see what I mean. The trick, I suppose, would be to adapt the basic concept of the example program that follows to use fscanf to read the stuff from the file, rather than scanf from the keyboard.

Were you in Ahrens class last quarter?

HA haa. . . I was in his class. I'm thinking of dropping the class cus of money reason too.

Does C know to put the char into the right place and the int into the right place?? What if the is only 5 numbers?? Will the other 4 have 0 place in the values??

If you declare the structure outside(global) any function, including main(), I think the array elements will all be initialized to zero.

EDIT: I've have just tested this by declaring a global structure(with an int array). I printed out the array's values, and they were all zero. So they have all been initialized to zero.

fscanf() will only read what it can for each line, and put the values into array number[i]. For the values fscanf() can't read(because there is nothing to else read), the array elements will be already initialized to zero.