Hi guys, I have been having troubles with file processing. I want to open a new file with ofstream, but it fails every time.. Can someone help me please?
Hi guys, I have been having troubles with file processing. I want to open a new file with ofstream, but it fails every time.. Can someone help me please?
Post some code that shows the problem.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Most probably that file is not found.
i have got
and i try to open a new file with it. Did I did it the right way?Code:void savefunc(char* file) { char filename[80]; std::strcat(filename, "C:\\Documents and Settings\\_lazycat_\\All Users\\"); std::strcat(filename, file); std::strcat(filename, ".txt"); std::ofstream record; record.open(filename,std::ofstream::out); if (!record) { std::cerr << "Unable to open file \'file.txt\'"; savefunc(file); } record << "THE ORIGINAL MESSAGE IS: " << PlainText << std::endl; record << "THE SQUARE CIPHER KEY IS: " << Key << std::endl; record << "THE PUBLIC KEY OF ER IS: " << n << "," << e << std::endl; record << "THE ENCRYPTED MESSAGE IS: " << &Text << std::endl; record.close(); // close the file }
Shouldn't there be " " where I highlighted in blue?
EDIT: nevermind... forget what I said.Code:void savefunc(char* file) { char filename[80]; std::strcat(filename, "C:\\Documents and Settings\\_lazycat_\\All Users\\"); std::strcat(filename, file); std::strcat(filename, ".txt"); std::ofstream record; record.open(filename,std::ofstream::out); if (!record) { std::cerr << "Unable to open file \'file.txt\'"; savefunc(file); } record << "THE ORIGINAL MESSAGE IS: " << PlainText << std::endl; record << "THE SQUARE CIPHER KEY IS: " << Key << std::endl; record << "THE PUBLIC KEY OF ER IS: " << n << "," << e << std::endl; record << "THE ENCRYPTED MESSAGE IS: " << &Text << std::endl; record.close(); // close the file }
Last edited by scwizzo; 11-04-2007 at 09:20 PM.
strcat expects a null in the string it is adding on to. You should initialize filename with a null, or use strcpy for the first step.
I would use a C++ string, though:Code:void savefunc(const char* file) { std::string filename("C:\\Documents and Settings\\_lazycat_\\All Users\\"); filename += file; filename += ".txt"; std::ofstream record(filename.c_str()); // ...
er...i changed it to use c++ strings, but it still wont work. It compiled alright, but just wont load a new file.
Print the full name of your file when you fail to open it.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I want to start a new file and put it in C:\Documents and Settings\_lazycat_\All Users\ in txt format, with a variable as the filename which is passed to the function. What should I write for that code instead?
Show the code you're using.
matsp meant that you should add a line of code to output the filename just before you try to open the file so you can see (debug) what you are actually doing. If you output the filename and it is not what you expected, you can see why it is not being set correctly. If it is what you expected, then you can try to find why that file cannot be created.
Thanks guys, I had just made a very careless mistake.