skipping lines with an input text file

This is a discussion on skipping lines with an input text file within the C++ Programming forums, part of the General Programming Boards category; Hi, if i'm reading from a text file using <fstream>, is there a way to skip to a particular line ...

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    skipping lines with an input text file

    Hi, if i'm reading from a text file using <fstream>, is there a way to skip to a particular line in the text file (for reading, not writing)? What would be the easiest way to do this?

    So far, this is what I have coded to open/close the file.

    Code:
    void readTxtFile()
    {
    string temp;
    
    ifstream inData;
    inData.open("indata.txt");
    
    //not sure on the usage of the getline function
    inData.getline(temp, '\n');
    
    inData.close;
    }
    Last edited by kwikness; 12-10-2006 at 02:24 PM.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Read it all in and then parse the text (aka after four new lines ignore all text until the next newline).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Hmm.. when I try to use getline() this way, i'm getting a compiler error..

    .\MP3Tool.cpp(146) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *'

    I could use a char[] instead of a string, but with the file i'm reading, the line of text i'm going to read isn't always going to be the same size. How can I get around this?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    std::string.c_str().
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You need to use:

    std::getline(inData,line);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Quote Originally Posted by Cat
    You need to use:

    std::getline(inData,line);
    There is no variable 'line' declared..
    My problem is that when I call getline using a string as the 1st parameter, I get a compiler error. How can I store a line of text from a .txt file in a string?

    Please help!! I've got a term project due wednesday!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The first parameter is the filestream, the second parameter is your string variable. Just change line to temp. You should have a reference that tells you what the different standard functions and their parameters do.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Code:
    ifstream inData("indata.txt");
     
    const int line_length = 255; //declare size
    char line[line_length];  // store line
    
    
    inData.getline(line,line_length);  // Read line
    cout << line << endl;  // Output line
    inData.ignore (line_length,'\n'); //skip next line
    inData.getline(line,line_length);  // Read 3rd line
    cout << line << endl;  // Output 3rd line
    
    
        cout << "\n";
        cout << "press any key to exit \n";
        std::cin.get();
        inData.close();
        return 0;
        }
    *check topic 'reading second line' (he (or she) pretty much sumed it up)
    Last edited by Oldman47; 12-12-2006 at 09:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Counting the number of lines in a text file - help
    By Erkan in forum C Programming
    Replies: 11
    Last Post: 11-12-2005, 04:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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