How do you read a specific line from a text file

I need to read line 180 of a file...

I'm having trouble understanding the model from the tutorial...Here is my code, basic file in/out...How do I read a specific line from a text file?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
string line;
	ifstream myfile ("c:\output.txt");
	getline( myfile, line ); //Can I direct the compiler to read a certain line (ie. line 180)?

	myfile.close();
return 0;
}
Last edited on
Unless you know a priori where exactly in the file the line is, you must read and discard the first 179 lines.

Here's something fun to play with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;

//----------------------------------------------------------------------------
int usage( const char* progname, int exitcode )
  {
  string name( progname );
  name.erase( 0, name.find_last_of( "\\/:" ) + 1 );
  if (name.find( ".exe" ) == (name.length() - 4))
    name.erase( name.length() - 4 );

  ((exitcode == 0)
    ? cout
    : cerr)
  << "usage:\n  " << name << " LINE_NUMBER [FILENAME ...]\n\n"
     "Prints the LINE_NUMBER-th line of each given FILENAME.\n"
     "If no files are given, reads from the standard input.\n"
     "If LINE_NUMBER is too big for a file, an empty line is printed.\n";

  return exitcode;
  }

//----------------------------------------------------------------------------
void print_line( istream& f, size_t line_number )
  {
  while (--line_number)
    f.ignore( numeric_limits <streamsize> ::max(), '\n' );

  string s;
  getline( f, s );
  cout << s << endl;
  }

//----------------------------------------------------------------------------
int main( int argc, char** argv )
  {
  if (argc < 2)
    return usage( argv[ 0 ], 0 );

  size_t line_number;
  {
    istringstream ss( argv[ 1 ] );
    ss >> line_number;
    if (!ss.eof())
      return usage( argv[ 0 ], 1 );
  }

  if (argc == 2)
    print_line( cin, line_number );
  else
    for (int n = 2; n < argc; n++)
      {
      ifstream f( argv[ n ] );
      if (!f)
        cerr << "Could not open file \"" << argv[ n ] << "\".\n";
      else
        print_line( f, line_number );
      }

  return 0;
  }

:-)
thank duoas...but is there no way to scroll to a certain line?

I'm taking a txt file from the internet that contains statistics for 32 teams (NFL)...the file is constant...the only differences are the numbers within the lines I need to read. So, my thought was to scroll to certain lines and use delimiters to extract the information. It sounds though like my method will be more complicated than just scrolling to line 180...is that correct?
Like Duoas said, you can only skip directly to a line when you can make some assumptions about the file, like "all lines have 80 characters".
"Files are just\narrays of bytes without inherent\nstructure. If a line can be of any length,\nyou can only count newline\ncharacters to know which line you're on.\n"
if I know the size of each line, how would I skip to line 180 if all lines have 80 characters?
can you search a file for certain characters, strings, etc..?

also, the file is from a webpage(html)...most of the lines are like the following, which throw errors while declaring the line as a string:

string line2 = ("<td style="text-align:right;">364.0 <small>(#15)</small></td>");

the compiler says: missing identifier ')' before text...any help or locations of relevant information would be great...
Last edited on
If you have syntax highlighting, you shoudl be able to see why that's giving you an error:

string line2 = ("<td style="text-align:right;">364.0 <small>(#15)</small></td>");

notice how the string ends before text-align because you have a closing quote.

To put a quote character in a string literal, you need to escape it. That prevents that from happening.

string line2 = ("<td style=\"text-align:right;\">364.0 <small>(#15)</small></td>");

EDIT: also, I doubt every line has exactly 80 characters, so that's probably not a good way to approach the problem.

I would just scan the file for newline characters ("\n"). Once you found the 180th newline character you're on the 180th line. So just keep reading characters from the file and ignore them until you get to the 180th newline.
Last edited on
how would I skip to line 180 if all lines have 80 characters?
Then you could just skip to byte 179*81. Sort of.

"This string is has \"quotes\" in it."
ok thanks all...i know not every line has 80 characters...just theory i'm interested in...i'm trying to find ways to search through the text file...I will go line by line searching for delimiting characters to extract the info...

EDIT: just read your edit disch...that method makes sense...I will try it, thanks!
Last edited on
I used a similar method to that of Disch's...added a count to the while() statement so that when count = 180, print the line. works ok...

1
2
3
4
5
6
7
8
9
10
11
12
string line;
int count=0;
ifstream myfile ("c:/output.txt");
	
while(myfile.good()) {
getline(myfile, line);
count++;
if(count==180) {
cout << line << endl;
... //here I can use info from "line"
}
}
Last edited on
Topic archived. No new replies allowed.