473,407 Members | 2,676 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,407 software developers and data experts.

read text file

I need to read a text file but I need to start on line 2 of the file, How
can I start reading the text file starting with the second line and not the
first line in the file?

Nov 17 '05 #1
31 4938
Hi

seudo code:

StreamReader st = new StreamReader( ... )
string line = st.ReadLine();
while( (line=st.ReadLine()) != null )
{
}
st.Close();
cheers,

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

"microsoft.news.com" <CSharpCoder> wrote in message
news:eL**************@TK2MSFTNGP14.phx.gbl...
I need to read a text file but I need to start on line 2 of the file, How
can I start reading the text file starting with the second line and not the
first line in the file?

Nov 17 '05 #2
but the first line isn't NULL, it just doesn't have the data i'm looking
for.
Line 2 starts with the data i'm looking for so I need to start reading the
lines from line 2 on.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:O5**************@tk2msftngp13.phx.gbl...
Hi

seudo code:

StreamReader st = new StreamReader( ... )
string line = st.ReadLine();
while( (line=st.ReadLine()) != null )
{
}
st.Close();
cheers,

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

"microsoft.news.com" <CSharpCoder> wrote in message
news:eL**************@TK2MSFTNGP14.phx.gbl...
I need to read a text file but I need to start on line 2 of the file, How
can I start reading the text file starting with the second line and not
the first line in the file?


Nov 17 '05 #3
You are just discarding line 1 :)

that's why you are not reading it in the cicle, that is where you do your
work

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"microsoft.news.com" <CSharpCoder> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
but the first line isn't NULL, it just doesn't have the data i'm looking
for.
Line 2 starts with the data i'm looking for so I need to start reading the
lines from line 2 on.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:O5**************@tk2msftngp13.phx.gbl...
Hi

seudo code:

StreamReader st = new StreamReader( ... )
string line = st.ReadLine();
while( (line=st.ReadLine()) != null )
{
}
st.Close();
cheers,

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

"microsoft.news.com" <CSharpCoder> wrote in message
news:eL**************@TK2MSFTNGP14.phx.gbl...
I need to read a text file but I need to start on line 2 of the file, How
can I start reading the text file starting with the second line and not
the first line in the file?



Nov 17 '05 #4
I'm not following you on this one.
I don't need line 1 even though it has data in it. It doesn't have all the
data i need I just has a reference number in it. So i need to start reading
line 2 .

I'm not understanding what you mean

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eH**************@TK2MSFTNGP09.phx.gbl...
You are just discarding line 1 :)

that's why you are not reading it in the cicle, that is where you do your
work

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"microsoft.news.com" <CSharpCoder> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
but the first line isn't NULL, it just doesn't have the data i'm looking
for.
Line 2 starts with the data i'm looking for so I need to start reading
the lines from line 2 on.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:O5**************@tk2msftngp13.phx.gbl...
Hi

seudo code:

StreamReader st = new StreamReader( ... )
string line = st.ReadLine();
while( (line=st.ReadLine()) != null )
{
}
st.Close();
cheers,

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

"microsoft.news.com" <CSharpCoder> wrote in message
news:eL**************@TK2MSFTNGP14.phx.gbl...
I need to read a text file but I need to start on line 2 of the file,
How can I start reading the text file starting with the second line and
not the first line in the file?




Nov 17 '05 #5
<"microsoft.news.com" <CSharpCoder>> wrote:
I'm not following you on this one.
I don't need line 1 even though it has data in it. It doesn't have all the
data i need I just has a reference number in it. So i need to start reading
line 2 .

I'm not understanding what you mean


By reading line 1 and ignoring it, you effectively *are* starting
reading on line 2. Put it this way: reading line 1 and ignoring it
certainly give you the semantics you desire, in terms of the data you
actually care about - what are the downsides? Unless the first line is
likely to be *hugely* long (which it doesn't sound like from your
description) there won't be any significant performance penalty from
reading it, and it's *much* easier to do that than to write your own
code to find the end of the first line.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
what's happening is that i'm reading the file and looking for particular
dataitems using

substring(40,10)

and in line 1 location 40 is blank and its giving me an error and not
continuing. So how can I get around this?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"microsoft.news.com" <CSharpCoder>> wrote:
I'm not following you on this one.
I don't need line 1 even though it has data in it. It doesn't have all
the
data i need I just has a reference number in it. So i need to start
reading
line 2 .

I'm not understanding what you mean


