Reading an array in a while not end of file loop

This is a discussion on Reading an array in a while not end of file loop within the C Programming forums, part of the General Programming Boards category; As part of a program, I have to read golf scores for multiple people into arrays. With the structure of ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    Question Reading an array in a while not end of file loop

    As part of a program, I have to read golf scores for multiple people into arrays. With the structure of the program, I have to load the array, use it to perform basic operations, and then load the next one, until the end of the input file. Here's what I have so far, where strokes[] is the array, golf is the input file, and result is the output file:

    Code:
    i = 1;
    while(!feof(golf))
    {
        for(i = 1; i <= 18; i++)
        {
             fscanf(golf, "%d", &strokes[i]);
             fprintf(result, "%d", strokes[i]);
        }
        i = 1;
        fscanf(golf, "%d", &strokes[i]);
    }
    I've been using the print for now just as a test to see if the data is reading correctly. I don't know if something is missing or what, but I keep getting infinite loops. I'm new to programming, so any help would be greatly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    A couple of things i noted:
    feof is bad
    an array starts at 0 and goes to number of elements-1.

    And, without knowing exactly what those functions you have there do, you dont seem to have any read function so feof will never be true. (not sure here so correct me if i am wrong).
    Last edited by Shakti; 10-18-2004 at 07:58 AM.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    Well, I have to use feof according to instructions. As far as the array goes, do you mean that I should set i to 0 and set it to less than 18 in the for statement? Would a while loop be better than a for loop for loading the array?

    Thanks

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    A for loop does just fine but the way you have you go out of bounds.
    Code:
    int i;
    for(i=0; i<18;i++)
    {
        // Do stuff
    }

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    Thanks for your help, I figured out what I need to do instead. Each row of scores has a name, so I'm using the name to find EOF, which translates to this:

    Code:
    int i;
    char name[15];
    
    fscanf(golf, "%s", name);
    while(!feof(golf))
    {
         fprintf(result, "\n%-15s\n", name);
         for(i = 0; i < 18, i++);
         {
              fscanf(golf, "%d", &strokes[i]);
              fprintf(result, "%d", &strokes[i]);
         }
         fscanf(golf, "%s", name);
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > Well, I have to use feof according to instructions.
    Even if that instruction turns out to be nonsense / inaccurate / wrong / bad advice
    Teachers and books are far from infallible.

    Your answer works, but you'll notice that you've now got an identical line of code in two different places. This is very bad from a maintenance point of view - what happens if the line has to change and only one of them gets changed?

    Assuming an otherwise well-formed file, you can do this
    Code:
    while ( fscanf(golf, "%s", name) == 1 ) {
         fprintf(result, "\n%-15s\n", name);
         for(i = 0; i < 18; i++) /* I hope this ; at the end of this line was a typo! (and the comma) */
         {
              fscanf(golf, "%d", &strokes[i]);
              fprintf(result, "%d", &strokes[i]);
         }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 01:36 AM
  5. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21