473,486 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

basic file I/O issue

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?

Oct 18 '07 #1
2 1179
xo****@yahoo.com wrote:
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?
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
Oct 18 '07 #2
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]);
}
}
<xo****@yahoo.comwrote in message
news:11*********************@k35g2000prh.googlegro ups.com...
>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?

Oct 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4199
by: Steve | last post by:
objNewMail = Server.CreateObject("CDONTS.NewMail") How do I know whether the "Send" command from a CDONTS object actually was sent? Is there a status variable that can be checked? Or a log...
3
1460
by: nick | last post by:
Hi, I'm pretty much a database beginner and have what I think is a basic question: If I have a table which has a 'status' column and I can have say three statuses: "active", "pending",...
5
7205
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
6
2373
by: Erez Shor | last post by:
Hi, I need to build and asp page which access a remote windows server's registry and create a registry key. In order for the ASP page to be able to access the registry on the remote server I need...
3
1097
by: Dave | last post by:
I am reading Microsoft's ASP.NET Step by Step that states that I can create an aspx file in notepad. When I do this I get a an error message: "Server Application Unavailable" when I try to view...
2
1112
by: | last post by:
I was trying to display a report on a web page. I added a report object to my project and dropped a report viewer control on the page. I assigned the reportsource property in page_load, but I...
0
2007
by: Speilman_54 | last post by:
Hi, I'm converting an excel Macro into visual basic 2005 express, as I don't have a copy of VB 6 and trying to make and executable from it, I know this version doesn't have the save file as .exe,...
19
1783
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
6
38449
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
3
4876
by: klaus654 | last post by:
I have posted this also in the XML forum, as the code below actually came from an XML doc, but I think it's a basic HTML issue: Original post I am an IT consultant, but I work entirely on the...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.