I have to read lines from an extern text file and need the 1. character of so开发者_开发技巧me lines.
Is there a function, which can tell me, in which line the pointer is and an other function, which can set the pointer to the begin of line x?
I have to jump to lines before and after the current position.
There is no such function i think. You will have to implement this functionality yourself using getline()
probably, or scan the file for endline characters (\n
) one character at a time and store just the one character after this one.
You may find a vector (vector<size_t>
probably) helpful to store the offsets of line starts, this way you might be able to jump in the file in a line-based way. But haven't tried this, so it may not work.
You may ake a look at ifstream
to read your file in a stream, then use getline()
to get each line in a std::string
.
Doing so, you can easily iterate trough the lines and grab the characters you need.
Here is an example (taken from here):
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl; // Here instead of displaying the string
// you probably want to get the first character, aka. line[0]
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
精彩评论