Using StreamReader in managed code... or any other kind of file I\O

This one is easy I am sure.  I just haven't been able to track down much information about my problem.

I am trying to use the StreamReader to read a file (MSDN says I can do this) but I cannot get it to work the way they say it does.  In the code below:

---- code snip ----------------------------------------------------------------
StreamReader *sr = new StreamReader("testfile.txt");
String *line;
while( (line = sr->ReadLine()) )
{
    cout << line << endl;
}
----------------------------------------------------------------------------------

the output I get is similar to this:

1
1
1
1
1
...

It seems as though the value returned by the ReadLine function is returning a bool (true in this case because the read was a success I guess).  ReadLine() is supposed to return a managed String.

If anyone knows what the problem is, I would appreciate it.  

If there is a better way to do File I\O in managed C++ I would glad to try that as well.  I am getting a bit frustrated.  I tried the ifstream, but the compiler told me that it did not like that in a managed class? Please Help!
LVL 1
donfmorrisonAsked:
Who is Participating?
 
nonubikConnect With a Mentor Commented:
StreamReader* sr = new StreamReader(path);
        try {
            while (sr->Peek() >= 0) {
                Console::WriteLine(sr->ReadLine());
            }
        } __finally {
            if (sr) __try_cast<IDisposable*>(sr)->Dispose();
        }
0
 
donfmorrisonAuthor Commented:
excellent!

What if I just want to read one word at a time?  Is there a easy way to this.

I guess that the cout is why my earlier try did not work?
0
 
nonubikCommented:
>What if I just want to read one word at a time?  Is there a easy way to this.
No, there's no direct method to call. You'll nead to parse your line for words.

>What if I just want to read one word at a time?  Is there a easy way to this.
My guess is that Peek() enables you to go on
0
 
donfmorrisonAuthor Commented:
Thanks again.  This at least get me going in the right direction, which is much better off than I was a few minutes ago.
0
All Courses

From novice to tech pro — start learning today.