Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2005

Tip: Looking for answers? Try searching our database.

Read text based file on double-click

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ashton - 15 Nov 2004 20:10 GMT
Hello,

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.

How can I then read the contents of this file into my application?

Any help would be appreciated
Steve Alpert - 16 Nov 2004 17:17 GMT
> Hello,
>
[quoted text clipped - 5 lines]
> For example, I would right click on this file, select open with, then
> navigate to my EXE to open this file.

Look at file associations

C:\temp>assoc .scx
.fle=fle_auto_file

C:\temp>ftype fle_auto_file
fle_auto_file="C:\myxapp\myapp.exe" "%1"

/steveA

Signature

Steve Alpert
my email Fgrir_Nycreg @ vqk.pbz is encrypted with ROT13 (www.rot13.org)
and spaces

Derrick Coetzee [MSFT] - 10 Jan 2005 23:54 GMT
Hi! I realize this is an old post, but I thought your question deserved some
additional explanation.

> 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/default.asp?url=/library/en-us/cpref/html/frlr
fsystemiostreamreaderclassreadtoendtopic.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.
Signature

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

Hendrik Schober - 12 Jan 2005 14:26 GMT
> [...]
>
[quoted text clipped - 9 lines]
>     return contents;
> }

 Isn't it terribly inefficient to read
 character by character???

> [...]

 Schobi

Signature

SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett

Derrick Coetzee [MSFT] - 12 Jan 2005 21:49 GMT
> > [...]
> >
[quoted text clipped - 12 lines]
>   Isn't it terribly inefficient to read
>   character by character???

The library and OS include several layers of buffering, so it's not too bad,
but the calls do impose a lot of overhead. I just gave the simplest thing I
could think of. For typical text files, another simple and more efficient
way to do it is to use std::getline():

std::string ContentsToString(std::istream& in)
{
   std::string contents, line;
   while(!in.eof()) {
       contents += line;
       std::getline(in, line);
   }
   return contents;
}

This may use excessive memory if the file contains very long lines (as it
might if you supply a binary file or if the supplier is malicious.) You can
use istream::readsome() to get the buffer in blocks or even all at once
(although only into an ordinary char*; getting it into a std::string
requires temporarily using more memory.)
Signature

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

Hendrik Schober - 14 Jan 2005 11:10 GMT
> [...]
>
> The library and OS include several layers of buffering, so it's not too bad,
> but the calls do impose a lot of overhead. I just gave the simplest thing I
> could think of.

 Just setting up sentries for every
 character seems like a lot of wasted
 performance, I guess.

>                 For typical text files, another simple and more efficient
> way to do it is to use std::getline():
[quoted text clipped - 8 lines]
>     return contents;
> }

 Note that this strips EOLs from the file.

> This may use excessive memory if the file contains very long lines (as it
> might if you supply a binary file or if the supplier is malicious.) You can
> use istream::readsome() to get the buffer in blocks or even all at once
> (although only into an ordinary char*; getting it into a std::string
> requires temporarily using more memory.)

 Also see here:
   http://www.google.de/groups?threadm=c1j5uf%24o2g%241%40news1.transmedia.de

 Schobi

Signature

SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.