C++ Read Int From File to Array
-
01-11-2013 #1
Registered User
reading from file and storing the values in an array!! HELP PLEASE!! :O
Hey everyone!
Then i have a simple programme that's supposed to read upwardly to 50 values from a .txt file, store the values in an assortment, and impress the values to the user. However, when I run the program, it just outputs nothing to the user..but a blank infinite, i don't run across any values.... i created the .txt file and saved information technology into "My Documents" on my computer so I'k pretty sure the program knows how to access it....maybe there'southward some other fault in my code that i'm not catching?
If anyone would similar to help, I would greatly appreciate your assist!!
Give thanks Y'allAnd here's my code:
Code:
/*Written by: Kalpana Chinnappan Date: Jan 17, 2013 Homework ane */ #include <stdio.h> int main (void) { int nums[50]; //upwardly to fifty element int assortment 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"); render 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 then 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 #three
Ultraviolence Connoisseur
See, working fine for me:
Code:
$ cat test.c #include <stdio.h> int chief(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]); 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[16] = 293847 num[15] = 34987 num[14] = 38947 num[thirteen] = 9349873 num[12] = 28934 num[eleven] = 498724 num[ten] = 348973 num[nine] = 293847 num[8] = 43298 num[vii] = 43297 num[half dozen] = 437 num[v] = 402348 num[4] = 340823 num[3] = 3498 num[two] = 344908 num[ane] = 34 num[0] = 234
Last edited by nonpuz; 01-eleven-2013 at 11:59 PM. Reason: Pointed out the demand for bound checking in the while loop
-
01-12-2013 #four
Registered User
Ohh ok, and so I can just supplant that whole "for" loop with:
Lawmaking:
int nums[50] = {0};
and actually, the contents are:Code:
0 3 3 2 3 0 4 two iv iv two 0 0 0 4 2 3 3 3 iii 0 ii 0 0 1 1 1 ii iii 4 4 0 3 4 0 0 iii three iv 4 4 4 0
OHHHH i just saw your reply......peradventure that's my problem, cause I didn't include the i<fifty 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 but running a quick popup window and then disappears?
Regarding your logic, if each numerical value represents a candidate and then why not practise 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 every bit earlier.. */ if (fp = fopen("votes.txt", "r")) { while (fscanf(fp, "%d", &i) != EOF) { /* invalid vote (out of range */ if (i < 0 || i > v) { fprintf(stderr, "Invalid Candidate: %d!\n", i); keep; } /* 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; }
-
01-12-2013 #6
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 bare space at where the values are supposed to be printed, then:
"Procedure returned 0 (0x0) execution fourth dimension : 0.031 s
Press any central to continue."idk whats wrong!!
Final edited by kal123456; 01-12-2013 at 12:18 AM.
-
01-12-2013 #7
Ultraviolence Connoisseur
Read and Answer my questions, so maybe you will figure it out ?
-
01-12-2013 #viii
Registered User
Welcome to the forum, kal!
Your file is not being opened. Your program volition only piece of 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 lot aren't seeing the error message, because the console window is closing before yous can see the message.
Last edited by Adak; 01-12-2013 at 12:30 AM.
-
01-12-2013 #9
Registered User
@nonpuz
Ok so you asked how I was executing it--I'm using codeblocks, just building and running the program. Ok, and then I used the lawmaking you only gave me (the candidate ane) and information technology's finally outputting some results!!! Thanks!! The only problem is that I need the up to 50 size limit to still be at that place (because what if I take more than fifty numbers?), merely i'll try and figure that out on my ain. Besides, 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 #iv had 0 votes
Candidate #v 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 fourth dimension : 0.031 south
Press whatever key to continue.In my instance, there are but v candidates, so ignore the output stuff for "candidate 5" to "candidate nine". But look at the candidate vote count until "candidate four". Somehow it says that all 5 candidates got 0 votes...how do I get the program to really impress out the number of votes each candidate has? delight give me slight hints, I'll try to figure nearly of it out on my ain, It wouldnt be fair if u did my homework for me haha :P
-
01-12-2013 #ten
Registered User
@Adak
Ohhhhh!! Wow deceit imagine why I couldnt effigy that out earlier haha! Thanks!!
-
01-12-2013 #xi
Ultraviolence Connoisseur
Ok so that tells me that its not reading anything from your file. Either the file is non 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 information technology does, then move on and put a printf() call in the while loop just 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 "C++ Read Int From File to Array"
Post a Comment