By reading line 1 and ignoring it, you effectively *are* starting
reading on line 2. Put it this way: reading line 1 and ignoring it
certainly give you the semantics you desire, in terms of the data you
actually care about - what are the downsides? Unless the first line is
likely to be *hugely* long (which it doesn't sound like from your
description) there won't be any significant performance penalty from
reading it, and it's *much* easier to do that than to write your own
code to find the end of the first line.

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

Nov 17 '05 #7
<"microsoft.news.com" <CSharpCoder>> wrote:
what's happening is that i'm reading the file and looking for particular
dataitems using

substring(40,10)

and in line 1 location 40 is blank and its giving me an error and not
continuing. So how can I get around this?


By ignoring the first line, as we've said before. Just read it and then
*don't* use Substring (40, 10) on it. Read it and *then* enter the loop
you presumably has which is actually doing something useful.

Where you're opening the file, instead of just opening it, open it and
read the first line.

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

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:O5**************@tk2msftngp13.phx.gbl...
Hi

seudo code:

StreamReader st = new StreamReader( ... )
string line = st.ReadLine();
while( (line=st.ReadLine()) != null )
{
}
st.Close();
cheers,

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


// Create a new StreamReader.
StreamReader st = new StreamReader( ... );

try {
// Read and ignore the first line.
string line = st.ReadLine();

// Loop through, starting at the second line until the ReadLine
// method returns null, which indicates end of file.
while ((line = st.ReadLine() != null) {
// Do your processing on a line per line basis here.
}
} finally {
// Cleanup.
st.Close();
}
Notice at which point he is ignoring the first line...it's not part of the
loop :)

Mythran

Nov 17 '05 #9
I have all of the code you guys are showing but its still not working
correctly.
here is my text file

REFID5525236252625252632 //first line in text file
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line

I need the Name off of the second line on, as you can see the first line
does not have anything in it i really need.
I'm getting the data items by using Substring(31, 10) and when it reads the
first line it breaks do to nothing in that location on the first line.

all the code you guys are showing I have and is working if the first line is
blank in the text file, but it is no do to that is only the reference number
for the dealer sending the file over

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:Oz****************@TK2MSFTNGP15.phx.gbl...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:O5**************@tk2msftngp13.phx.gbl...
Hi

seudo code:

StreamReader st = new StreamReader( ... )
string line = st.ReadLine();
while( (line=st.ReadLine()) != null )
{
}
st.Close();
cheers,

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


// Create a new StreamReader.
StreamReader st = new StreamReader( ... );

try {
// Read and ignore the first line.
string line = st.ReadLine();

// Loop through, starting at the second line until the ReadLine
// method returns null, which indicates end of file.
while ((line = st.ReadLine() != null) {
// Do your processing on a line per line basis here.
}
} finally {
// Cleanup.
st.Close();
}
Notice at which point he is ignoring the first line...it's not part of the
loop :)

Mythran

Nov 17 '05 #10
<"microsoft.news.com" <CSharpCoder>> wrote:
I have all of the code you guys are showing but its still not working
correctly.
here is my text file

REFID5525236252625252632 //first line in text file
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line

I need the Name off of the second line on, as you can see the first line
does not have anything in it i really need.
I'm getting the data items by using Substring(31, 10) and when it reads the
first line it breaks do to nothing in that location on the first line.
That suggests you're *not* using the code which has been posted,
because if you were, you wouldn't be trying to call Substring on the
first line...
all the code you guys are showing I have and is working if the first line is
blank in the text file, but it is no do to that is only the reference number
for the dealer sending the file over


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

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

StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

}
sr.Close();
}

I need to use SubString because I need to get only certain dataitems, from
the line, not all only 3.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"microsoft.news.com" <CSharpCoder>> wrote:
I have all of the code you guys are showing but its still not working
correctly.
here is my text file

REFID5525236252625252632 //first line in text file
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line

I need the Name off of the second line on, as you can see the first line
does not have anything in it i really need.
I'm getting the data items by using Substring(31, 10) and when it reads
the
first line it breaks do to nothing in that location on the first line.


That suggests you're *not* using the code which has been posted,
because if you were, you wouldn't be trying to call Substring on the
first line...
all the code you guys are showing I have and is working if the first line
is
blank in the text file, but it is no do to that is only the reference
number
for the dealer sending the file over


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

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

Nov 17 '05 #12

"microsoft.news.com" <CSharpCoder> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
here is what i have

StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

}
sr.Close();
}

I need to use SubString because I need to get only certain dataitems, from
the line, not all only 3.


Ok, your first sr.ReadLine()...

string line = sr.ReadLine();

This reads in the first line...before it calls substring, it performs the
while statement:

while ((line = sr.ReadLine()) != null)

