Thread: reading int array from file

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    Question reading int array from file

    I am trying to read an unknown number of integer test scores from a file into an array. None of the input is > 100, but i keep getting garbage like -32451 and such. Almost like reading the wrong data type or something.

    int count = 1;
    FILE *fin;
    fin = fopen ( "lab06.dat", "r" );

    while ( fscanf ( fin, "%d", numbers[count] ) != EOF )
    count ++;

    return ( count );


    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I'm surprised it isn't falling over in a big heap
      numbers[count]
    should be
      &numbers[count]

    > int count = 1;
    And arrays start at 0
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    Thanks. I thought it was prolly something simple.

    The instructor wanted this array to strt at 1 to make formatting the output easier, but thanks for that too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Replies: 2
    Last Post: 03-24-2006, 08:36 PM