Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

basic file I/O issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xontrn@yahoo.com - 18 Oct 2007 02:24 GMT
I have a text file that is just a list of integers separated by a
space.  In C++ I could do something like:

ifstream fin("filename.txt");

int x, y, z;
fin >> x >> y >> z;

to read the integers in.

How do I do that in C# with .NET IO?

Do I have to just read it all in as a giant string and then parse the
string?
Peter Duniho - 18 Oct 2007 04:18 GMT
> I have a text file that is just a list of integers separated by a
> space.  In C++ I could do something like:
[quoted text clipped - 10 lines]
> Do I have to just read it all in as a giant string and then parse the
> string?

I'm not aware of anything in C# like the C++ standard i/o stuff.
However, you definitely don't have to read everything in "as a giant
string".  You can use StreamReader to open the file, and then use the
StreamReader.Read() method to read the characters from the file.

The easiest thing to code would be to just read a single character at a
time, adding it iteratively to a StringBuilder instance.  Then when you
hit a space, call int.Parse(), int.TryParse(), or Convert.ToInt32()
passing the result of calling StringBuilder.ToString() to convert the
string to an int.  Clear out the StringBuilder and continue, until
you've read all the data you want to.

Pete
Hilton - 18 Oct 2007 10:05 GMT
Not the most efficient, and it assumes (at least) three integers per line.
Add the necessary error checking and performance improvements as required.

using System.IO;

using (StreamReader sr = new StreamReader ("filename.txt"))
{
 string line;
 while ((line = sr.ReadLine()) != null)
 {
   string[] tokens = line.Split (' ');

   int a = int.Parse (tokens [0]);
   int b = int.Parse (tokens [1]);
   int c = int.Parse (tokens [2]);
 }
}

>I have a text file that is just a list of integers separated by a
> space.  In C++ I could do something like:
[quoted text clipped - 10 lines]
> Do I have to just read it all in as a giant string and then parse the
> string?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.