Reading textfile into array

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. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    192

    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
    Last edited by a.mlw.walker; 08-13-2011 at 08:17 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.
    Last edited by AndrewHunter; 08-13-2011 at 08:19 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    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. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by a.mlw.walker View Post
    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. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    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. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by a.mlw.walker View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    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. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Split a textfile in an array
    By peterderijp in forum C Programming
    Replies: 15
    Last Post: 10-11-2010, 02:59 PM
  2. Load Array From Whitespace-delimited TextFile
    By pantherman34 in forum C Programming
    Replies: 7
    Last Post: 04-30-2010, 07:12 PM
  3. Replies: 9
    Last Post: 08-09-2008, 11:47 AM
  4. Reading a TextFile into a Linked List
    By cisco_leon in forum C Programming
    Replies: 13
    Last Post: 10-30-2006, 07:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21