473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getting int out of a plain text file

(Type your message here)

--------------------------------
From: Zephyr .

hey, i got trouble getting integers out of a plain text file.
i dont want to use binary files, just plain text files so they
can be hand-edited more easily.

now, for all i know (still a rookie)
there's StreamReader.Read() and StreamReader.ReadLine()
ReadLine() gets me somewhere, as it gets full string lines,
but as i need access to the integers, i have to use the Read()
which gives me strange?!? results. could anyone help me?

many thx

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>M6EI6Vs7ckGzsA2ft89Bnw==</Id>
Nov 17 '05 #1
4 1938
Zephyr . via .NET 247 wrote:
(Type your message here)

--------------------------------
From: Zephyr .

hey, i got trouble getting integers out of a plain text file.
i dont want to use binary files, just plain text files so they
can be hand-edited more easily.

now, for all i know (still a rookie)
there's StreamReader.Read() and StreamReader.ReadLine()
ReadLine() gets me somewhere, as it gets full string lines,
but as i need access to the integers, i have to use the Read()
which gives me strange?!? results. could anyone help me?

many thx

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>M6EI6Vs7ckGzsA2ft89Bnw==</Id>


Hey,

StreamReader.Read() will only return a single character at a time,
whereas StreamReader.ReadLine() will read an entire line from a file. I
assume these are the results you're seeing.

To better answer your question, it'd helpful to know the format of the
text file you're importing. If it's simply something like:

2134
253
364
35865
967
<etc...>

Then you can do something along the lines of:

while (reader.Peek() > -1)
{
int i = int.Parse(reader.ReadLine());
}

If the format is more complex, like:

123:234:235:346

Then you'd need to split the string you read in into an array, and
repeat the above, like so:

while (reader.Peek() > -1)
{
string[] numbers = reader.ReadLine().Split(":");
foreach (string s in numbers)
{
int i = int.Parse(s);
DoSomethingWithInteger(i);
}
}

Hopefully that helps out,
Clint

Nov 17 '05 #2
Hi,

How is your file orginized?

Note if it's a binary file you have no other choise but use BinaryReader.

If you are creating the file then use something easy like one number per
line, this can be edited easily and is a piece of cake reading:

int val_ = Convert.ToInt32( reader.ReadLine() );
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Zephyr . via .NET 247" <an*******@dotnet247.com> wrote in message
news:eq**************@TK2MSFTNGP14.phx.gbl...
(Type your message here)

--------------------------------
From: Zephyr .

hey, i got trouble getting integers out of a plain text file.
i dont want to use binary files, just plain text files so they
can be hand-edited more easily.

now, for all i know (still a rookie)
there's StreamReader.Read() and StreamReader.ReadLine()
ReadLine() gets me somewhere, as it gets full string lines,
but as i need access to the integers, i have to use the Read()
which gives me strange?!? results. could anyone help me?

many thx

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>M6EI6Vs7ckGzsA2ft89Bnw==</Id>

Nov 17 '05 #3

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

How is your file orginized?

Note if it's a binary file you have no other choise but use BinaryReader.

If you are creating the file then use something easy like one number per
line, this can be edited easily and is a piece of cake reading:

int val_ = Convert.ToInt32( reader.ReadLine() );
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Zephyr . via .NET 247" <an*******@dotnet247.com> wrote in message
news:eq**************@TK2MSFTNGP14.phx.gbl...
(Type your message here)

--------------------------------
From: Zephyr .

hey, i got trouble getting integers out of a plain text file.
i dont want to use binary files, just plain text files so they
can be hand-edited more easily.

now, for all i know (still a rookie)
there's StreamReader.Read() and StreamReader.ReadLine()
ReadLine() gets me somewhere, as it gets full string lines,
but as i need access to the integers, i have to use the Read()
which gives me strange?!? results. could anyone help me?

many thx

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>M6EI6Vs7ckGzsA2ft89Bnw==</Id>


As a tangent, is there any difference between Convert.ToInt32() and
Int.Parse()?

Clint

Nov 17 '05 #4
Clint (cm******@online.nospam) <cj*******@gmail.com> wrote:
As a tangent, is there any difference between Convert.ToInt32() and
Int.Parse()?


Only in terms of null handling - Convert.ToInt32 returns 0 for null;
int.Parse throws a NullReferenceException.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

5
by: Domestos | last post by:
Hi all - please help... using <FORM> HTML tag for user input and need to get get data from it and store in php variable... <input type="text" name="user_name" size="18" maxlength="16"...
4
by: hoke | last post by:
I want to display plain text files in the browser. The files contain html and javascript and have a .txt extension. This works fine with files with just html. Unfortunately when showing files with...
3
by: pradeep gummi | last post by:
I have an XML FILE that is to be converted to Plain Text using an XSL file. Since I just want plain text, I do not want to set any root element during transformation.And if I do not any root...
11
by: Brett | last post by:
In Yahoo mail, I click the Inbox link and see my messages. If I view source, I don't have HTML which contains the URL of each message. The source HTML contains javascripting and framesets. This...
2
by: Mike Bridge | last post by:
Is there any way to get Internet explorer to treat a text/plain .net page as plain text using asp.net? It seems like IE doesn't trust text/plain as a mime type, and so it (ironically) displays it...
10
by: Eric Lindsay | last post by:
This may be too far off topic, however I was looking at this page http://www.hixie.ch/advocacy/xhtml about XHTML problems by Ian Hickson. It is served as text/plain, according to Firefox...
7
by: toby989 | last post by:
Hi All Sorry for reposting...the entries of the post from 11/23/2005 by Eric Lindsay have been removed from the server already and I am seeing only the header. So, I have the problem of...
0
by: ptraj | last post by:
Dear ppls, I'm doing a small project to extract all messages from outlook by Visual Basic 6.0 and write them into a file(in MBOX format). While accessing messages i able to get headers, body,...
0
by: Rey | last post by:
Howdy all. Am using visual web developer 2005 (vb), xp pro sp2. In testing of the system.net.mail to send email from an aspx page where I'm pulling the email contents from a textbox, find that...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.