im trying to make a simple program that list all txt file in the directory then append hello world in them but i face an issue while passing the vector into WriteFiles Function
this is the following code i've tried to fix it for a while any oil be grateful for any help
#define _CRT_SECURE_NO_WARNINGS #include <string> #include <vector> #include <iostream> #include <windows.h> #include <fstream> using namespace std; void ListFiles(vector<string>& f) // list all files { FILE* pipe = NULL; string pCmd = "dir /b /s *.txt "; char buf[256]; if (NULL == (pipe = _popen(pCmd.c_str(), "rt"))) { return; } while (!feof(pipe)) { if (fgets(buf, 256, pipe) != NULL) { f.push_back(string(buf)); } } _pclose(pipe); } void WriteFiles (const char* file_name) { std::ofstream file; file.open(file_name, std::ios_base::app); // append instead of overwrite file << "Hello world"; file.close(); } int main() { vector<string> files; ListFiles(files); vector<string>::const_iterator it = files.begin(); while (it != files.end()) { WriteFiles(*it); // the issue is here cout << "txt found :" << *it << endl; it++; } }
FILE* pipe = _popen(pCmd.c_str(), "rt"); if (!pipe) return;.std::ofstream file; file.open(file_name, std::ios_base::app);tostd::ofstream file(file_name, std::ios_base::app);. Also, you don't have to callfile.close(). The destructor will do that.dir.