#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
string line;
ifstream myfile ("test.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
How to write out to a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// writing on a text file
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
ofstream myfile ("test.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
I took the example code on the page I linked, and swapped "example.txt" for "test.txt"
If you struggled to work that out for yourself, I think you're not ready for using files.
You don't need fstream to read the data. The contents of the file will be included as a string in the resource.
To get at the string, your call sequence is pretty much there. I would avoid using 0 as your custom resource type, but other than that, all you need to do is cast the return of LockResource to char* (assuming it's an ascii text file, rather than unicode, etc).
If you have line breaks in your file, you might have to convert them, depending on how you want to use the data (the \r\n get mapped to \n automatically when you use ifstream, but not when you just read the text directly from the resource). But maybe you could use the char* string to init a std::istringstream and read from that? (I haven't tried this, but it should work.)
[Edit: istringstream is OK, but it doesn't map the \r\n to \n...]
can u give me some example of how to write to that file?
Did you also want to update the string in your resource??
@moschops how I said, try to read post first, and then reply
@andywestken thanks a lot once again! but I have more questions as usual :P
edit: sorry, I tried that code and then replied...
yes, I wanna make program to hold username and password if user wants that (remember me option), so it will looks like this:
1 2
username: name
password: pw
I know how to update normal file, how to get specific word in line etc, but I don't know how to work with files in resource, so an example on how to update, save and write to file will be helpful :P and also reading specific lines, not all at once (i know u mentioned it but can u be a little bit more specific and show example? :))
In general you would not update an embedded text file. Normal users don't have the necessary privileges to write to an executable, just to run it. So I've only ever seen resource update code being used to modify files for development purposes (For example, an in-place localization tool, which swaps all the e.g. English strings for French equivalents.)
More usually, the embedded file is used as the starting point. When the app starts it checks for the real file. If it's missing, it loads the one from the resource and then saves it. Changes are then made to the actual file, which is found the next time the app is run.
While not all apps have done as they are supposed to, app settings are supposed to be in either the user's or the all users application data folder. To get the path to this folder you use -- with Windows Vista and Windows 7 -- SHGetKnownFolderPath() using either FOLDERID_LocalAppData (for per-user data) or FOLDERID_ProgramData (for all users). And you usually create a subfolder (or even two -- company name\app name) to store your files in.
(For Windows XP and earlier, you need to use SHGetFolderPath() instead of SHGetKnownFolderPath().)
FOLDERID_LocalAppData is good if more that one person uses an app and you want to keep per-user settings. You can save the file once for each user, and they will see only there changes. Of course, you can also save the settings in a common file instead, or as well.
And as it's in a file, once you got things working, you'll need to worry about encrypting your logon information...
@mekkatorqu try to read forum heading first, and then ask question
This is the general C++ forum, so I gave you the general C++ answer. You're doing Windows programming. If you want just the Windows answer, don't ask in the general C++ forum.
@Moschops -- I agree with you that this is the wrong forum for the question. But cplusplus.com seems to let people off, unlike some other forums, so I went ahead and answered the question.
But the only actual general C++ answer to the question asked is : you cannot do that (read from a Windows resouce) in standard C++ (...so go and ask in the Windows forum!)
@moschops wow, u seems to be really slow or something, I was asking how to read from txt file in resource, not how to read from txt file, it's like someone's asking "how to show messagebox" and u'll say "use cout"
@andy so... if I understood it right, I'm supposed to create normal txt file in user's AppData or ProgramData and update it there and not to use resource? if I'm wrong can u explain me it better and show me example ?:P and if I'm right, can u show me some easy example on how to create/find/save file in AppData? can I use fstream? like
Once you've loaded you file data from the resource and found/created the appropriate directory, it's just normal file reading and writing code you need (i.e. ofstream/ifstream or whatever -- C style/WIN32/Boost)