std::string to std::ifstream

I want std::string to be converted to std::ifstream

Here is the code written, but unfortunately not working :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    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.


/home/viraj/Document/ML/TEST/posi/ldimage_49_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_21_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_10_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_22_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_23_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_7_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_0_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_36_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_52_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_15_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_5_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_31_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_39_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_34_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_54_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_45_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_5_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_47_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_47_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_5_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_21_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_39_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_48_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_7_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_29_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_34_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_46_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_29_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_22_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_28_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_50_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_0_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_44_3_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_14_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_12_1_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_38_0_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_20_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_29_2_p.png
--> �PNG
/home/viraj/Document/ML/TEST/posi/ldimage_48_3_p.png
--> �PNG
Last edited on
Typehello wrote:
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.

https://www.w3.org/TR/PNG-Rationale.html#R.PNG-file-signature
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <filesystem>

int main() {
	std::ifstream fileList { "mylist.txt" };

	if (!fileList)
		return (std::cout << "Cannot open list file\n"), 1;

	std::vector<std::ifstream> positiveTextFiles;

	for (std::string textFileName; getline(fileList, textFileName); ) {
		if (!std::filesystem::exists(textFileName)) {
			std::cout << "`\('')/` : " << textFileName << '\n';
			exit(2);
		}

		std::cout << textFileName << '\n';
		positiveTextFiles.push_back(std::ifstream { textFileName });
	}

	for (std::string s; auto& f : positiveTextFiles) {
		f >> s;
		std::cout << "--> " + s << '\n';
	}
}

Last edited on
Topic archived. No new replies allowed.