Thread: file saving issues

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    32

    file saving issues

    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Most probably that file is not found.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    i have got
    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
    }
    and i try to open a new file with it. Did I did it the right way?

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Shouldn't there be " " where I highlighted in blue?
    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
    }
    EDIT: nevermind... forget what I said.
    Last edited by scwizzo; 11-04-2007 at 09:20 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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());
    
    	// ...

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    er...i changed it to use c++ strings, but it still wont work. It compiled alright, but just wont load a new file.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    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?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    Thanks guys, I had just made a very careless mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. saving screen to file
    By z0diac in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-23-2003, 07:00 PM