This is a discussion on Reading textfile into array within the C Programming forums, part of the General Programming Boards category; What wrong with the following code? I am trying to read two text files into an array, one text file ...

1. ## Reading textfile into array

What wrong with the following code? I am trying to read two text files into an array, one text file into each column (2 columns and two text files in total) It runs, however once its finished it crashes?
Code:
file = fopen("C:\T0.txt", "r");
i = 0;
while (!feof(file)) {
/* loop through and store the numbers into the array */
fscanf(file, "%f\t", &init_T[i][0]);
i++;
}

file = fopen("C:\theta_f.txt", "r");
i = 0;
while (!feof(file)) {
/* loop through and store the numbers into the array */
fscanf(file, "%f\t", &init_T[i][1]);
i++;
}
for (i = 0; i <= num_inputs; i++)//Convert to radians
{
init_T[i][1] = init_T[i][1]*2*pi;

}
for (i = 0; i <= num_inputs; i++)
{
printf("%f    %f\n",init_T[i][0],init_T[i][1]);
}
according to the debugger, the error occurs on the 'while' line of the second while loop - the one reading from theta_f.txt

2. You need to close your files when you are done with them and you should check to make sure they opened correctly prior to trying to use the file handle. Additionally, read Why it's bad to use feof to control a loop.

EDIT: In fact your files are not opening. That is the main problem. In C you would need to have your path be "C:\\myFile.txt.." not what you have.

3. I have added the closing line:
Code:
file = fclose("C:\\T0.txt");
and the equiv for the other, the error is on the while line (I edited after you saw), does that mean there is a prob with the file, because the first of the two (T0.txt) works, however the other (theta_f.txt) doesnt.

4. Ok you need to have your program setup something like:
Code:
file = fopen("file1.txt","r")
if(file){
//do all your stuff with file1
fclose(file);
}else
//error processing for file not opening

file = fopen("file2.txt", "r");
if(file){
//same as above

5. Originally Posted by a.mlw.walker
I have added the closing line:
Code:
file = fclose("C:\\T0.txt");
and the equiv for the other, the error is on the while line (I edited after you saw), does that mean there is a prob with the file, because the first of the two (T0.txt) works, however the other (theta_f.txt) doesnt.
Does your compiler have any kind of help file with it? Something that details the standard library functions?

You really need to look these things up before just randomly guessing how they work...

fclose() takes a file handle not a filename.

Moerover you are not checking return values for fopen() or fscanf(), you are re-using the same file handle causing a memory leak as well as destroying the existing handle and your arrays are overrunning their boundaries.

Seriously... crack those textbooks and do some honest studying... this is not how you learn to program!

6. Yeah I thought it maybe something like that. In my first and second year at university we were programming in C the whole time, I guess I thought I would be able to remember the use of the commands, turns out I can remember about half of using commands. I'll get my C book out again.

7. Originally Posted by a.mlw.walker
Yeah I thought it maybe something like that. In my first and second year at university we were programming in C the whole time, I guess I thought I would be able to remember the use of the commands, turns out I can remember about half of using commands. I'll get my C book out again.
No offense, but based on your posts, you have retained less than half. For I quick review may I suggest looking at Cboard Tutorials and the CBoard FAQs.

8. Originally Posted by AndrewHunter
No offense, but based on your posts, you have retained less than half. For I quick review may I suggest looking at Cboard Tutorials and the CBoard FAQs.
The thing is that you shouldn't have to memorize the whole language and all it's library calls... It is far more important and a lot easier to gather the relevent documentation and cultivate a skill of "looking stuff up"...

To give an example... When I'm working on some new code it is very typical that I will have the Pelles C help file, the Windows SDK browser, one or more web pages and of course the IDE on my screen. Yes there are a number of things I remember outright, but on the slightest twinge of uncertainty, I will look up the function in question and make certain I'm getting it right... With a couple of hundred C Library calls and 30,000 Windows API calls available, there is no way anyone can hope to memorize even 10% of it....

It should also be noted that textbooks are a good starting point, they impart concept and structure but by no means are they a valid reference to the C library or even all of C's syntatical wizardry...

Yes indeedy, help files are our friends! (So why do people work so hard to avoid them???)

9. Originally Posted by CommonTater
The thing is that you shouldn't have to memorize the whole language and all it's library calls... It is far more important and a lot easier to gather the relevent documentation and cultivate a skill of "looking stuff up"...
I understand and agree with what you are saying and that is not in fact what I was referring to at all. My comment was more directed at the aggregate of all the posts from the OP, ranging from questions on function calls to variable declarations and the such. Hence, my suggestion so as to avoid more frustrations while continuing his education.

10. Originally Posted by AndrewHunter
I understand and agree with what you are saying and that is not in fact what I was referring to at all. My comment was more directed at the aggregate of all the posts from the OP, ranging from questions on function calls to variable declarations and the such. Hence, my suggestion so as to avoid more frustrations while continuing his education.
Yep, he definately needs a review... and I've told him so myself.

What I was referring to is the way he seems to guess at stuff, wasting his time and invoking ours, instead of spending a couple of minutes looking it up.