this reads in the second line and discards the first line...therefore, the
first line never has anything done to it...it is essentially skipped. So,
how is this not what you are looking for? Maybe you are assuming the real
first line is line 0 and you need to skip the first 2 lines?

Mythran

Nov 17 '05 #13
<"microsoft.news.com" <CSharpCoder>> wrote:
here is what i have

StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

}
sr.Close();
}

I need to use SubString because I need to get only certain dataitems, from
the line, not all only 3.


That's not a short but complete program - it wouldn't even compile
within a method.

See http://www.pobox.com/~skeet/csharp/incomplete.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #14
this is how the text file looks like

//this is the very first line in the file
REFID5525236252625252632 //first line in text file

//this is the second line in the file.
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line

I need to use SubString to get the name out of the string.


"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...

"microsoft.news.com" <CSharpCoder> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
here is what i have

StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

}
sr.Close();
}

I need to use SubString because I need to get only certain dataitems,
from the line, not all only 3.


Ok, your first sr.ReadLine()...

string line = sr.ReadLine();

This reads in the first line...before it calls substring, it performs the
while statement:

while ((line = sr.ReadLine()) != null)

this reads in the second line and discards the first line...therefore, the
first line never has anything done to it...it is essentially skipped. So,
how is this not what you are looking for? Maybe you are assuming the real
first line is line 0 and you need to skip the first 2 lines?

Mythran

Nov 17 '05 #15
"microsoft.news.com" <CSharpCoder> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
here is what i have

StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

}
sr.Close();
}

I need to use SubString because I need to get only certain dataitems, from
the line, not all only 3.

<OBSERVATION>
Obviously this is not your actual code since SubString is not a method of
string (Substring is)
</OBSERVATION>

First things first:
What exception are you getting?
What Line is throwing the exception?
How Long is that line?

I suspect that you have some line (after the first) that is shorter than 41
characters in length.
You will get something that looks like this:
System.ArgumentOutOfRangeException: Index and length must refer to a
location within the string

Try replacing

string name = line.SubString(31,10);
with
if (line.Length < 41)
Console.WriteLine("SHORT:{0}",line);

This should point you to the offending line.

Good luck
Bill



Nov 17 '05 #16
<"microsoft.news.com" <CSharpCoder>> wrote:
this is how the text file looks like

//this is the very first line in the file
REFID5525236252625252632 //first line in text file

//this is the second line in the file.
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line

I need to use SubString to get the name out of the string.


So you keep saying. What you haven't provided is a short but complete
program which demonstrates the problem.

You've provided some code which doesn't quite compile and isn't
complete, but which looks like it's a reasonable stab at a solution.
Without some *real* code which doesn't work, we won't be able to work
out what you're doing wrong.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #17
its breaking on the very first line in the text file do to the line is only
20 characters long and I need to look for the item at position 31.

I get the following error "input string is in the incorrect format" and this
is do to nothing being there.
"Bill Butler" <Bi@DigitalArts.c0m> wrote in message
news:eo**************@TK2MSFTNGP15.phx.gbl...
"microsoft.news.com" <CSharpCoder> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
here is what i have

StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

}
sr.Close();
}

I need to use SubString because I need to get only certain dataitems,
from the line, not all only 3.

<OBSERVATION>
Obviously this is not your actual code since SubString is not a method of
string (Substring is)
</OBSERVATION>

First things first:
What exception are you getting?
What Line is throwing the exception?
How Long is that line?

I suspect that you have some line (after the first) that is shorter than
41 characters in length.
You will get something that looks like this:
System.ArgumentOutOfRangeException: Index and length must refer to a
location within the string

Try replacing

string name = line.SubString(31,10);
with
if (line.Length < 41)
Console.WriteLine("SHORT:{0}",line);

This should point you to the offending line.

Good luck
Bill



Nov 17 '05 #18
this is the entire function in the code.
it reads the file, gets the item at position 31, which is 10 characters
then takes that and populates a word doc.
the code is right here and i can compile it and get it working. BUT when the
first line is only 20 characters long - in which it should be it breaks. I
get the following errror : input string is in the incorrect format.
and it will no longer process.
the entire thing is here.
StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

//take the name value and generate an word document
}
sr.Close();
}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"microsoft.news.com" <CSharpCoder>> wrote:
this is how the text file looks like

//this is the very first line in the file
REFID5525236252625252632 //first line in text file

//this is the second line in the file.
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line

I need to use SubString to get the name out of the string.


So you keep saying. What you haven't provided is a short but complete
program which demonstrates the problem.

