Thread: Not reading from file after a certain chunk of code

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Question Not reading from file after a certain chunk of code

    I have recently run into an issue where my program will read some numbers from a file and then stop reading them.

    In the next code:
    Code:
    int A[20];
    int B[20];


    Code:
    for(k=0 ; k < 20 ; k++){
                A[k] = 0;
                B[k] = 0;
            }
    
    
            k = 0;      //Reset value of k to 0
    
    
                //Take values for set A, this loop reads the numbers on the file correctly
            while(A[k] != (-1)) {
                fscanf(ifp, "%d", &A[k]);
                k++;
            }
    
    
            k = 0;      //Reset value of k to 0
    
    
                //Take values for set B, no longer reads values from the file, I confirmed that the loop actually works.
            while(B[k] != (-1)) {
                fscanf(ifp, "%d", &B[k]);
                k++;
            }
    Reading from the file
    3 2 1 5 4 -1
    5 8 4 -1
    A[] will be 3 2 1 5 4,
    but B[] will be 0 0 0 0...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your loop for A will read all the numbers, since A[k] != -1 must always be true (since at that point A[k] must always be zero, since you initialized them all to zero and you haven't read A[k] yet).

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Got it, thanks. I changed the while loops to do-while loops, and the evaluation is now A[k-1] != (-1) for both cases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption code file reading error---I'm using VI
    By SupermanC in forum C Programming
    Replies: 1
    Last Post: 12-03-2013, 09:48 PM
  2. Reading/Writing to a file error? (short code)
    By tmac619619 in forum C Programming
    Replies: 5
    Last Post: 10-15-2012, 07:00 PM
  3. Chunk Split
    By bobbelPoP in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2008, 08:32 PM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  5. File reading code - beyond help.
    By Brian in forum C Programming
    Replies: 3
    Last Post: 01-31-2002, 12:28 PM

Tags for this Thread