Thread: Need Help reading values from an .txt file and storing them to an array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Need Help reading values from an .txt file and storing them to an array

    Hello,
    I am trying to write a simple program that calls a function that takes in a file pointer for a txt document containing the different 'donation' values in a bank to collect to buy a nintendo game.

    I am trying to scan all the integer values in the txt file and store it into an array. The last number in the doc is ALWAYS going to be zero, thats guaranteed, thats what determines the end of the list. After I scan all the values and store them into an array, I am trying to print them out to the screen as a 'test' before I go any further with my program...

    Can someone help me please?

    Right now, after I try scan all the values in the .txt document and try to access for example SLOT number 2 in the array, it gives me an address or some garbage value when I try to print it to the screen.



    Code:
    #include <stdio.h>
    
    #define GAME 50
    
    int TotalDonations(FILE *ifp);
     
    int main (void) {
    
    	FILE *ifp;
    
    	ifp=fopen("nintendo.txt", "r");
    
    	TotalDonations(ifp);
    
    
    system("PAUSE");
    return 0;
    }
    
    int TotalDonations(FILE *ifp){
    
    	int num;
    	int donations[100], i=0;
    
       while(donations[i]!=0){
    		fscanf(ifp, "%d", &donations[i]);
    		i++;
    	}	
    		
    	printf("%d\n", donations[i]);
    
    
    
    }
    txtfile- Nintendo.txt

    3
    7
    2
    4
    5
    6
    7
    3
    2
    2
    10
    2
    3
    0
    Last edited by matthayzon89; 09-17-2010 at 04:53 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What is this line doing exactly?
    Code:
    while(donations[i]!=0)
    Explain.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Code:
    	int num;
    	int donations[100], i=0;
    
       while(donations[i]!=0){
    		fscanf(ifp, "%d", &donations[i]);
    		i++;
    	}
    Try working it through on paper, line by line, writing down the contents of the variables and array as you go. You should see the problem very quickly...

    Or if you'd rather just have the answer....

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by rags_to_riches View Post
    What is this line doing exactly?
    Code:
    while(donations[i]!=0)
    Explain.
    It compares array slot i to the value 0.
    If donations[i]==0 that means the we are at the end of the list and we are done reading in all the donation values, thats why I set the loop up saying while donation value's DONT equal zero then keep reading in and storing values..... I don't see why this shouldn't work...

    NOTE: ZERO indicates END of list. Zero will ALWAYS be the last number in the .txt doc. It simply a FLAG to stop reading in values.
    Last edited by matthayzon89; 09-17-2010 at 06:12 PM.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Code:
    	int num;
    	int donations[100], i=0;
    
       while(donations[i]!=0){
    		fscanf(ifp, "%d", &donations[i]);
    		i++;
    	}
    Ok, step by step. Let's say we have an input file like
    3
    4
    0

    Beginning the first loop...
    i = 0;
    What does donations[0] have in it at this point in the program?

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by smokeyangel View Post

    Ok, step by step. Let's say we have an input file like
    3
    4
    0

    Beginning the first loop...
    i = 0;
    What does donations[0] have in it at this point in the program?
    donations[0]== 3 so 3 !=0 therefore, the loop continues to read in values... right?

    Or maybe because I haven't initialized donations[0] to anything YET than the computer does not know how to perform the comparison when it is about to enter the while loop....

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    int donations[] is a static array, with local scope. Check your compiler, but you can't guarantee what values will be in the array. In other words, it has junk in it.

    (global arrays should be set to zero by the compiler).

    Although it's rare, donations[0] could be equal to 0, or any other value.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by Adak View Post
    int donations[] is a static array, with local scope. Check your compiler, but you can't guarantee what values will be in the array. In other words, it has junk in it.

    (global arrays should be set to zero by the compiler).

    Although it's rare, donations[0] could be equal to 0, or any other value.
    So what are my options as to going about fixing this problem? would using malloc fix this, since malloc allocates dynamic memory?

    Is there a way to read in all the values from an input file and store them in an array if the length or number of lines of the input data is unknown?
    Last edited by matthayzon89; 09-19-2010 at 07:53 PM.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Help anyone?
    Please

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could just set donations[i]=1, before the while loop starts.

    As long as you don't increment i, the value of 1 you give it, will be over-ridden inside the while loop.

    I'm not sure you want to declare the donations array, outside of main, however. When you leave the function totalDonations(), the donations array will be destroyed.

    Either malloc it (anywhere), and return a pointer to it back to main(), or make it static, and in main(), and then pass the name of the array (which acts as a pointer), to each function you call.
    Last edited by Adak; 09-19-2010 at 08:02 PM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Code:
       while(donations[i]!=0){
          fscanf(ifp, "%d", &donations[i]);
          i++;
       }
    I would change the above to:

    Code:
       while( fscanf(ifp, "%d", &donations[i]) != 0){
          printf("%d\n", donations[i]);
          i++;
       }
    This should get all the lines into donations.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Hello,
    I do not know how to slove this and I have been trying to figure this out for a long time. I have a test coming up tomorrow and I need to know this.

    (82 – 41) x 6 mod 3 <-The answer should be 29 here but I get 0


    (- 40 x 144) mod 71 <--for this I get 9 for the answer but it shows 62?

    ( I am checking the answers on google calc and wolframalpha.com..)


    Can anyone help?

    Thanks in advance (I did not want to start a new thread for such a small question)
    Last edited by matthayzon89; 09-23-2010 at 10:19 AM.

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A modification to jimblumberg's code (his does not check for array overflow):
    Code:
    while( i < 100 && fscanf(ifp, "%d", &donations[i]) == 1){
        printf("%d\n", donations[i]);
        i++;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  3. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  4. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM