Read Text From File and Store in Array C#
-
01-11-2013 #one
Registered User
reading from file and storing the values in an assortment!! Help PLEASE!! :O
Hey everyone!
So i have a simple program that'due south supposed to read upwardly 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 plan, it merely outputs nothing to the user..just a bare space, i don't run into any values.... i created the .txt file and saved information technology into "My Documents" on my reckoner so I'm pretty sure the program knows how to admission it....maybe there's some other mistake in my lawmaking that i'1000 not catching?
If anyone would like to aid, I would greatly appreciate your aid!!
CheersAnd here's my lawmaking:
Code:
/*Written by: Kalpana Chinnappan Date: January 17, 2013 Homework 1 */ #include <stdio.h> int chief (void) { int nums[50]; //up to 50 element int assortment FILE *fp1; //file arrow int i; //****************** code starts hither *************** for(i=0;i<50;i++) //initialize array 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\north"); return ane; } else while((fscanf(fp1,"%d",&nums[i]))!=EOF) //scanf and check EOF { printf("nums[%d] is %d\northward",i,nums[i]); i++; } return 0; }
-
01-eleven-2013 #2
Ultraviolence Connoisseur
Other so the fact that:
Lawmaking:
//****************** lawmaking starts here *************** for(i=0;i<50;i++) //initialize assortment with 0 nums[i]=0;
Code:
int nums[fifty] = {0};
Should be something like:
Lawmaking:
230 2398 34988 30489 9488 8598 34893 48984 34989 489 49848 58958 985
-
01-11-2013 #iii
Ultraviolence Connoisseur
See, working fine for me:
Lawmaking:
$ 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\north", 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[viii] = 43298 num[7] = 43297 num[6] = 437 num[5] = 402348 num[four] = 340823 num[iii] = 3498 num[2] = 344908 num[one] = 34 num[0] = 234
Last edited by nonpuz; 01-eleven-2013 at xi:59 PM. Reason: Pointed out the need for bound checking in the while loop
-
01-12-2013 #4
Registered User
Ohh ok, so I can but replace that whole "for" loop with:
Code:
int nums[50] = {0};
and actually, the contents are:Code:
0 3 three ii 3 0 4 2 four 4 2 0 0 0 four 2 3 3 three 3 0 2 0 0 1 1 ane ii 3 four 4 0 3 4 0 0 3 three 4 four 4 four 0
OHHHH i simply saw your respond......maybe that's my problem, cause I didn't include the i<fifty in my while loop....thank you soooo much for helping out!! lemme go and meet if it works now!!!
Last edited past kal123456; 01-12-2013 at 12:06 AM.
-
01-12-2013 #5
Ultraviolence Connoisseur
Like I said at that place is zilch in the code that is causing information technology to non piece of 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 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 l size limit every bit before.. */ 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); proceed; } /* 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]); render 0; }
-
01-12-2013 #6
Registered User
ok let me meet if the lawmaking 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 fourth dimension : 0.031 s
Press any key to continue."idk whats wrong!!
Last edited past kal123456; 01-12-2013 at 12:18 AM.
-
01-12-2013 #7
Ultraviolence Connoisseur
Read and Reply my questions, then peradventure y'all will effigy it out ?
-
01-12-2013 #8
Registered User
Welcome to the forum, kal!
Your file is not beingness opened. Your plan volition 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 endmost before you can see the message.
Terminal edited by Adak; 01-12-2013 at 12:thirty AM.
-
01-12-2013 #9
Registered User
@nonpuz
Ok and then you asked how I was executing it--I'g using codeblocks, only building and running the plan. Ok, and so I used the code you lot just gave me (the candidate i) and information technology'southward finally outputting some results!!! Thank you!! The only problem is that I demand the up to l size limit to however be in that location (because what if I accept more 50 numbers?), but i'll effort and figure that out on my own. As well, the output that i get is:
Candidate #0 had 0 votes
Candidate #1 had 0 votes
Candidate #2 had 0 votes
Candidate #three had 0 votes
Candidate #4 had 0 votes
Candidate #5 had 0 votes
Candidate #6 had 0 votes
Candidate #seven had 0 votes
Candidate #8 had 0 votes
Candidate #9 had 0 votesProcedure returned 0 (0x0) execution fourth dimension : 0.031 s
Press any key to go on.In my instance, at that place are just v candidates, so ignore the output stuff for "candidate five" to "candidate ix". Just wait at the candidate vote count until "candidate 4". Somehow it says that all v candidates got 0 votes...how practise I become the program to really print out the number of votes each candidate has? please give me slight hints, I'll endeavor to figure near of it out on my own, 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 then that tells me that its non 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 come across if it outputs opened file successfully. If it does, then motility on and put a printf() telephone 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 "Read Text From File and Store in Array C#"
Post a Comment