You've provided some code which doesn't quite compile and isn't
complete, but which looks like it's a reasonable stab at a solution.
Without some *real* code which doesn't work, we won't be able to work
out what you're doing wrong.

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

Nov 17 '05 #19
<"microsoft.news.com" <CSharpCoder>> wrote:
its breaking on the very first line in the text file do to the line is only
20 characters long and I need to look for the item at position 31.
I get the following error "input string is in the incorrect format" and this
is do to nothing being there.


You wouldn't get that error from Substring.

Once more, *please* post a short but *complete* program which
demonstrates the problem. Just telling us the same thing again and
again isn't going to get you anywhere.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #20
this is the entire function in the code.
it reads the file, gets the item at position 31, which is 10 characters
then takes that and populates a word doc.
the code is right here and i can compile it and get it working. BUT when the
first line is only 20 characters long - in which it should be it breaks. I
get the following errror : input string is in the incorrect format.
and it will no longer process.
the entire thing is here.

static void ReadFile()
{
StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

//take the name value and generate an word document
}
sr.Close();
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"microsoft.news.com" <CSharpCoder>> wrote:
its breaking on the very first line in the text file do to the line is
only
20 characters long and I need to look for the item at position 31.
I get the following error "input string is in the incorrect format" and
this
is do to nothing being there.


You wouldn't get that error from Substring.

Once more, *please* post a short but *complete* program which
demonstrates the problem. Just telling us the same thing again and
again isn't going to get you anywhere.

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

Nov 17 '05 #21

"microsoft.news.com" <CSharpCoder> wrote in message
news:Of**************@TK2MSFTNGP09.phx.gbl...
its breaking on the very first line in the text file do to the line is
only 20 characters long and I need to look for the item at position 31.

I get the following error "input string is in the incorrect format" and
this is do to nothing being there.
"Bill Butler" <Bi@DigitalArts.c0m> wrote in message
news:eo**************@TK2MSFTNGP15.phx.gbl...
"microsoft.news.com" <CSharpCoder> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
here is what i have


Post the code, and the first 10 lines of the file you are reading (making
sure no sensitive data is shown). We will do our best to provide you with a
suitable answer.

What he means by a complete program, is the entire file you are using to
write with. If not that, then the ENTIRE method used to read the file.
(Not just the snippet you are providing us..but the actual function such
as):

public string ReadFile(string FileName)
{
// Code to read the file here.
}

:)

Mythran

Nov 17 '05 #22

"microsoft.news.com" <CSharpCoder> wrote in message
news:Os**************@TK2MSFTNGP12.phx.gbl...
<snip>
I get the following errror : input string is in the incorrect format.


This error almost always means that you have a problem with a format string.
EXAMPLE:
Console.WriteLine("{0)",3); // Curly bracket replaced with curved bracket
produces
System.FormatException: Input string was not in a correct format.

I would suggest that your problem lies AFTER the Substring call

Had you posted ACTUAL code we could have helped you faster.

Bill
Nov 17 '05 #23
> static void ReadFile()
{
StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

//take the name value and generate an word document
}
sr.Close();
}
}


ONce again, this can't be the full program. There is no SubString method of
the string object. Therefore, the above code could not even compile.
Anywho, maybe you are getting a compiler error? If that's the case, change:

string name = line.SubString(31, 10);

to

string name = line.Substring(31, 10);

:)
static void ReadFile()
{
StreamReader reader = new StreamReader("file.txt");

try {
string line = reader.ReadLine();

while ((line = reader.ReadLine()) != null) {
string name = line.Substring(31, 10);

// take the name value and generate a word document.
}
} finally {
// Cleanup.
reader.Close();
}
}

Also, just as an extra word of advice...you should take note of the
try...finally being used in the example above. Using that will make sure
that even if an exception was raised, the StreamReader closes the connection
to the file (open file handle) and release it's resources associated with
reading the file.

Mythran

Nov 17 '05 #24
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--


PasteBin link is broken.

Mythran

Nov 17 '05 #25
<"microsoft.news.com" <CSharpCoder>> wrote:
this is the entire function in the code.


No it's not. It doesn't compile as you've written it. You haven't got a
semi-colon at the end of the first line, and you've used SubString
instead of Substring.

Even if it *were* the entire function, there's a big difference between
one function and a complete program.

Please read http://www.pobox.com/~skeet/csharp/incomplete.html

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

<snip>
Also, just as an extra word of advice...you should take note of the
try...finally being used in the example above. Using that will make sure
that even if an exception was raised, the StreamReader closes the connection
to the file (open file handle) and release it's resources associated with
reading the file.


