473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Read text based file on double-click

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
Nov 17 '05 #1
5 1398
ashton wrote:
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.

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

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

Nov 17 '05 #2
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
Nov 17 '05 #3
Derrick Coetzee [MSFT] <no*****@microsoft.invalid> wrote:
[...]

std::string ContentsToString(std::istream& in)
{
std::string contents;
char c;
in.get(c);
while(!in.eof()) {
contents += c;
in.get(c);
}
return contents;
}
Isn't it terribly inefficient to read
character by character???
[...]


Schobi

--
Sp******@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
Nov 17 '05 #4
"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:#A**************@TK2MSFTNGP11.phx.gbl...
Derrick Coetzee [MSFT] <no*****@microsoft.invalid> wrote:
[...]

std::string ContentsToString(std::istream& in)
{
std::string contents;
char c;
in.get(c);
while(!in.eof()) {
contents += c;
in.get(c);
}
return contents;
}


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.)
--
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
Nov 17 '05 #5
Derrick Coetzee [MSFT] <dc******@online.microsoft.com> wrote:
[...]

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():

std::string ContentsToString(std::istream& in)
{
std::string contents, line;
while(!in.eof()) {
contents += line;
std::getline(in, line);
}
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=....transmedia.de
Schobi

--
Sp******@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
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Bowling | last post by:
I'm trying to read a file that contains text for a decimal count, convert it to binary, increment and write it back out. I'm using Java 2 in SuSE 8.2, installed from the SuSE CD's. When using...
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
3
by: akang2005 | last post by:
When I write 'double' data to the file, it seems working fine, but when I read it later, it returns a eof when it encounters a particular number even the file is not at the end yet. However, while...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
1
by: Matrixinline | last post by:
Hi All, File Text.txt Contains following text as : "C:\program file\application data\details\app" "D:\Program File" I tried to read that data as fscanf(oFp, "%s %s", sCopyDirectory,...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.