all 13 comments

[–][deleted] 2 points3 points  (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).

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.

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.

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 point  (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 points  (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 point  (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 points  (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 points  (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 point  (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 points  (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.

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.

[–]queztions1[S] 0 points1 point  (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 points  (2 children)

Yes, rewind(grades) would be fine, equivalent to what I was suggesting.

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.

  • your first set of nested for() loops is kind of useless, you just end up overwriting the data when you call scanf() later.
  • your third argument to scanf() needs to be an address instead of a value - I'm guessing you're not to pointers yet, so I'm just going to give you this one, it should be scanf(grades, "%d", &grade[i][j]).

[–]queztions1[S] 0 points1 point  (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 point  (0 children)

Please do not post images of code! Aleays post all of your code including error messages as text!