read text file

  • Thread starter Thread starter microsoft.news.com
  • Start date Start date
M

microsoft.news.com

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?
 
Hi

seudo code:

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


cheers,
 
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.
 
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,
 
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
 
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.
 
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?
 
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.
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi

seudo code:

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


cheers,

// 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
 
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
 
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.
 
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.
 
microsoft.news.com said:
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
 
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
 
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.
 
microsoft.news.com said:
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
 
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.
 
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.
 
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.
 
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();
}
 
Back
Top