Thread: reading from file and storing the values in an array!! HELP PLEASE!! :O

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53

    Exclamation reading from file and storing the values in an array!! HELP PLEASE!! :O

    Hey everyone!

    So i have a simple program that's supposed to read up to 50 values from a .txt file, store the values in an array, and print the values to the user. However, when I run the program, it just outputs nothing to the user..just a blank space, i don't see any values.... i created the .txt file and saved it into "My Documents" on my computer so I'm pretty sure the program knows how to access it....maybe there's another mistake in my code that i'm not catching?

    If anyone would like to help, I would greatly appreciate your help!!
    Thank You

    And here's my code:
    Code:
    /*Written by: Kalpana Chinnappan
    Date: January 17, 2013
    Homework 1
    */
        #include <stdio.h>
    
        int main (void)
     {
        int nums[50];   //up to 50 element int array
        FILE *fp1;      //file pointer
        int i;
    
        //******************  code starts here ***************
        for(i=0;i<50;i++)   //initialize array with 0
            nums[i]=0;
        i=0;        //clean up and initialize LCV
        if ((fp1=fopen("votes.txt","r"))==NULL)
        {
        printf("votes.txt failed to open\n");
        return 1;
        }
        else
            while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and check EOF
            {
                printf("nums[%d] is %d\n",i,nums[i]);
                i++;
            }
    
    
        return 0;
     }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Other then the fact that:
    Code:
        //******************  code starts here ***************
        for(i=0;i<50;i++)   //initialize array with 0
            nums[i]=0;
    Can easily be done at initialization:
    Code:
    int nums[50] = {0};
    There is nothing wrong with the code and as long as votes.txt is in the current directory the program is run from, should work fine. What are the contents of votes.txt?
    Should be something like:
    Code:
    230 2398 34988 30489 9488 8598 34893 48984
    34989 489 49848 58958 985

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    See, working fine for me:

    Code:
    $ cat test.c
    #include <stdio.h>
    
     
    int main(void)
    {
        int nums[50] = {0};
        int i = 0;
        FILE * fp;
    
        if (fp = fopen("votes.txt", "r")) {
            while (fscanf(fp, "%d", &nums[i]) != EOF) {
                ++i;
            }
            fclose(fp);
        }
    
        for (--i; i >= 0; --i)
            printf("num[%d] = %d\n", i, nums[i]);
    
        return 0;
    }
    $ cat votes.txt 
    234 34 344908 3498 340823 402348 437 43297 43298
    293847 348973 498724 28934 
    9349873 38947 34987 293847 293847347 48
    $ ./a.out 
    num[18] = 48
    num[17] = 293847347
    num[16] = 293847
    num[15] = 34987
    num[14] = 38947
    num[13] = 9349873
    num[12] = 28934
    num[11] = 498724
    num[10] = 348973
    num[9] = 293847
    num[8] = 43298
    num[7] = 43297
    num[6] = 437
    num[5] = 402348
    num[4] = 340823
    num[3] = 3498
    num[2] = 344908
    num[1] = 34
    num[0] = 234
    You should really throw a i < 50 check in the while () condition as well.
    Last edited by nonpuz; 01-11-2013 at 11:59 PM. Reason: Pointed out the need for bound checking in the while loop

  4. #4
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Ohh ok, so I can just replace that whole "for" loop with:

    Code:
     
    int nums[50] = {0};
    I dont know if i'll do it, but it should work both ways, thanks for showing me a simpler way
    and actually, the contents are:

    Code:
    
    0 3 3 2 3 0 4 2 4 4 2 0 0 0 4 2 3 3 3 3 0 2 0 0 1 1 1 2 3 4 4 0 3 4 0 0 3 3 4 4 4 4 0
    
    


    OHHHH i just saw your reply......maybe that's my problem, cause I didn't include the i<50 in my while loop....thanks soooo much for helping out!! lemme go and see if it works now!!!







    Last edited by kal123456; 01-12-2013 at 12:06 AM.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Like I said there is nothing in the code that is causing it to not work. How are you executing it? Is it just running a quick popup window and then disappears?

    Regarding your logic, if each numerical value represents a candidate then why not do something like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int candidates[5] = {0};
        int i;
        FILE * fp;
    
        /* note this has no 50 size limit as before.. */
        if (fp = fopen("votes.txt", "r")) {
            while (fscanf(fp, "%d", &i) != EOF) {
                /* invalid vote (out of range */
                if (i < 0 || i > 5) {
                    fprintf(stderr, "Invalid Candidate: %d!\n", i);
                    continue;
                }
    
                /* otherwise we got a valid vote, count it */
                ++candidates[i];
            }
            fclose(fp);
        }
    
        for (i = 0; i < 5; ++i) 
            printf("Candidate #%d had %d votes\n", i, candidates[i]);
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    ok let me see if the code that u gave me works...and it just gives me the black running window with a blank space at where the values are supposed to be printed, and then:

    "Process returned 0 (0x0) execution time : 0.031 s
    Press any key to continue."

    idk whats wrong!!
    Last edited by kal123456; 01-12-2013 at 12:18 AM.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Read and ANSWER my questions, then maybe you will figure it out ?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, kal!

    Your file is not being opened. Your program will only work if the data file is moved or copied into the same directory that it is located in.

    Not "My Documents". Must be the very same directory. You aren't seeing the error message, because the console window is closing before you can see the message.
    Last edited by Adak; 01-12-2013 at 12:30 AM.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @nonpuz

    Ok so you asked how I was executing it--I'm using codeblocks, just building and running the program. Ok, so I used the code you just gave me (the candidate one) and it's finally outputting some results!!! Thank you!! The only problem is that I need the up to 50 size limit to still be there (because what if I have more than 50 numbers?), but i'll try and figure that out on my own. Also, the output that i get is:

    Candidate #0 had 0 votes
    Candidate #1 had 0 votes
    Candidate #2 had 0 votes
    Candidate #3 had 0 votes
    Candidate #4 had 0 votes
    Candidate #5 had 0 votes
    Candidate #6 had 0 votes
    Candidate #7 had 0 votes
    Candidate #8 had 0 votes
    Candidate #9 had 0 votes

    Process returned 0 (0x0) execution time : 0.031 s
    Press any key to continue.

    In my case, there are only 5 candidates, so ignore the output stuff for "candidate 5" to "candidate 9". Just look at the candidate vote count until "candidate 4". Somehow it says that all 5 candidates got 0 votes...how do I get the program to actually print out the number of votes each candidate has? please give me slight hints, I'll try to figure most of it out on my own, It wouldnt be fair if u did my homework for me haha :P

  10. #10
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @Adak
    Ohhhhh!! Wow cant imagine why I couldnt figure that out earlier haha! Thanks!!

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Ok so that tells me that its not reading anything from your file. Either the file is not in the directory or it is not readable or fscanf is failing. Put a "printf()" call right after the "fopen" call that just says "openned file successfully". Execute and run and see if it outputs opened file successfully. If it does, then move on and put a printf() call in the while loop just before the ++candididate[i] line;

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from a file into array of structs
    By ryanmcclure4 in forum C Programming
    Replies: 3
    Last Post: 05-11-2012, 09:37 AM
  2. Replies: 7
    Last Post: 04-25-2012, 04:30 AM
  3. Reading from File and Storing into Array
    By Magic staple in forum C Programming
    Replies: 13
    Last Post: 12-05-2011, 12:00 AM
  4. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  5. 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

Tags for this Thread