read text file

  • Thread starter Thread starter microsoft.news.com
  • Start date Start date
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();
}
}
 
microsoft.news.com said:
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.

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
 
"microsoft.news.com" <CSharpCoder> wrote in message
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
 
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
 
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 :)
 
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/d...html/frlrfsystemstringclasssubstringtopic.asp
 
microsoft.news.com said:
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
 
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?
 
microsoft.news.com said:
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/d...html/frlrfsystemstringclasssubstringtopic.asp


Substring != SubString.

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

Willy.
 
Back
Top