Read a File and Put Contents in an Array
-
01-eleven-2013 #1
Registered User
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 upward to 50 values from a .txt file, store the values in an array, and print the values to the user. Notwithstanding, when I run the program, it just outputs nothing to the user..but 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 plan 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 capeesh your assist!!
Give thanks Y'allAnd here's my lawmaking:
Code:
/*Written past: Kalpana Chinnappan Date: Jan 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\due north"); return 1; } else while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and cheque EOF { printf("nums[%d] is %d\n",i,nums[i]); i++; } return 0; }
-
01-11-2013 #2
Ultraviolence Connoisseur
Other and so the fact that:
Code:
//****************** lawmaking starts here *************** for(i=0;i<50;i++) //initialize array with 0 nums[i]=0;
Code:
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 #three
Ultraviolence Connoisseur
See, working fine for me:
Code:
$ cat exam.c #include <stdio.h> int primary(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\due north", 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[18] = 48 num[17] = 293847347 num[xvi] = 293847 num[15] = 34987 num[14] = 38947 num[13] = 9349873 num[12] = 28934 num[11] = 498724 num[10] = 348973 num[ix] = 293847 num[8] = 43298 num[7] = 43297 num[6] = 437 num[five] = 402348 num[4] = 340823 num[3] = 3498 num[2] = 344908 num[1] = 34 num[0] = 234
Last edited past nonpuz; 01-11-2013 at xi:59 PM. Reason: Pointed out the need for spring checking in the while loop
-
01-12-2013 #4
Registered User
Ohh ok, so I can just supersede that whole "for" loop with:
Lawmaking:
int nums[50] = {0};
and actually, the contents are:Code:
0 iii three ii 3 0 4 2 4 iv 2 0 0 0 4 2 3 iii 3 3 0 ii 0 0 1 1 1 2 3 4 4 0 3 four 0 0 3 3 4 4 4 4 0
OHHHH i only 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 come across if it works now!!!
Last edited by kal123456; 01-12-2013 at 12:06 AM.
-
01-12-2013 #5
Ultraviolence Connoisseur
Like I said in that location is aught in the code that is causing information technology 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 and so why non do something like this:
Code:
#include <stdio.h> int main(void) { int candidates[5] = {0}; int i; FILE * fp; /* annotation this has no 50 size limit as earlier.. */ 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 < 5; ++i) printf("Candidate #%d had %d votes\n", i, candidates[i]); return 0; }
-
01-12-2013 #vi
Registered User
ok let me see if the code that u gave me works...and it only gives me the black running window with a blank space at where the values are supposed to be printed, and so:
"Process returned 0 (0x0) execution time : 0.031 s
Press any key to go along."idk whats wrong!!
Last edited by kal123456; 01-12-2013 at 12:eighteen AM.
-
01-12-2013 #7
Ultraviolence Connoisseur
Read and Respond my questions, then possibly yous will effigy it out ?
-
01-12-2013 #viii
Registered User
Welcome to the forum, kal!
Your file is non being opened. Your program will only work if the data file is moved or copied into the aforementioned directory that it is located in.
Non "My Documents". Must exist the very same directory. Y'all aren't seeing the mistake bulletin, considering the console window is closing before you tin can see the bulletin.
Last edited past Adak; 01-12-2013 at 12:30 AM.
-
01-12-2013 #nine
Registered User
@nonpuz
Ok so you lot asked how I was executing it--I'm using codeblocks, just building and running the program. Ok, so I used the code yous merely gave me (the candidate one) and it's finally outputting some results!!! Give thanks you!! The only problem is that I need the up to 50 size limit to however be there (because what if I accept more than 50 numbers?), merely i'll endeavour and figure that out on my own. Also, the output that i become is:
Candidate #0 had 0 votes
Candidate #1 had 0 votes
Candidate #ii had 0 votes
Candidate #3 had 0 votes
Candidate #4 had 0 votes
Candidate #5 had 0 votes
Candidate #six had 0 votes
Candidate #seven had 0 votes
Candidate #viii had 0 votes
Candidate #9 had 0 votesProcess returned 0 (0x0) execution time : 0.031 southward
Press any fundamental to continue.In my example, at that place are only 5 candidates, so ignore the output stuff for "candidate v" to "candidate nine". Merely expect at the candidate vote count until "candidate 4". Somehow it says that all 5 candidates got 0 votes...how practise 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 information technology out on my own, It wouldnt exist off-white if u did my homework for me haha :P
-
01-12-2013 #10
Registered User
@Adak
Ohhhhh!! Wow deceit imagine why I couldnt figure that out earlier haha! Thank you!!
-
01-12-2013 #xi
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 not readable or fscanf is failing. Put a "printf()" call right subsequently the "fopen" call that only says "openned file successfully". Execute and run and see if it outputs opened file successfully. If it does, so move on and put a printf() telephone call in the while loop just earlier the ++candididate[i] line;
-
01-12-2013 #12
and the hat of int overfl
hargroveallonce1964.blogspot.com
Source: https://cboard.cprogramming.com/c-programming/153674-reading-file-storing-values-array-help-please-o.html
0 Response to "Read a File and Put Contents in an Array"
Postar um comentário