Hello,
First of all, I have tried using the search function and browsing the tutorials to solve my problem, but have come up empty-handed so far...
I have a program that basically opens files in a child dir of the current dir, pulls certain data from them and outputs that data in a range of different output files. These output files have the name of the original input file with a certain extension.
Problem is that my output files come up empty. After a test I ran it seems the input file cannot be opened for some reason.
Here is my code (shortened for easier read):
What this does (or is supposed to do) is open the dir "Test", which is a child dir of the dir the program is in, opens the files in it and for each of these files with name XXX output "XXX_magblauw" and "XXX_magrood", which contain certain elements of the original XXX file.Code:/* Program to retrieve magnitudes from Astro-WISE tables */ #include <iostream> #include <iomanip> #include <math.h> #include <string> #include <fstream> #include <dirent.h> using namespace std; int main(){ DIR *d; struct dirent *dir; string g_1 = "Test"; d = opendir ( g_1.c_str() ); if (d) { while((dir=readdir(d)) != NULL) { string g = dir->d_name; string sl = "/"; string sldot = "../"; string filename = sldot + g_1 + sl + g; ifstream ifs(filename.c_str()); //adding name extensions for output files string mb = "_magblauw"; string mr = "_magrood"; //merging the original filename with the extensions string pathmb = g + mb; string pathmr = g + mr; //file output ofstream ofs(pathmb.c_str()); ofstream ofs2(pathmr.c_str()); //selecting data from input file and writing to different output files int i=0; string Value; char M[25]; while(ifs>>Value){ i=i%24; if(i==3){ ifs>>M; ofs<<M<<endl; i++; } if(i==9){ ifs>>M; ofs2<<M<<endl; i++; } i++; } } closedir(d); } return 0; }
This program works perfectly when I omit the part about opening a dir with files and simply run it on a single file (like ifstream ifs("XXX") and ofstream ofs("XXX_magblauw") etc.).
I have tried adding:
This indeed returned "file could not be opened".Code:if( !ifs.is_open() ) { cout<<"The file could not be opened"}
So why isn't the file opening? What am I doing wrong?
Any help is greatly appreciated! Thanks in advance!



LinkBack URL
About LinkBacks



