Thread: Error opening files in a different dir

  1. #1
    Registered User
    Join Date
    Sep 2008

    Error opening files in a different dir


    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):

    /* 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];
      return 0;
    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.

    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:
    if( !ifs.is_open() ) {
    cout<<"The file could not be opened"}
    This indeed returned "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!

  2. #2
    Registered User
    Join Date
    Sep 2008
    i think this will work
    filename=g_1+ "/" + filename;
     ifstream ifs (filename.c_str());

  3. #3
    Registered User
    Join Date
    Sep 2008
    Thanks for the input, however I managed to discover the problem.

    dir->d_name also returns "." and ".." as its first two statements. Of course, trying to read these "files" will give errors

    So I added:

    if( strcmp( dir->d_name, "." ) != 0 && strcmp( dir->d_name, ".." ) != 0 )
    Which solved the problem.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    You may want to look at a different approach, as there might be other directories

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Last one for a while ;0 - Index.dat files & internet cache
    By Robert_Sitter in forum C# Programming
    Replies: 1
    Last Post: 12-31-2005, 09:16 AM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Files not updating in folder
    By BobS0327 in forum Tech Board
    Replies: 4
    Last Post: 06-06-2005, 05:55 PM