Im trying to read a text file that has strings separated by tab characters, each of these will be assigned to a variable. Then the process repeats for every line until there are no lines remaining.
Currently, I have set up a nested loop to read each string in. It loops the correct number of times (if 3 lines in file, it loops 3x), but the information that gets assigned each of these times is from the first line only. What am I doing wrong? Thank you
for example - the text file might say:
test1 test2 test3 test4
test5 test6 test7 test8
test9 test10 test11 test12
but my output is:
test1 test2 test3 test4
test1 test2 test3 test4
test1 test2 test3 test4
update: Upon further inspection, it looks like this is just pushing new items onto the vector - so the data from subsequent lines are not assigned correctly. But when I try to do tokens.pop_back() four times so that the vector is reset, it crashes the program for being out of range. It appears that the push_back() is only being done for one line.
First the input file was separated by spaces not tabs. So the std::getline(ss, temp, '\t') had nothing to find except when it reached the ens of the string stream.
Second I have most often seed the string stream defined inside the while loop. When I did this it worked.
Third you should check to see if the input opened correctly as you can see in the code below.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <chrono>
#include <thread>
int main()
{
//std::string item1, item2, item3, item4;
std::ifstream inFile;
std::string line, temp;
std::vector<std::string> tokens;
inFile.open("textfile.txt");
std::string inFileName("textfile.txt");
if (!inFile)
{
std::cout << "\n File " << inFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5)); // <--- Needs header files chrono" and "thread".
return 1;
}
while (getline(inFile, line))
{
std::istringstream ss(line);
//ss << line;
while (std::getline(ss, temp, '\t'))
tokens.push_back(temp);
}
for (auto it : tokens)
std::cout << it << std::endl;
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}