1

I need a quick solution, for this simple task.

I am modifying a source code, which has this function:

OFCondition MdfDatasetManager::saveFile(const char *file_name,
                                        E_TransferSyntax opt_xfer,
                                        E_EncodingType opt_enctype,
                                        E_GrpLenEncoding opt_glenc,
                                        E_PaddingEncoding opt_padenc,
                                        OFCmdUnsignedInt opt_filepad,
                                        OFCmdUnsignedInt opt_itempad,
                                        OFBool opt_dataset)

Inside the function, there is a line:

    result = dfile->saveFile(file_name, opt_xfer, opt_enctype, opt_glenc,
                             opt_padenc,
                             OFstatic_cast(Uint32, opt_filepad),
                             OFstatic_cast(Uint32, opt_itempad),
                             (opt_dataset) ? EWM_dataset : EWM_fileformat);

See the file_name variable?

I want to modify, so the file_name will have ".out" added at the end.

So, I added a new variable:

char *output_file;

And before calling the dfile->saveFile() function, I added:

strcpy(output_file, file_name);
strcat(output_file, ".out");

    result = dfile->saveFile(output_file, opt_xfer, opt_enctype, opt_glenc,
                             opt_padenc,
                             OFstatic_cast(Uint32, opt_filepad),
                             OFstatic_cast(Uint32, opt_itempad),
                             (opt_dataset) ? EWM_dataset : EWM_fileformat);

But, that does not work.

The application crash. And I think, the issue is I need to initialize the *output_file ?

The thing is, I am not C++ programmer :(

So, could anyone help?

Thanks.

1
  • 1
    please don't write "I need a quick solution". The way to get quick solutions here on SA is to add a bounty to your question, apart from that, do not forget: You are not paying, we are not paid. Therefore, we define priority ourself. Commented Sep 25, 2012 at 9:12

7 Answers 7

5

No memory has been allocated for output_file. Use a std::string instead:

std::string s(file_name);
s += ".out";

Then use s.c_str() as the first argument o saveFile() function.

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

Comments

2

Lots of people suggesting using += to append ".out" to the string, which is correct and efficient, but not really intuitive. If you consider a more natural expression...

std::string x = file_name + ".out";

...you'll find it doesn't work because the compiler adds a pointer-to-char to a char array. Array's can't be added to anything, so the compiler runs through a list of standard conversion seeking something potentially meaningful. Sadly, the array can be converted to another pointer-to-char and a numeric sum of the two pointers is allowed (they're ultimately just numbers after all), but the result is an irrelevant address (which may even wrap due to the bit-size) which may not even correspond to your program's virtual address space and might as well be random. At the time of the + the compiler isn't thinking of the parameters in terms of std::strings: anything on the right of an assignment is evaluated before the left-hand-side destination is involved. So, the minimal intuitive change to get that working is to explicitly make one of the parameters to + be a std::string:

std::string x = std::string(file_name) + ".out";

...or...

std::string x = file_name + std::string(".out");

As an expression that adds lots of things is evaluated left to right, once you've got a std::string as the first or second parameter, you can add in other ASCIIZ/C/const char* "string"s until the cows come home.

Comments

1

Of course it does crash. output_file is a pointer to an array of chars. But you don't allocate that memory, nor initialize the pointer. Therefor it points to some random area on the heap. When you try to copy to it, the app crashes.

size_t total_size = strlen(file_name) + strlen(".out")+1;
char* output_file = new char[total_size];
memset(output_file, 0, total_size)
strcpy(output_file, file_name);
strcat(output_file, ".out");

Mind that you have to delete the allocated memory.

BTW, why don't you just use a std::string so you don't have to care?

std::string output_file(file_name);
output_file+= ".out";
result = dfile->saveFile(output_file.c_str(), opt_xfer, opt_enctype, opt_glenc,
                         opt_padenc,
                         OFstatic_cast(Uint32, opt_filepad),
                         OFstatic_cast(Uint32, opt_itempad),
                         (opt_dataset) ? EWM_dataset : EWM_fileformat);

Comments

0
#include <string>     // at the top of the program

std::string output_file = file_name;
output_file += ".out";
result = dfile->saveFile(output_file.c_str(), opt_xfer, opt_enctype, opt_glenc,
                         opt_padenc,
                         OFstatic_cast(Uint32, opt_filepad),
                         OFstatic_cast(Uint32, opt_itempad),
                         (opt_dataset) ? EWM_dataset : EWM_fileformat);

1 Comment

deleted ... I compiled successfully :)
0

Use std::string and make your life easier:

std::string outputFile(output_file);
outputFile += ".out";

result = dfile->saveFile(outputFile.c_str(), ... );

Comments

0

An all C approach:

output_file = malloc(strlen(output_file) + 5);
strcpy(output_file, file_name);
strcat(output_file, ".out");

P.S. You'll need to free(output_file) after you're done w/ output_file.

Comments

0

You must allocate memory for the new string, for example:

char *cNewString = malloc(strlen(file_name)+5); //+5 for the ".out"

and then copy it over.

Don't forget to delete the memory when no longer used

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.