ifstream fileList("/home/viraj/Document/ML/TEST/filepos.txt"); //List containing the file paths
std::vector<ifstream>positiveTextFiles;
string textFileName;
while (getline(fileList,textFileName )) {
if(!std::filesystem::exists(textFileName)){
cout<< "`\('')/` : "<<textFileName<<endl;
exit(0);
}
cout<<textFileName<<endl; //Prints well
//method-1
ifstream x(textFileName.c_str());
string s;
x >> s;
cout<<"--> "+s<<endl;
//positiveTextFiles.push_back(x); //gives error of --> use of deleted function
//method-2
positiveTextFiles.push_back(ifstream(textFileName)); // Doesn't work
}
I am not getting desired output in both the methods.
The contents of files are corectly written in the "textFileName" variable, but when output its not correct.
positiveTextFiles.push_back(x); //gives error of --> use of deleted function
std::ifstream objects cannot be copied, but they can be moved. The following code should work:
positiveTextFiles.push_back(std::move(x));
Typehello wrote:
1 2
//method-2
positiveTextFiles.push_back(ifstream(textFileName)); // Doesn't work
This should compile. If it doesn't, what error message do you get?
Typehello wrote:
The contents of files are corectly written in the "textFileName" variable, but when output its not correct.
What is incorrect about the output? You mean the � in front of PNG? It's because the first byte in a PNG file does not represent a printable character.
This will create the required vector, display the file names as the vector is created and then iterate the vector to display the first text line from each of the files.