I want to read in a text file and place it in an edit box. I know how to use the WINAPI to do this but I would like to use C++ streams.
eg. ifstream fin;
fin.open ( filename, ios::beg );
I then want to place the entire text file into 1 variable so that I can place it into the edit box using SetWindowText
Is this possible. I usually use getline ( fin, string ) to retrieve one line. Is there any way to use a char * or something to get the entire text file into a LPCTSTR.
Thanks a lot
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I want to read in a text file and place it in an edit box. I know how to use the WINAPI to do this but I would like to use C++ streams.
eg. ifstream fin;
fin.open ( filename, ios::beg );
I then want to place the entire text file into 1 variable so that I can place it into the edit box using SetWindowText
Is this possible. I usually use getline ( fin, string ) to retrieve one line. Is there any way to use a char * or something to get the entire text file into a LPCTSTR.
Thanks a lot
Hi,
I almost have the solution :
#include <fstream.h>
#include <iostream>
int main()
{
ifstream fin("c:\\windows\\desktop\\test.txt");
fin.seekg(0,ios::end);
int size = fin.tellg();
fin.seekg(NULL,ios::beg);
char *buffer = new char[size];
fin.read(buffer,size);
std::cout << size << std::endl;
std::cout << buffer << std::endl;
std::cout << std::endl;
delete[] buffer;
return 0;
}
The only problem here is that the fin.tellg() function returns an invalid amount if there is more than one line eg. if you type :
G
R
E
A
T
It gives you 15 characters - can anyone help?
If you are doing WINAPI, why not use a resource file?
eg.
// something.rc included into project
LEVELS TEXT DISCARDABLE "ResLevels.txt"
// Useage in main
HGLOBAL textlevels;
char *file;
textlevels = LoadResource (_glInstance, FindResource(_glInstance, TEXT("LEVELS"), TEXT("TEXT")));
file = (char*)LockResource(textlevels);
// Don't forget this at the end
FreeResource(textlevels);
// This is actual *working* code from a game of mine.
Curtis