Solved

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

Posted on 2004-10-18
4
260 Views
Last Modified: 2013-12-03
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!
0
Comment
Question by:donfmorrison
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 50 total points
ID: 12339104
StreamReader* sr = new StreamReader(path);
        try {
            while (sr->Peek() >= 0) {
                Console::WriteLine(sr->ReadLine());
            }
        } __finally {
            if (sr) __try_cast<IDisposable*>(sr)->Dispose();
        }
0
 
LVL 1

Author Comment

by:donfmorrison
ID: 12339143
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
 
LVL 16

Expert Comment

by:nonubik
ID: 12339174
>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
 
LVL 1

Author Comment

by:donfmorrison
ID: 12339188
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question