-1

I have this problem with my code. Im was copying some numbers from a file i had opened into an array using a for loop. And at the same time, i put a printf statement after the fscanf statement to see whether the value i wanted actually entered the arr. This works fine except that it puts an extra number at the end of the array when it's done. i don't understand why this happens. At first, i thought that I was using a wrong count of the number of elements in the file or that i had messed up while using malloc but those are completely fine. Can anyone pls tell what wrong. I can't share the code until a specific date cause i am a student and it would be considered cheating.

EDIT: Below is a link to a segment of what is happening in the code. THE LANGUAGE IS C. The code is meant to read the numbers 1 7 5 6 8 3 9 4 2 10 from a file a made but instead it always reads 1 7 5 6 8 3 9 4 2 10 1

EDIT: In the function declaration i meant char* file_name not int char*filename.

the image but i cant embed yet so a link

uche ozor
  • 35
  • 4
  • Your instructor probably meant it's cheating to share the code with other students in your class. Anyways, what happens if you use a different method for printing? (I don't know what language you're using so I can't give you an alternative) – dustytrash Sep 10 '18 at 18:32
  • 1
    Uche, unfortunately without code its difficult to help you. Its akin to me asking you to tell me how many fingers I'm holding up but won't show you my hand. Consider creating a small test case that encapsulates your problem that doesn't compromise your project. – Hogstrom Sep 10 '18 at 18:32
  • I have edited the question for those that answered before – uche ozor Sep 10 '18 at 22:00
  • 1
    You can paste the text of the code instead of an image. We prefer that anyway. – Robert Harvey Sep 10 '18 at 22:02

1 Answers1

0

There are multiple problems in your code:

  • there is a syntax error in the function prototype: int char* filename should just be const char *filename.
  • you do not test the return value of fopen(). You will have undefined behavior if the program cannot open the file.
  • you do not test if fscanf() succeeds at converting the file contents as an integer. You should verify the it returns 1 and handle the error if it does not.

It is quite likely that the number of values passed to the function exceeds the number of values actually present in the file. In this case, the contents of the destination array is not changed beyond the last value read and printf outputs whatever happens to be present there.

You should post the whole program as text in your question for a more complete analysis and potential corrections.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I would have but like i said i cant yet and i put that as someone asked me to put a segment that represents what im looking for. The syntax is actually correct. This is a function that is called after getting the count through means of converting the file contents as an integer and if it does return 1 then it should count the number. And there are specifications for what we should use in some cases. like i cant use the const char *filename in that situation – uche ozor Sep 10 '18 at 23:53
  • I'm sorry to insist, `int char* filename` is a syntax error. Can you show how the function `ArrayFiller` is invoked in your code? – chqrlie Sep 11 '18 at 13:26
  • Sorry, I see what you guys were referring to, that was a mistype on my part. That's not how its been in the code. – uche ozor Sep 11 '18 at 15:11