Hello everybody. I am seeking help on how to save 3 output images into a specific path after filter by 3 different mask size e.g. 3x3, 5x5 and 7x7 mask. My program is in C:\Cpp\Project\, I wish to save those output images inside "filter" folder with path in C:\Cpp\Project\filter. Thus, the "filter" folder will have output images with name - "outputBOHE 3 x 3.pgm", "outputBOHE 5 x 5.pgm", and "outputBOHE 7 x 7.pgm". However, when I run my program, those output images are save at C:\Cpp\Project. Although I had created the "filter" folder, the output images cannot be save in C:\Cpp\Project\filter. Please advise me on how to solve this problem. Thanks in advance.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

string NumberToString(int t)
    {
       ostringstream os;
       os << t;
       return os.str();
    }


int main()
{
   int cr_w, cr_h; 
   int ind=0;
   image= cv::imread("BOHE.pgm", 0);  //openCV format for reading an image

   nl = image.rows;  //openCV format to obtain image rows
   nc = image.cols;  //openCV format to obtain image columns

    for (int i = 0; i < 3; i++)
    {
     cr_w = 3 + 2*(i%3); //mask width
     cr_h = 3 + 2*(i%3); //mask height

     //create 3 output images with name of "outputBOHE 3 x 3.pgm", "outputBOHE 5 x 5.pgm", and "outputBOHE 7 x 7.pgm" after filtering with 3x3, 5x5, and 7x7 mask
       std::string name = "outputBOHE " + NumberToString(cr_w) +" x "+   NumberToString(cr_h)+".pgm";

     //save the 3 output images into the following path
       ofstream fout ("C:\Cpp\Project\filter");


     fout.open (name.c_str());

     fout <<"P2\n"<<nc<<'\t'<<nl<<endl<<cr_w<<'\t'<<cr_h<<endl<<255<<endl;  //P2 is for image with pgm extension

     for (int y=0; y<nl; y++)
     {
        for (int x=0; x<nc; x++) 
        {
          fout << int(image.at<uchar>(y,x))<<' ';
        }
     }


     fout.close();
   }

return 0;
}

Dani AI

Generated

Two independent issues explain the observed behavior.

First, the code opens the stream with a directory (or the wrong string) and then calls open with only the bare filename. That sequence simply writes the file into the program’s current working directory (hence the files ending up in C:\Cpp\Project). was right that the path given to the ofstream must be the full filename (directory + filename), not the directory alone.

Second, the literal with single backslashes is being misinterpreted by the compiler: backslash is an escape character (\f, \t, \", etc.) and a trailing \ can break the literal. That explains why the “full path” form failed while the relative "filter\..." form worked (the relative string was interpreted differently or already matched the working directory).

Practical, robust fixes:

  • Build a full filename (directory + filename) and pass that to ofstream exactly once. Do not construct the stream with the folder and later call open(name.c_str()).
  • Avoid manual escaping mistakes: either use forward slashes ("C:/Cpp/Project/filter/"), a raw string literal (R"(C:\Cpp\Project\filter)"), or prefer std::filesystem (C++17+) to compose paths safely.
  • Always check the stream after opening and ensure the directory exists (create it if necessary) and that permissions allow writing.

Minimal example using std::filesystem (C++17+):

#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;

fs::path outdir = R"(C:\Cpp\Project\filter)";   // raw string literal
fs::create_directories(outdir);                 // ensure folder exists

fs::path full = outdir / ("outputBOHE " + std::to_string(cr_w) + " x " + std::to_string(cr_h) + ".pgm");
std::ofstream fout(full.string());
if (!fout) std::cerr << "Failed to open " << full << '\n';

Extra debugging tips: print the constructed path before opening, test with forward slashes, and verify the program’s working directory or IDE run settings (different IDEs set different working directories).

Recommended Answers

All 3 Replies

I think you're confused about the string passed to ofstream for opening a file. It's the file path, not the folder. So for each file, you need to make sure that the folder is included in the path. In other words:

std::string name = "outputBOHE " + NumberToString(cr_w) +" x "+   NumberToString(cr_h)+".pgm";
std::string path = "C:\\Cpp\\Project\\filter\\" + name;

ofstream fout (path.c_str());

fout <<"P2\n"<<nc<<'\t'<<nl<<endl<<cr_w<<'\t'<<cr_h<<endl<<255<<endl;

...

However, when i tried to use the full path (std::string path = "C:\Cpp\Project\filter\" + name;), there was no output image in the "filter" folder.

When I edited the above code to (std::string path = "filter\" + name;), there were output images in the "filter" folder.

Why does this happen? Is the full path redundant since the "filter" folder located in the same path as my program?

How about when I want to save those output images in other path, for example in "D:\output\"? In this case, when i specify the full path ("D:\output\"), there were no images save in the "output" folder. My program still in "C:\Cpp\Project\"

Please advise. Thanks.

Please read the code I posted very carefully. A single backslash and two backslashes in a string literal are very different things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.