Although a better solution to that would be to use a using statement:

using (StreamReader reader = new StreamReader ("file.txt"))
{
...
}

No need to manually call Close, Dispose etc :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #27
Mythran <ki********@hotmail.comREMOVETRAIL> wrote:
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.


PasteBin link is broken.


Cheers - fixed now.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #28
this is the entire code cause I'm using it now and it works just fine. it
compiles etc.
if type this in your IDE it will compile.
and if you type "LINE" and hit the period you will get the intellisense and
you'll see SubString as an option.
static void ReadFile()
{
StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

//take the name value and generate an word document
}
sr.Close();
}
}

due to sensitve data here is a test file i'm using

REFID5525236252625252632 //first line in text file
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line
LEXUSES3002004VIN9686548V54G54 PaulBunnan 25,2525
BMW328 2005VIN66525G252R252 JohnSmith 24,000
this code does work and compile i've been using it all day with no compile
issues.
as for line and the SubString method, check this out.
http://msdn.microsoft.com/library/de...tringtopic.asp

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
static void ReadFile()
{
StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

//take the name value and generate an word document
}
sr.Close();
}
}


ONce again, this can't be the full program. There is no SubString method
of the string object. Therefore, the above code could not even compile.
Anywho, maybe you are getting a compiler error? If that's the case,
change:

string name = line.SubString(31, 10);

to

string name = line.Substring(31, 10);

:)
static void ReadFile()
{
StreamReader reader = new StreamReader("file.txt");

try {
string line = reader.ReadLine();

while ((line = reader.ReadLine()) != null) {
string name = line.Substring(31, 10);

// take the name value and generate a word document.
}
} finally {
// Cleanup.
reader.Close();
}
}

Also, just as an extra word of advice...you should take note of the
try...finally being used in the example above. Using that will make sure
that even if an exception was raised, the StreamReader closes the
connection to the file (open file handle) and release it's resources
associated with reading the file.

Mythran

Nov 17 '05 #29

"microsoft.news.com" <CSharpCoder> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
this is the entire code cause I'm using it now and it works just fine. it
compiles etc.
if type this in your IDE it will compile.
and if you type "LINE" and hit the period you will get the intellisense
and you'll see SubString as an option.


THERE IS NO WAY THAT THIS IS YOUR CODE!!!

'SubString' will not compile since the method does not exist.
C# is case sensitive - the method is Substring // lower case string

Also
StreamReader sr = new StreamReader("file.txt") // is missing a semicolon

Please post code that will actually compile and demonstrates the problem

Bill
Nov 17 '05 #30
<"microsoft.news.com" <CSharpCoder>> wrote:
this is the entire code cause I'm using it now and it works just fine. it
compiles etc.
No, it doesn't.
if type this in your IDE it will compile.
No it won't.
and if you type "LINE" and hit the period you will get the intellisense and
you'll see SubString as an option.
No, you'll see Substring, with a lower case second s.
this code does work and compile i've been using it all day with no compile
issues.
Sorry, but I don't believe you. I don't believe that you have the only
case-insensitive C# compiler (and one which doesn't require a semi-
colon at the end of statements).
as for line and the SubString method, check this out.


That gives details of the Substring method. There is no SubString
method. C# is case-sensitive.

Now, how many people do you need to tell you that the code you're
posting won't compile before you start to believe them?

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

"microsoft.news.com" <CSharpCoder> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
this is the entire code cause I'm using it now and it works just fine. it
compiles etc.
if type this in your IDE it will compile.
and if you type "LINE" and hit the period you will get the intellisense
and you'll see SubString as an option.
static void ReadFile()
{
StreamReader sr = new StreamReader("file.txt")
{
string line = sr.ReadLine();
while ((line = sr.ReadLine()) !=null)
{
//needs to start reading from line 2 not line 1
string name = line.SubString(31,10);

//take the name value and generate an word document
}
sr.Close();
}
}

due to sensitve data here is a test file i'm using

REFID5525236252625252632 //first line in text file
BMW325CI 2003VIN52525TG582528 JohnSmith 45,000 //second line
LEXUSES3002004VIN9686548V54G54 PaulBunnan 25,2525
BMW328 2005VIN66525G252R252 JohnSmith 24,000
this code does work and compile i've been using it all day with no compile
issues.
as for line and the SubString method, check this out.
http://msdn.microsoft.com/library/de...tringtopic.asp

Substring != SubString.

Anyway it works for me with 20 chars in the first line!

Willy.
Nov 17 '05 #32

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

Similar topics

3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
3
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
0
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola,...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
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
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
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...
0
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
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,...

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.