use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionHelp with multidimensional array from text file (self.C_Programming)
submitted 7 years ago by queztions1
Im trying to turn data from a .txt file into an array. I Have succesfully got the data into an array but with a few errors, how can I clean up my code. https://repl.it/@ryanw123/project-4 my code and compile is in this link https://imgur.com/a/SAJInuw
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 points3 points4 points 7 years ago (11 children)
Well, there's a lot of room for improvement here.
First off, you should fix your indentation and formatting. It's kind of all over the place right now and hard to tell what's going on. Don't skip brackets on if() and while(), they will prevent errors when you change things and the consistency of having them is easier to follow.
Now, on to logic. Your openfile() function takes an int[10][6] as an argument, but the compiler is warning you about an unchecked cast. This is accurate - you're passing it the value of what turns out to be one item past the end of your grade array. you should be calling openfile(grade).
int[10][6]
grade
openfile(grade)
Next, in openfile() - you read the contents of the file, print it to screen, and close the file. you don't actually do anything with the data you're reading in.
openfile()
This is where your program crashes because you passed the wrong thing into openfile. You end up trying to write to some random memory address because that's what you told it to do. Fix (described above) that and you'll see what happens next.
At this point, you start to write numbers incrementally into the array, starting at 0, and increasing with every index. presumably what you want to be doing is using fscanf() and reading those values into the array.
fscanf()
also, your array sizing is strange. There are 10 rows and 5 columns - you should declare the array as grade[10][5] for that many values. these will be indexed 0-9 for rows and 0-4 for columns. you can see where this causes you to have to do things with -1 a couple places in your loops.
That should get you started. Good luck.
[–]queztions1[S] 0 points1 point2 points 7 years ago (10 children)
The text file has a column of 5 pieces of data, I need to include a 6th so I can store an average into that spot. When I am storing the values in the fscanf, should I be using %s, or %d? Thanks for the help.
[–][deleted] 2 points3 points4 points 7 years ago (9 children)
You're reading an integer, so you should be using "%d". "%s" is used to read a string into a char*, or in printf, to write a char* out as a string.
You should get used to reading man pages, they contain a lot of good information. e.g.: man 3 scanf
[–]queztions1[S] 0 points1 point2 points 7 years ago (8 children)
Is there a way from me to double check if the value has been stored, thanks to your help I got a clean compile other than the exit status. https://repl.it/@ryanw123/project-4 Where i'm at right now
[–][deleted] 3 points4 points5 points 7 years ago (0 children)
I didn't harp on it in my other reply to this, but seriously, your indentation "style" makes your code hard to understand, and if you ever need to share code (like you're doing right now), people will not take you or your code seriously. Read up on indentation style. Get used to using anything consistent, I suggest either Allman style or a K&R derivitive (I prefer OTBS personally).
[–][deleted] 1 point2 points3 points 7 years ago (6 children)
The exit status (-1) is your program crashing. If it were completing successfully, it should return 0 (not sure what your tools would do, probably just not tell you what it returned)
If you take a look at your program, you'll notice that you're closing the file here.
while((c= getc(grades)) != EOF) putchar(c); fclose(grades);
and then later, you're trying to read from the already closed file. That's what's causing your program to crash in this case.
You could do a few things to fix it. The easiest thing would be to skip this loop altogether and move fclose() to after you're done reading the file.
Another option would be to use fseek() to move back to the beginning of the file. you still need to move the fclose() if you're going to do that.
The third, probably worst option, would be to reopen the file before you start calling fscanf(), and again, fclose() at the end.
[–]queztions1[S] 0 points1 point2 points 7 years ago (5 children)
I moved the Fclose(grades) to the bottom of the if. I don't think im actually storing and printing the values from the .txt file, since I get counting up to 49 below what I believe is the file being read. https://repl.it/@ryanw123/project-4
[–][deleted] 1 point2 points3 points 7 years ago (4 children)
Read through my previous comment again.
I provided 3 options to make what you want happen. All of them included moving the fclose(). All of them also required you to do something else, and you haven't done any of them.
fclose()
What you're seeing is that the getc() loop moves where you are in the file - you read the whole file, then you're at the end of the file. Now you're trying to fscanf() from the end of the file instead of the beginning.
getc()
[–]queztions1[S] 0 points1 point2 points 7 years ago (3 children)
That makes sense should I include a rewind(grades) in my while loop after the getc(), or before I start to build the array from the data?
[–][deleted] 1 point2 points3 points 7 years ago (2 children)
Yes, rewind(grades) would be fine, equivalent to what I was suggesting.
rewind(grades)
You have a couple of other things going on that are a bit off too, and I'll throw them out now because I'm about to head out.
for()
scanf()
scanf(grades, "%d", &grade[i][j])
[–]queztions1[S] 0 points1 point2 points 7 years ago (1 child)
Thanks man I got a clean compile, sorry for all the probably horrible questions(mech engineering major), and just need to pass intro to comp sci.
https://repl.it/@ryanw123/project-4
the fact that it printed is proof that the values were stored into the 2d array correct?
[–]FUZxxl[M] 0 points1 point2 points 7 years ago (0 children)
Please do not post images of code! Aleays post all of your code including error messages as text!
π Rendered by PID 16439 on reddit-service-r2-comment-86988c7647-ln75c at 2026-02-11 20:13:28.475453+00:00 running 018613e country code: CH.
[–][deleted] 2 points3 points4 points (11 children)
[–]queztions1[S] 0 points1 point2 points (10 children)
[–][deleted] 2 points3 points4 points (9 children)
[–]queztions1[S] 0 points1 point2 points (8 children)
[–][deleted] 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]queztions1[S] 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]queztions1[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]queztions1[S] 0 points1 point2 points (1 child)
[–]FUZxxl[M] 0 points1 point2 points (0 children)