Click here to Skip to main content
15,890,123 members
Home / Discussions / C#
   

C#

 
GeneralRe: .NET C# RegEx - Poor Perfomance - Suggested Alternative? Pin
Ryan Neil Shaw3-Feb-09 11:55
Ryan Neil Shaw3-Feb-09 11:55 
QuestionString to Type Keys. Pin
zetrixfire2-Feb-09 12:36
zetrixfire2-Feb-09 12:36 
AnswerRe: String to Type Keys. Pin
Christian Graus2-Feb-09 13:06
protectorChristian Graus2-Feb-09 13:06 
AnswerRe: String to Type Keys. Pin
Guffa2-Feb-09 13:32
Guffa2-Feb-09 13:32 
QuestionSerialization in practice..When? Pin
bolly-812-Feb-09 10:40
bolly-812-Feb-09 10:40 
AnswerRe: Serialization in practice..When? Pin
Christian Graus2-Feb-09 10:48
protectorChristian Graus2-Feb-09 10:48 
AnswerRe: Serialization in practice..When? Pin
PIEBALDconsult2-Feb-09 12:11
mvePIEBALDconsult2-Feb-09 12:11 
QuestionRead in text file Pin
alwaysthinking2-Feb-09 9:42
alwaysthinking2-Feb-09 9:42 
I am trying to take the some information from a txt file and use them as values for variables. I am in the development stage and just trying to get a hang of what exactly is required to do this and then build up.

CODE:
using System;
using System.IO;
public class TextFromFile
{
private const string FILE_NAME = "e:\\test.txt";
public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
Console.Read();

return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String fromtxt;
int IntValue;

while ((fromtxt = sr.ReadLine()) != null)
{
Console.WriteLine(fromtxt);
IntValue = Convert.ToInt32(fromtxt);
Console.WriteLine(IntValue);

}

Console.WriteLine("The end of the stream has been reached.");
Console.Read();
sr.Close();
}
}
}



In my case I have the text file simply have the number 5 written so the expected output is:
5
5
The end of the stream has been reached.

When I put:

while ((fromtxt = sr.ReadLine()) != null)
{
Console.WriteLine(fromtxt);


}
IntValue = Convert.ToInt32(fromtxt);
Console.WriteLine(IntValue);
I get the output:
5
0

When Keep put


while ((fromtxt = sr.ReadLine()) != null)
{
Console.WriteLine(fromtxt);

IntValue = Convert.ToInt32(fromtxt);
Console.WriteLine(IntValue);
}

Console.WriteLine(IntValue);

I get the error message:
Error 1 Use of unassigned local variable 'IntValue
AnswerRe: Read in text file Pin
Natza Mitzi2-Feb-09 10:15
Natza Mitzi2-Feb-09 10:15 
QuestionPrint a DataGridView Pin
Jordanwb2-Feb-09 8:43
Jordanwb2-Feb-09 8:43 
AnswerRe: Print a DataGridView Pin
Christian Graus2-Feb-09 8:52
protectorChristian Graus2-Feb-09 8:52 
GeneralRe: Print a DataGridView Pin
Jordanwb2-Feb-09 8:58
Jordanwb2-Feb-09 8:58 
GeneralRe: Print a DataGridView Pin
Duncan Edwards Jones2-Feb-09 11:41
professionalDuncan Edwards Jones2-Feb-09 11:41 
Questionpain Datagrid row in color - WinCE Pin
E_Gold2-Feb-09 8:37
E_Gold2-Feb-09 8:37 
AnswerRe: pain Datagrid row in color - WinCE Pin
EliottA2-Feb-09 8:45
EliottA2-Feb-09 8:45 
QuestionBios clock , system time control Pin
nedracix2-Feb-09 7:02
nedracix2-Feb-09 7:02 
AnswerRe: Bios clock , system time control Pin
EliottA2-Feb-09 7:08
EliottA2-Feb-09 7:08 
AnswerRe: Bios clock , system time control Pin
Wendelius2-Feb-09 7:24
mentorWendelius2-Feb-09 7:24 
AnswerRe: Bios clock , system time control Pin
Dave Kreskowiak2-Feb-09 7:36
mveDave Kreskowiak2-Feb-09 7:36 
GeneralRe: Bios clock , system time control Pin
EliottA2-Feb-09 8:02
EliottA2-Feb-09 8:02 
GeneralRe: Bios clock , system time control Pin
Dave Kreskowiak2-Feb-09 10:50
mveDave Kreskowiak2-Feb-09 10:50 
GeneralRe: Bios clock , system time control Pin
nedracix2-Feb-09 8:07
nedracix2-Feb-09 8:07 
GeneralRe: Bios clock , system time control Pin
EliottA2-Feb-09 8:36
EliottA2-Feb-09 8:36 
GeneralRe: Bios clock , system time control Pin
nedracix5-Feb-09 0:11
nedracix5-Feb-09 0:11 
QuestionCreate hotkeys for C# project Pin
Christian_Done2-Feb-09 6:08
Christian_Done2-Feb-09 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.