0

I've written this code, which it get the repository and look for the files within. it aims to create binary files for each file found so as to write some data inside it later. However, the code is not running as expected. and the binary file are not created this the issue.

the directory has two images, and the output I get is as follows :

Creating bin files
C:\repo\1.bin

Error: failed to create file
Press <RETURN> to close this window... 

I really do not know where I miss it. Any advice I'd be glad.

#include <vector>
#include <string>
#include <iostream> // for standard I/O
#include <string>   // for strings
#include <iomanip>  // for controlling float print precision
#include <sstream>  // string to number conversion
#include <fstream>

using namespace std;

void getDir(string d, vector<string> & f)
{
    FILE* pipe =  NULL;
    string pCmd = "dir /B /S " + string(d);
    char buf[256];

    if( NULL == (pipe = _popen(pCmd.c_str(),"rt")))
    {
        cout<<"Error"<<endl;
        return;
    }

    while (!feof(pipe))
    {
        if(fgets(buf,256,pipe) != NULL)
        {
            f.push_back(string(buf));
        }

    }

    _pclose(pipe);
}

void replaceExt(string& s, const string& newExt) {

   string::size_type i = s.rfind('.', s.length());

   if (i != string::npos) {
      s.replace(i+1, newExt.length(), newExt);
   }
}

using namespace std;

int main(int argc, char* argv[])
{
    vector<string> files;
    string path = "C:\\repo";
    getDir(path, files);
    vector<string>::const_iterator it = files.begin();
    cout<<"Creating bin files "<<endl;

    ofstream myOfstream;

    while( it != files.end())
    {
        string fileName = (string) *it;
        replaceExt(fileName, "bin");
        cout << fileName << '\n';

        std::stringstream ss;
        ss << fileName << "" ;
        myOfstream.open(ss.str(),  fstream::binary);
        if ( !myOfstream )
        {
            std::cerr << "Error: failed to create file " << '\n';
            break;
        }

        myOfstream.close();

        it++;
    }

    return 0;
}
9
  • What is the expected output and what do you get as output? Commented Jun 27, 2018 at 14:27
  • when I gave the code a path to a directory which contains images png I will apply some filters on these images then I want to save the resultats in binary file Commented Jun 27, 2018 at 14:34
  • there is not solution to this issue ? Commented Jun 27, 2018 at 15:29
  • 1
    @cht_usr - easy tiger. One hour is not a long response time. ;) Commented Jun 27, 2018 at 15:34
  • 1
    You have several places where output is written to the screen (or to whatever is hooked up to cout and cerr). What output did you get? (Use a small example, say a directory with two files in it.) Commented Jun 28, 2018 at 1:05

2 Answers 2

2

First I have to say, if you directory you are looking for doesn't exists or is empty, the program gets locked, it would be nice to have that fixed if making a bigger program.

Then, for your case, I don't see whars the point of that stringstream, so I tried removing that, and changing it by a normal string, removing the last \n character you get from reading the filenames:

        cout << fileName << '\n';

        string ss = fileName.substr(0, fileName.size() - 1);
        myOfstream.open(ss.c_str(), fstream::binary);
        if (!myOfstream)
        {

hope it helps

Sign up to request clarification or add additional context in comments.

Comments

1

I found the issue bro, after debugging ;D

the problem is in the "newline", the string fileName has a "\n" at the end that's whats rise your error. Thus you have to erase it, I ve used this statement fileName.erase(std::remove(fileName.begin(), fileName.end(), '\n'), fileName.end()); and I included algorithm lib. the working code is as follows :

#include <vector>
#include <string>
#include <iostream> // for standard I/O
#include <string>   // for strings
#include <iomanip>  // for controlling float print precision
#include <sstream>  // string to number conversion
#include <fstream>
#include <algorithm>

using namespace std;

void getDir(string d, vector<string> & f)
{
    FILE* pipe =  NULL;
    string pCmd = "dir /B /S " + string(d);
    char buf[256];

    if( NULL == (pipe = _popen(pCmd.c_str(),"rt")))
    {
        cout<<"Error"<<endl;
        return;
    }

    while (!feof(pipe))
    {
        if(fgets(buf,256,pipe) != NULL)
        {
            f.push_back(string(buf));
        }

    }

    _pclose(pipe);
}

void replaceExt(string& s, const string& newExt) {

   string::size_type i = s.rfind('.', s.length());

   if (i != string::npos) {
      s.replace(i+1, newExt.length(), newExt);
   }
}

using namespace std;

int main(int argc, char* argv[])
{
    vector<string> files;
    string path = "C:\\repo";
    getDir(path, files);
    vector<string>::const_iterator it = files.begin();
    cout<<"Creating bin files "<<endl;

    ofstream myOfstream;

    while( it != files.end())
    {
        string fileName = (string) *it;
        replaceExt(fileName, "bin");
        cout << fileName << '\n';
        fileName.erase(std::remove(fileName.begin(), fileName.end(), '\n'), fileName.end());
        std::stringstream ss;
        ss << fileName << "" ;
        myOfstream.open(ss.str(),  fstream::binary);
        if ( !myOfstream )
        {
            std::cerr << "Error: failed to create file " << '\n';
            break;
        }

        myOfstream.close();

        it++;
    }

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.