1

I want to access specific lines in of text file to display or modify that one. My text file contains lines of different lengths and i heard cannot use seekg or seekp for such files. I got a code but it doesn't works. Can someone please tell me its fault of some other helpful idea?

#include <fstream>
#include <limits>
#include <string>
#include <iostream>

std::fstream& GotoLine(std::fstream& file, unsigned int num)
{
    file.seekg(std::ios::beg);
    for(unsigned int i=0; i < num - 1; ++i)
    {
        file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    return file;
}

int main()
{
    using namespace std;

    std::fstream& GotoLine(std::fstream& file, unsigned int num);
    cout << "Starting..." << endl;
    fstream file("bla.txt");
    GotoLine(file, 8);
    string line8;
    file >> line8;
    cout << "[" << line8 << "]" << endl;
    // cin.get();
    cout << "Finished..." << endl;
    return 0;
}    
Hassaan Salik
  • 413
  • 4
  • 22
  • 1
    Helpful idea 1: step through the code with debugger... Think what next line should do, then hit "step" in debugger, then see what it actually did. – hyde Nov 22 '13 at 11:14
  • 1
    Helpful idea 2: when asking for help from somebody else, "it doesn't works" is not very descriptive. You should tell what the code does/how it behaves, and how it is different from what it should do/how it should behave. – hyde Nov 22 '13 at 11:16
  • 1
    Helpful idea 3: when using any system or library functions, always check for errors, and (for example) print an error message if error happens (see http://stackoverflow.com/questions/839644/get-stdfstream-failure-error-messages-and-or-exceptions). – hyde Nov 22 '13 at 11:19

2 Answers2

1

Standard C++11 (and earlier versions of the standard) dont have notion of lines contained inside files. Also POSIX (and even Windows or MacOSX) don't have it. Textual files usually contain lines of variable length (and only the line terminator is relevant, either \n or \r\n or \n\r, depending upon the operating system and perhaps the file read mode -binary or textual-).

In the 1960s or 1970s IBM mainframe OS/360 operating systems had "file" systems which did have files made of fixed-length lines, mimicking punched cards.

So, you have to read your file line by line and remember where are the line limits (or use std::istream::ignore to skip till \n). Alternatively, read every line of your file into a std::vector<std::string> using std::getline on std::ifstream-s.

For /etc/fstab (or /proc/mounts) reading all the lines in a vector is a good idea, since it is always a tiny file. It usually have less than an few dozens of lines, often less than about a hundred char each. The pathological case could be a file with many thousand lines of comments, but that don't really happen in practice.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1
string line8;
file >> line8;

will only extract until the first whitespace character is hit.

you could use something like:

string line8;
getline(file, line8);

This at least worked for me with the rest of your code on Windows 7 with VS2012

randooom
  • 541
  • 2
  • 11