doing my head in...

This is a discussion on doing my head in... within the C Programming forums, part of the General Programming Boards category; the code below is ment to read the first char of every line of a .txt file except when it ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    doing my head in...

    the code below is ment to read the first char of every line of a .txt file
    except when it comes accorss grouped returns it has a habbit of skipping a line iv'e been sitting on this for almost 2-3 hours and im banging my head against the wall with it...
    I tryed to catch it with an if statement but it's not quite working.

    Heres the code thats troubleing me:
    Code:
    char reader; //Reads the file
    
    FILE *obj = NULL;
    obj = fopen("test1.obj", "r"); //Open File
    
    
    if(obj == NULL) //Error Check
    {cout << "file not opened!";}
    else
    {cout<< "opened file! :)" << endl;}
    
    
    reader = getc(obj);
    cout << reader << endl;
    
    for (int x = 0; x < 10; x++) // get 10 lines of info
    {
    
    
    do //find the next line value
    {
    reader = getc(obj);
    }while(reader != '\n');
    
    reader = getc(obj); //goto next byte
    
    if(reader == '\n')
    {
    cout << "\n";
    }
    else
    {
    cout << reader << endl;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    'reader' should be an int, that way you can tell when you have reached EOF. You'll probably have to cast it when you output the values though because you're using C file i/o and C++ i/o.

    Can you give an example of output that you're getting and output that you expect?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,247
    On a quick look, I suspect your code will skip lines (except maybe the first time) if there is less than two characters in a line.

    It might also pay to check for, and respond to, any errors that occur when reading from the file.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Code:
    you're using C file i/o and C++ i/o.
    Yea Iv'e just recently moved from C to C++ i was origanally using the fstream but it didn't read the whitespace and could quite figgure out how to read it at a lower level so i just threw in the c code, I know it's now exatly the done thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program won't continue
    By cmsc in forum C Programming
    Replies: 2
    Last Post: 10-17-2009, 02:10 PM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 07:33 AM
  5. Linked list, is this correct?
    By scrappy in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 11:06 PM

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