Solved

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

Posted on 2004-10-18
4
257 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now