Hi! I realize this is an old post, but I thought your question deserved some
additional explanation.
"ashton" <as****@discussions.microsoft.com> wrote:
I have a file (mytest.fle) it is just a text file with a different
extension. I would like to be able to open this file through my C++ application and
read it in as a string.
For example, I would right click on this file, select open with, then
navigate to my EXE to open this file.
When you Open or Open With a file in Explorer, it invokes the application
you're opening it with, passing the file's full path as the first argument.
You can find this path by examining argv[1] in your main entry function -
but be careful if you invoke your program from the command-line, where you
will have to put quotes around the filename if it contains a space.
As for reading the contents of the file in as a string, if you have access
to the .NET Framework you can use StreamReader's member function ReadToEnd.
See this topic, which includes an example in C++:
http://msdn.microsoft.com/library/de...toendtopic.asp
If you cannot access the .NET Framework, or wish to port this code in the
future, you can accomplish the same thing in standard C++ using a function
like this one:
#include <fstream>
#include <iostream>
#include <string>
std::string ContentsToString(std::istream& in)
{
std::string contents;
char c;
in.get(c);
while(!in.eof()) {
contents += c;
in.get(c);
}
return contents;
}
int main(int argc, char* argv[])
{
std::ifstream in(argv[1]);
if (in) {
std::cout << ContentsToString(in);
in.close();
}
return 0;
}
I hope this helps.
--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm