How do I ignore an empty first line in "input.txt"? I don't necessarily know that there is an empty line (in this particular case there is, but I want to make my code generic), so I need to be able to read the line if there is information, or skip it if it is blank. This is just for the first line.
while (getline(mcFile, line)) {
istringstream liness2(line); ... }
That's how I'm reading the lines. If I knew for certain that any input file I ran this on had an empty first line, I would just do "getline" before, but I don't know that.
string data;
while (getline(inputFile, data))
{
if (data == "") continue; // Skip blank line
... // Do stuff with non-blank line
}
ifstream ReadFile;
ReadFile.open("input.txt");
string content;
string line;
if (myReadFile.is_open()) {
while (!ReadFile.eof()) {
getline(cin,line);
content += line + '\n';
if (!line.empty()) {
/// do what you want to do
}
}
}
Related
I am trying to read through a text file line by line and then I am trying to read that line word by word. Once I found a particular word, skip the remaining word from that line and jump to next line.
I tried this way:
void SymbolScanning::symScanning()
{
std::string s;
std::ifstream myfile;
myfile.open("SymbolRead.txt");
if(myfile.is_open())
{
while(std::getline(myfile, s))
{
std::istringstream iss(s);
std::string word;
while(iss >> word)
{
std::cout << word << std::endl;
// if desired word found, skip remainnig word and jump
// over next line.
}
}
}
else
cout<<"File is not open";
}
Output : File is not open.
I want to read a file with std::getline. but reads first line only
string FileReader::readLine() {
string line;
string read;
ifstream ReadFile;
ReadFile.open("input.txt");
if (ReadFile.is_open()) {
getline(ReadFile, line);
//ReadFile.close();
}
return line;
}
this is my method. I call this method several time but always reads first line how can i do to read next lines?
You need to change your program flow.
Don't return a string. Use the line within the loop to do whatever it is you want. Ensuring that you either don't leave the method or return to it.
You can't keep coming back to a function like this, as it will keep reading from the beginning.
void FileReader::readLine() {
string line;
string read;
ifstream ReadFile;
ReadFile.open("input.txt");
if (ReadFile.is_open()) {
while(getline(ReadFile, line))
{
//do what you want with that line, but return program flow here.
}
ReadFile.close();
}
}
I'm trying to to read ~36KB and it would take ~20 seconds to finish this loop:
ifstream input_file;
input_file.open("text.txt");
if( !(input_file.is_open()) )
{
cout<<"File not found";
exit(1);
}
std::string line;
stringstream line_stream; //to use << operator to get words from lines
int lineNum=1;
while( getline(input_file,line) ) //Read file line by line until file ends
{
line_stream.clear(); //clear stream
line_stream << line; //read line
while(line_stream >> word) //Read the line word by word until the line ends
{
//insert word into a linked list...
}
lineNum++;
}
input_file.close();
Any help would be appreciated.
stringstream::clear() does not clear all context inside it. It only resets the error and EOF flags, see http://en.cppreference.com/w/cpp/io/basic_ios/clear.
The result is your line_stream accumulates all previous lines and the inner loop will run words over all the accumulated lines again and again.
So the total time you spend is about O(n^2) compared to O(n) of what you expect it to be.
Instead of using the same object across each line, you could define the new line_stream instance inside the while loop to have a brand new and also empty one. Like this:
fstream input_file;
input_file.open("text.txt");
if( !(input_file.is_open()) )
{
cout<<"File not found";
exit(1);
}
std::string line;
int lineNum=1;
while( getline(input_file,line) ) //Read file line by line until file ends
{
stringstream line_stream; // new instance, empty line.
line_stream << line; //read line
while(line_stream >> word) //Read the line word by word until the line ends
{
//insert word into a linked list...
}
lineNum++;
}
input_file.close();
You could attempt the following:
std::ifstream file("text.txt");
std::string str;
while (std::getline(file, str))
{
cout << str; //call function to to retrieve words of str in memory not in file
}
I ran your code in 11ms, but with the mentioned option in 8ms. May be it works for you.
Try compiling with build flag -O2 or -O3.
I was surprised to see that a simple for-loop to read a 1GB file took 4.7 seconds, whereas another higher level language (Dart) did it in 3.x seconds.
After enabling this flag, runtime dropped to 2.1 seconds.
I would like some help with getting the first line from a txt file called "test.txt", I have discovered the getline function however I am not sure why my code doesn't work or what I need to do. I would like to get the first line from the .txt file, but it prints "t" for some reason. Feel free to correct me as you please if I am not handling it correctly. This is the code I am using:
string FirstLine;
ifstream File("test.txt");
string line;
if (File)
{
while (getline(File, line))
{
FirstLine = line[0];
}
File.close();
}
cout << FirstLine;
And this is the .txt file:
this is line 1
this is line 2
this is line 3
If you just want the first line:
string line;
getline(File, line);
Your first line of the file is then stored in line as a, you guessed it, string
To get all lines (line by line):
while(getline(File, line).good())
//do something with line
string FirstLine;
ifstream File("test.txt");
string line;
if (File)
{
getline(File, line);
FirstLine = line;
File.close();
}
cout << FirstLine;
Is the absolute minimum changes you need to your code to make it do what you want to do. However, there is A LOT of room for improvement on the above code sample. For example, why create two strings, line, and FirstLine, just pass FirstLine to the getline() function. I just modified what you provided to highlight where the mistakes where. Hope this helps...
I'm trying to read a file word by word and do some implementation on each word. In future I want to know where was the position of each word. Position is line number and character position in that line. If character position is not available I only need to know when I'm reading a file when I go to the next line. This is the sample code I have now:
string tmp;
while(fin>>tmp){
mylist.push_back(tmp);
}
I need to know when fin is going to next line?!
"I need to know when fin is going to next line"
This is not possible with stream's operator >>. You can read the input line by line and process each line separately using temporary istringstream object:
std::string line, word;
while (std::getline(fin, line)) {
// skip empty lines:
if (line.empty()) continue;
std::istringstream lineStream(line);
for (int wordPos = 0; lineStream >> word; wordPos++) {
...
mylist.push_back(word);
}
}
just don't forget to #include <sstream>
One simple way to solve this problem would be using std::getline, run your own counter, and split line's content into words using an additional string stream, like this:
string line;
int line_number = 0;
for (;;) {
if (!getline(fin, line)) {
break;
}
istringstream iss(line);
string tmp;
while (iss >> tmp) {
mylist.push_back(tmp);
}
line_number++;
}