Read File in C and Store in Array
-
01-11-2013 #1
Registered User
reading from file and storing the values in an array!! HELP PLEASE!! :O
Hey everyone!
So i have a simple programme that's supposed to read upwards to 50 values from a .txt file, shop the values in an array, and print the values to the user. Yet, when I run the program, it but outputs nothing to the user..just a blank infinite, i don't see whatever values.... i created the .txt file and saved it into "My Documents" on my computer then I'm pretty sure the program knows how to admission information technology....maybe in that location'southward another mistake in my lawmaking that i'one thousand not catching?
If anyone would like to help, I would profoundly capeesh your help!!
ThanksAnd here's my code:
Lawmaking:
/*Written by: Kalpana Chinnappan Date: January 17, 2013 Homework 1 */ #include <stdio.h> int main (void) { int nums[fifty]; //up to 50 chemical 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; //make clean up and initialize LCV if ((fp1=fopen("votes.txt","r"))==Zero) { printf("votes.txt failed to open up\n"); return 1; } else while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and check EOF { printf("nums[%d] is %d\due north",i,nums[i]); i++; } return 0; }
-
01-11-2013 #2
Ultraviolence Connoisseur
Other then the fact that:
Code:
//****************** code starts here *************** for(i=0;i<l;i++) //initialize array with 0 nums[i]=0;
Code:
int nums[l] = {0};
Should exist something like:
Code:
230 2398 34988 30489 9488 8598 34893 48984 34989 489 49848 58958 985
-
01-11-2013 #three
Ultraviolence Connoisseur
See, working fine for me:
Code:
$ cat examination.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]); render 0; } $ true 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[xviii] = 48 num[17] = 293847347 num[16] = 293847 num[fifteen] = 34987 num[xiv] = 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[two] = 344908 num[1] = 34 num[0] = 234
Last edited by nonpuz; 01-11-2013 at 11:59 PM. Reason: Pointed out the demand for bound checking in the while loop
-
01-12-2013 #iv
Registered User
Ohh ok, so I can just supercede that whole "for" loop with:
Code:
int nums[l] = {0};
and actually, the contents are:Code:
0 3 three 2 3 0 4 two 4 iv two 0 0 0 4 two 3 three 3 three 0 2 0 0 1 i ane 2 iii 4 4 0 3 4 0 0 three 3 4 iv 4 iv 0
OHHHH i just saw your answer......maybe that'due south my problem, crusade I didn't include the i<l in my while loop....thanks soooo much for helping out!! lemme go and run across if information technology works now!!!
Final edited by kal123456; 01-12-2013 at 12:06 AM.
-
01-12-2013 #five
Ultraviolence Connoisseur
Like I said there is goose egg in the code that is causing it to not work. How are you executing it? Is it just running a quick popup window and and then disappears?
Regarding your logic, if each numerical value represents a candidate and so why non do something similar this:
Lawmaking:
#include <stdio.h> int main(void) { int candidates[five] = {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 nosotros got a valid vote, count it */ ++candidates[i]; } fclose(fp); } for (i = 0; i < v; ++i) printf("Candidate #%d had %d votes\north", i, candidates[i]); return 0; }
-
01-12-2013 #6
Registered User
ok let me encounter 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 incorrect!!
Final edited by kal123456; 01-12-2013 at 12:18 AM.
-
01-12-2013 #7
Ultraviolence Connoisseur
Read and ANSWER my questions, then maybe y'all will figure it out ?
-
01-12-2013 #8
Registered User
Welcome to the forum, kal!
Your file is non being opened. Your program will simply work if the data file is moved or copied into the same directory that it is located in.
Not "My Documents". Must exist the very aforementioned directory. You lot aren't seeing the error message, because the console window is closing before y'all can meet the message.
Terminal edited by Adak; 01-12-2013 at 12:30 AM.
-
01-12-2013 #9
Registered User
@nonpuz
Ok and so you lot asked how I was executing it--I'm using codeblocks, just building and running the plan. Ok, so I used the lawmaking you just gave me (the candidate one) and it'south finally outputting some results!!! Thank you!! The only problem is that I need the upwardly to 50 size limit to however be there (because what if I take more than 50 numbers?), but i'll try and effigy that out on my ain. Also, the output that i get is:
Candidate #0 had 0 votes
Candidate #i had 0 votes
Candidate #ii had 0 votes
Candidate #3 had 0 votes
Candidate #4 had 0 votes
Candidate #five had 0 votes
Candidate #6 had 0 votes
Candidate #7 had 0 votes
Candidate #8 had 0 votes
Candidate #nine had 0 votesProcedure returned 0 (0x0) execution time : 0.031 s
Press any central to go on.In my case, there are only five candidates, and so ignore the output stuff for "candidate 5" to "candidate ix". Just look at the candidate vote count until "candidate iv". Somehow information technology says that all 5 candidates got 0 votes...how do I become the program to really print out the number of votes each candidate has? delight requite me slight hints, I'll attempt to figure most of information technology out on my ain, It wouldnt exist fair if u did my homework for me haha :P
-
01-12-2013 #ten
Registered User
@Adak
Ohhhhh!! Wow cant imagine why I couldnt figure that out earlier haha! Thanks!!
-
01-12-2013 #11
Ultraviolence Connoisseur
Ok so that tells me that its not reading anything from your file. Either the file is not in the directory or it is non readable or fscanf is declining. Put a "printf()" call correct subsequently the "fopen" telephone call that only says "openned file successfully". Execute and run and meet if information technology outputs opened file successfully. If information technology does, then move on and put a printf() call in the while loop but before the ++candididate[i] line;
-
01-12-2013 #12
and the hat of int overfl
threlkeldpless1969.blogspot.com
Source: https://cboard.cprogramming.com/c-programming/153674-reading-file-storing-values-array-help-please-o.html