Read an Array From a File in C
-
01-11-2013 #1
Registered User
reading from file and storing the values in an assortment!! HELP Please!! :O
Hey everyone!
So i have a simple program that'southward supposed to read up to 50 values from a .txt file, store the values in an array, and impress the values to the user. However, when I run the program, information technology just outputs nothing to the user..just a bare infinite, i don't encounter any values.... i created the .txt file and saved it into "My Documents" on my computer so I'1000 pretty sure the programme knows how to access it....maybe there's some other mistake in my code that i'm not communicable?
If anyone would like to help, I would profoundly appreciate your aid!!
Thank youAnd here's my code:
Lawmaking:
/*Written by: Kalpana Chinnappan Date: January 17, 2013 Homework one */ #include <stdio.h> int main (void) { int nums[l]; //up to 50 chemical element int array FILE *fp1; //file pointer int i; //****************** code starts here *************** for(i=0;i<l;i++) //initialize assortment with 0 nums[i]=0; i=0; //make clean upward and initialize LCV if ((fp1=fopen("votes.txt","r"))==NULL) { printf("votes.txt failed to open up\due north"); render ane; } else while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and check EOF { printf("nums[%d] is %d\n",i,nums[i]); i++; } return 0; }
-
01-eleven-2013 #2
Ultraviolence Connoisseur
Other and so the fact that:
Lawmaking:
//****************** lawmaking starts here *************** for(i=0;i<50;i++) //initialize array with 0 nums[i]=0;
Lawmaking:
int nums[l] = {0};
Should be something like:
Code:
230 2398 34988 30489 9488 8598 34893 48984 34989 489 49848 58958 985
-
01-11-2013 #iii
Ultraviolence Connoisseur
See, working fine for me:
Code:
$ true cat test.c #include <stdio.h> int main(void) { int nums[l] = {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[fourteen] = 38947 num[xiii] = 9349873 num[12] = 28934 num[11] = 498724 num[10] = 348973 num[nine] = 293847 num[8] = 43298 num[7] = 43297 num[6] = 437 num[5] = 402348 num[iv] = 340823 num[three] = 3498 num[2] = 344908 num[1] = 34 num[0] = 234
Last edited by nonpuz; 01-11-2013 at eleven:59 PM. Reason: Pointed out the need for jump checking in the while loop
-
01-12-2013 #4
Registered User
Ohh ok, so I can simply supplant that whole "for" loop with:
Code:
int nums[50] = {0};
and actually, the contents are:Code:
0 iii three 2 3 0 4 2 four 4 ii 0 0 0 4 ii 3 3 3 3 0 2 0 0 1 ane 1 ii 3 four 4 0 3 4 0 0 3 three four iv 4 iv 0
OHHHH i just saw your reply......maybe that'due south 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 past kal123456; 01-12-2013 at 12:06 AM.
-
01-12-2013 #5
Ultraviolence Connoisseur
Like I said there is nothing in the code that is causing it to not work. How are you executing information technology? Is it just running a quick popup window and so disappears?
Regarding your logic, if each numerical value represents a candidate so why not do something like this:
Code:
#include <stdio.h> int main(void) { int candidates[v] = {0}; int i; FILE * fp; /* note this has no l 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!\northward", i); go along; } /* otherwise we got a valid vote, count information technology */ ++candidates[i]; } fclose(fp); } for (i = 0; i < five; ++i) printf("Candidate #%d had %d votes\north", i, candidates[i]); render 0; }
-
01-12-2013 #6
Registered User
ok let me come across 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 and then:
"Process returned 0 (0x0) execution fourth dimension : 0.031 south
Press any key to go along."idk whats wrong!!
Terminal edited by kal123456; 01-12-2013 at 12:18 AM.
-
01-12-2013 #7
Ultraviolence Connoisseur
Read and ANSWER my questions, then maybe you volition effigy it out ?
-
01-12-2013 #8
Registered User
Welcome to the forum, kal!
Your file is non being opened. Your program volition merely piece of work if the data file is moved or copied into the aforementioned 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 earlier you can see the message.
Final edited by Adak; 01-12-2013 at 12:xxx AM.
-
01-12-2013 #9
Registered User
@nonpuz
Ok so you lot asked how I was executing it--I'chiliad using codeblocks, just building and running the program. Ok, and so I used the lawmaking you simply gave me (the candidate one) and it'south finally outputting some results!!! Give thanks yous!! The only problem is that I need the upward to 50 size limit to still be there (because what if I take more than fifty numbers?), only i'll try and figure that out on my own. Likewise, the output that i get is:
Candidate #0 had 0 votes
Candidate #ane 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 #ix had 0 votesProcess returned 0 (0x0) execution time : 0.031 s
Press whatever key to go on.In my instance, there are only 5 candidates, so ignore the output stuff for "candidate 5" to "candidate 9". Just wait 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 really print out the number of votes each candidate has? delight requite me slight hints, I'll try to figure most of it out on my ain, It wouldnt be fair if u did my homework for me haha :P
-
01-12-2013 #10
Registered User
@Adak
Ohhhhh!! Wow cant imagine why I couldnt figure that out earlier haha! Thanks!!
-
01-12-2013 #xi
Ultraviolence Connoisseur
Ok so that tells me that its non reading anything from your file. Either the file is non in the directory or it is not readable or fscanf is declining. Put a "printf()" telephone call right later the "fopen" telephone call that only says "openned file successfully". Execute and run and encounter if it outputs opened file successfully. If it does, then movement on and put a printf() call in the while loop only before the ++candididate[i] line;
-
01-12-2013 #12
and the hat of int overfl
Source: https://cboard.cprogramming.com/c-programming/153674-reading-file-storing-values-array-help-please-o.html
0 Response to "Read an Array From a File in C"
Post a Comment