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...
Related
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 am trying to use getline on a file (unkown size) to grap the first line, input it into a string, manipulate this string (replace words with others, move some around) and output the manipulated line back to the file.
After this, I need to do the same thing to line 2, 3, etc. until the end of the file. How would I go about doing this? I figured a while loop for getline would work, but not sure how to get the conditions for the while loop or how to manipulate each line individually. Such as lines 1 and 3 must be manipulated differently than lines 2 and 4. etc.
A rough idea of what I'm trying to do:
void readFile(string filename, string text)
{
ifstream infile;
infile.open(filename);
getline(cin, text) // pretty sure this is wrong..
infile.close(); // close here, or after manipulation???
}
void swapText(string filename, string text)
{
string decrypText;
//Manupulate several things..
return decrypText;
}
void writeToFile(string filename, string decrypText)
{
ofstream outfile;
outfile.open(filename);
outfile << decrypText << endl;
outfile.close();
}
The standard idiom for reading text lines from a file and storing them is:
std::vector<std::string> file_data;
std::string text_line;
while (std::getline(my_data_file, text_line))
{
// Optional: store the text line
file_data.push_back(text_line);
// Call a function to process (or ignore) the text line:
Process_Text_Line(text_line);
}
If you want to have a function that reads the file, you may need to pass the vector:
void Read_File(std::vector<std::string>& file_data)
{
//...
// Read the data, see "while" loop above.
}
Don't open and close the file for every read. Keep it open and read a line at a time:
std::istream in("filein.txt");
std::ostream out("fileout.txt");
std::string line;
while (std::getline(in, line)) {
// modify line as appropriate
out << line << '\n';
}
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 have a csv that I'd like to tokenize line by line with StringStream. The key is that I know apriori what the columns would look like. For example, say I know the file looks like the following
StrHeader,IntHeader
abc,123
xyz,456
I know ahead of time it is a string column, followed by an int column.
Common approach is to read the file line by line
std::string line;
stringstream lineStream;
while (getline(infile, line)) // read line by line
{
cout << "line " << line << endl;
lineStream << line;
string token;
while(getline(lineStream, token, ',')) // push into vector? this is not ideal
{
}
I know I can have 2 loops, and have inner loop tokenizes the string based on commas. Lots of sample code on stackoverflow would store the result into a vector<string>.
I don't want to do create a new vector every line. Since I know apriori what columns the file would have, can I somehow read directly into a string and int variable? Like this
std::string line;
stringstream lineStream;
while (getline(infile, line)) // read line by line
{
cout << "line " << line << endl;
lineStream << line; // DOESNT WORK - tell lineStream we have comma delimited string
string strValue;
int intValue;
lineStream >> strValue >> intValue; // SO MUCH CLEANER
// call foo(strValue, intValue);
}
The problem above is this line
lineStream << line; // DOESNT WORK - tell lineStream we have comma delimited string
From what I could tell, the above code works if the input line is space delimited, not comma delimited.
I have no control over the input. So, simply replacing the "spaces" with "commas" in the original string is not an ideal solution since I don't know if the input already has spaces.
Any ideas? thanks
You could try to only read to the delimiter with std::getline() and then put that in a string stream for conversion.
while (!infile.eof()){
std::getline(infile, strValue, ',');
std::getline(infile, line);
strstr.str(line);
strstr.clear();
int intValue;
strstr >> intValue;
foo(strValue, intValue);
}
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
}
}
}