From: mastermagrath on
Hi all,

I think i'm being very stupid here but its driving me nuts!
I'm running borland builderX on windows and trying to run a very simple
program using ifstreams.
For some reason i cant get the ifstream object to find the specific
input file i want to process:

int main() {

ifstream in ("C:\junk\C02\myfile.txt");
string myline;
while(getline(in, myline)){
cout << myline << endl;
}
}

Can someone comment?

From: mastermagrath on
Just noticed another strange thing, if i do the following:

int main() {
ofstream out ("C:\junk\C02\myfile.txt");

out << "one line" << endl << "two lines" << endl;

ifstream in ("C:\junk\C02\myfile.txt");
string myline;
while(getline(in, myline)){
cout << myline << endl;
}
}

It actually prints out the 2 lines, but if i then go to
c:\junk\CO2\myfile.txt it doesn't contain the 2 lines but rather its
contents are the same as before running the program??!!

From: osmium on
"mastermagrath" writes:

> I think i'm being very stupid here but its driving me nuts!
> I'm running borland builderX on windows and trying to run a very simple
> program using ifstreams.
> For some reason i cant get the ifstream object to find the specific
> input file i want to process:
>
> int main() {
>
> ifstream in ("C:\junk\C02\myfile.txt");
> string myline;
> while(getline(in, myline)){
> cout << myline << endl;
> }
> }
>
> Can someone comment?

It is standard practice to see if a file was actually opened before using
it. Also keep in mind that '\' has special characteristics in some
contexts because of its role as an escape character.


From: Bart van Ingen Schenau on
mastermagrath wrote:

> Just noticed another strange thing, if i do the following:
>
> int main() {
> ofstream out ("C:\junk\C02\myfile.txt");
>
> out << "one line" << endl << "two lines" << endl;
>
> ifstream in ("C:\junk\C02\myfile.txt");
> string myline;
> while(getline(in, myline)){
> cout << myline << endl;
> }
> }
>
> It actually prints out the 2 lines, but if i then go to
> c:\junk\CO2\myfile.txt it doesn't contain the 2 lines but rather its
> contents are the same as before running the program??!!

You could check if the file junkC02myfile.txt exists somewhere on drive
C:.
Your problem is that the backslash (\) in string literals and character
literals is interpreted by the compiler as the start of an escape
sequence.
Escape sequences are used to represent special characters, such as tab
(\t) and new-line (\n).
To get an actual backslash in your string, you have to use \\:
ofstream out ("C:\\junk\\C02\\myfile.txt");

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
From: Jim Langston on
"mastermagrath" <mbaxter1(a)lucent.com> wrote in message
news:1142862284.421149.106800(a)v46g2000cwv.googlegroups.com...
> Hi all,
>
> I think i'm being very stupid here but its driving me nuts!
> I'm running borland builderX on windows and trying to run a very simple
> program using ifstreams.
> For some reason i cant get the ifstream object to find the specific
> input file i want to process:
>
> int main() {
>
> ifstream in ("C:\junk\C02\myfile.txt");
> string myline;
> while(getline(in, myline)){
> cout << myline << endl;
> }
> }
>
> Can someone comment?

Answer has been given, to put a backslash in a quoted string you need 2. \\

Also, though, windows is smart enough to use forward slashes to load files.
That is, both of these open the same file:
ifstream in ("C:\\junk\\C02\\myfile.txt");

ifstream in ("C:/junk/C02/myfile.txt");

Beware, however, I know this works in windows 2000 and XP. I'm not sure how
far back the / used for \\ goes. Please check in
microsoft.public.vc.language or one of the other microsoft.* forums.


 |  Next  |  Last
Pages: 1 2
Prev: Matrixes
Next: try catch blocks