cht_usr
cht_usr

Reputation: 129

issue while creating binary files

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;
}

Upvotes: 0

Views: 111

Answers (2)

azdoud
azdoud

Reputation: 1229

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;
}

Upvotes: 1

petacreepers23
petacreepers23

Reputation: 184

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

Upvotes: 2

Related Questions