Access txt file in resource

Hello,

I've added TXT file to my resource, but now I need to know how to access data in it?

can I use fstream to read data? and if yes, how? can u give me some example of how to write to that file?

I have this:

resource.rc
1
2
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_0_1            0              "..\\..\\..\\..\\..\\Desktop\\test.txt"


resource.h
#define IDR_0_1 119

main.cpp
1
2
3
HRSRC hRes = FindResource(0, MAKEINTRESOURCE(IDR_0_1), "0");
HGLOBAL hData = LoadResource(0, hRes);
LPVOID data = LockResource(hData);

@moschops, ok mr. "I won't read your post but I'll reply with some shit on every forum", give me example on how to use that with my file
@mekkatorqu
How to read in from your file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace 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>
using namespace 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).

1
2
3
4
5
6
7
8
9
10
11
12
13
std::string result;

HRSRC hRes = FindResource(0, MAKEINTRESOURCE(IDR_0_1), MAKEINTRESOURCE(MYRESTYPE));
if(NULL != hRes)
{
    HGLOBAL hData = LoadResource(0, hRes);
    if(NULL != hData)
    {
	DWORD dataSize = SizeofResource(0, hRes);
        char* data = (char*)LockResource(hData);
	result.assign(data, dataSize);
    }
}


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??

Andy

P.S. Also see "Embed Text File in a Resource in a native Windows Application"
http://stackoverflow.com/questions/2933295/embed-text-file-in-a-resource-in-a-native-windows-application
Last edited on
@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? :))
Last edited on
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...

Andy
Last edited on

@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.
Last edited on
@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!)
Andy, no worries - I'll never whinge about someone being helpful! I'm only counter-whinging, rather than primary-whinging! :)
@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

1
2
3
4
5
6
string path = SHGetKnownFolderPath(FOLDERID_LocalAppData);
string folder = "\\Program\\test.txt";
string completePath = path+folder;

ofstream File(completePath)
...
Last edited on
See MSDN for how to use SHGetKnownFolder(). It doesn't return a string, it uses a buffer.

Otherwise that's pretty much it. Uses ofstream/ifstream as usual.

You will prob. need to create your subfolder, though.
that way I posted before?
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)
Topic archived. No new replies allowed.