Assigning values to arrays using fscanf

This is a discussion on Assigning values to arrays using fscanf within the C Programming forums, part of the General Programming Boards category; For a project for my intro C programming course, I need to read values from a file and store those ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    5

    Assigning values to arrays using fscanf

    For a project for my intro C programming course, I need to read values from a file and store those in an array. The file is in two columns, and i need to store the contents of one column in one array, and of the second column in the second array. Unfortunately, the code I have doesn't seem to be working at all and I don't know why.

    Here's the code I have:

    Code:
    void datatoarray(int section[], int scores[])
    {
       FILE *file;
       int lcv;
    
       file = fopen("exam1.txt", "r");
    
       for (lcv = 1; lcv > STUDENTS; lcv++)
       {
          fscanf(file, "%d %d", &section[lcv], %scores[lcv]);
       }
    
       fclose(file);
    }
    STUDENTS is defined as being equal to 100, the total number of lines in the file.

    The file looks like this:
    301 78
    101 93
    401 96
    101 93
    410 102
    101 78

    and so on...

    This is probably a really simple problem, but any help would be appreciated. Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    5
    Actually I just realized I can attach the file so I'm going to do that.
    Attached Files Attached Files

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >STUDENTS is defined as being equal to 100
    Code:
    for (lcv = 1; lcv > STUDENTS; lcv++)
    When do you suppose 1 will be greater than 100?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    Also...

    >> lcv = 1

    Array indexes start at '0', not '1'.

    >> file = fopen("exam1.txt", "r");

    Always test this return value. If it's NULL, the program will make an unglorious exit.

    >> fscanf(file, "%d %d", žion[lcv], %scores[lcv]);

    You can check the return value of fscanf, it will indicate how many items were read.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    5
    thank you...i knew it must be a dumb error, but i couldn't catch it.

    sebastiani, how do you check the return value?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how do you check the return value?
    Code:
    if (fscanf(...) != 2)
    {
      error
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. Confusion with assigning values to structs.
    By Tronic in forum C++ Programming
    Replies: 9
    Last Post: 03-22-2004, 07:01 AM
  4. Assigning string to values
    By Itanium in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2003, 07:28 AM
  5. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 08:33 AM

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