Solved

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

Posted on 2004-10-18
4
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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