Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 install failing. 18 79
What is the real difference between C++ and Visual C++.NET? 7 120
Visual Studio community 2015 2 80
Setup project 6 74
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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