Reading text file by line number

This is a discussion on Reading text file by line number within the C++ Programming forums, part of the General Programming Boards category; This assignment is driving me insane! I have a text file that looks like this: Code: 3 CODE TIME DESCRIPTION ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    Reading text file by line number

    This assignment is driving me insane!

    I have a text file that looks like this:
    Code:
    3
    CODE TIME DESCRIPTION
    CODE TIME DESCRIPTION
    CODE TIME DESCRIPTION
    4
    CODE1 CODE2 TIME1 TIME2
    CODE1 CODE2 TIME1 TIME2
    CODE1 CODE2 TIME1 TIME2
    CODE1 CODE2 TIME1 TIME2
    All of the codes are a 3 letter code, the times are in the form 00:00 and the description is the full version of the code. The first number tells you how many lines follow it and the second tells how many lines follow that.

    I'm trying to read the file one line at a time, by reading the first line, the next group of lines, the next number line and the last lines. But I'm stuck due to the line number having to be converted to a string. Is there any way of using an int for the line number?

    Here's my code so far:
    Code:
    ifstream inFile;
         inFile.open(fileName.c_str(), ios::in);
         
         if(!inFile) {
             cerr << "Error: can't open input file" << fileName << endl;
             exit(1);
         }
         
         int noOfAirports;
         string aLine = "1";
         
         //Reads line 1 of file to find out how many airports there are
         while(getline(inFile, aLine)) {
             inFile >> noOfAirports;
         }
         
         int lineNo = 2 + noOfAirports;
         string bLine;
         stringstream out;
         out << lineNo;
         bLine = out.str();
    
         //code needs to go here to read next group of lines, something along the lines of for(int i=2; i <(lineNo); i++)
         
         int noOfFlights;
         //Reads file to find out how many flights there are
         while(getline(inFile, bLine)) {
             inFile >> noOfFlights;
         }
    
         //code here some sort of loop to read from noOfFlights + 1 to inFile.eof()
         
         inFile.close();

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, you can use strtol on the c_str() element of the string that you read in, I suppose. Or read it in as a stringstream and then write it from the stringstream into an int.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Alright, so I've ended up doing this and it compiles:

    Code:
    for (int i=2; i < lineNo; i++) {
             string cLine;
             stringstream out;
             out << i;
             cLine = out.str();
             
             getline(inFile, cLine);
         }
    After the getline, how do I make sense of what's actually in that line? I assume I can do this for each line:
    Code:
    string code1;
    string code2;
    string time1;
    string time2;
    
    inFile >> code1 >> code2 >> time1 >> time2;
    Each item in the file is seperated by a single space, so I assume that's alright. Am I correct? (edit: it compiles, but the program is crashing with "This application has requested the RunTime to terminate it in an unusual way" which might be caused by that line or some other part of the file reading)

    Also, how do I convert a string time1 that looks like "7:30" to int hour 7 and int min 30?
    Last edited by whiskedaway; 06-15-2009 at 01:16 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It may compile, but it probably doesn't do what you want it to. You're not reading from the file to your stringstream, but putting the number you want in the stringstream, and then reading from the file to cLine.

    And how do you convert a string time1 that looks like "7:30" to int hour 7 and int min 30? (Your program will do the same, I would guess.)

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Okay, so I have the wrong code then? I assumed that getline(inFile, cLine) would take line number cLine in file inFile and read that line. From what you've said, I guess that's not correct? How would I do that then?

    I haven't converted any strings to ints at all, all I've done is the reverse. To do that, I did this:
    Code:
             int hour;
             string hourNo;
             stringstream out;
             out << hour;
             hourNo = out.str();
    
             int min;
             string minNo;
             stringstream out;
             out << min;
             minNo = out.str();
    
             string time = hourNo + ":" + minNo;

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we assume that you can get a string into a stringstream, how would you go about doing the opposite of the operations you use to CREATE the string?

    By the way, how about:
    Code:
    stringstream out;
    out << hour << ":" << min;
    string time = out.str();
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    If you're reversing stringstream, would you just be reversing the arrows? So maybe:

    Code:
    stringstream out;
    string time = in.str();
    out >> hour >> ":" >> min;
    int hour;
    int min;
    I'm not sure I have it quite right. :/

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't use the ":" in reading it out, but you are on the right track. What would "match" a colon?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Would it be a "."? Or maybe a " "? Or maybe it's another variable to store the ":"? I'm not really sure.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whiskedaway View Post
    Would it be a "."? Or maybe a " "? Or maybe it's another variable to store the ":"? I'm not really sure.
    So try it out a bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Nothing I try will compile. First I was getting that hour and min were undeclared, and then when I declared them I was getting an ambiguous overload.

    I'm not sure this part is as important as the problem with getline(inFile, cLine), though. That's the part that's ruining the entire program right now, and I don't know what the problem is.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What leads you to believe there's something wrong with getline?

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Oh, that was what I was led to believe from above:

    It may compile, but it probably doesn't do what you want it to. You're not reading from the file to your stringstream, but putting the number you want in the stringstream, and then reading from the file to cLine.
    Okay, so I have the wrong code then? I assumed that getline(inFile, cLine) would take line number cLine in file inFile and read that line. From what you've said, I guess that's not correct? How would I do that then?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, C++ is not a write-only language. You should be able to read what you've written to see what it is going to do. For instance, seeing the code
    Code:
    for (int i=2; i < lineNo; i++) {
             string cLine;
             stringstream out;
             out << i;
             cLine = out.str();
             
             getline(inFile, cLine);
         }
    you should be able to tell that out << i puts i into out and that getline reads from inFile into cLine. (If you think that getline cares about the previous value of cLine, then that's just being silly.)

    Now, it looks like you've been programming by magic (i.e., typing commands/functions and hoping they do what you want). I would suggest looking up all those functions you're using, like getline, either in your reference book or online at say cplusplus.com, to see what they actually do, and then putting all your tools together. Some hints: once you've read that first number from the file, that number should go somewhere in your for loop. It is neither necessary, possible, nor desirable for you to read a line from a file by line number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

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