Reading integers from a text file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reading integers from a text file

data_addedata_adde Posts: 5Member
Hi!
I am new to C# and hope for little help with a problem I have.

I have a text file with the content below:
970304065
020506080
000000000
005802900
002040300
008705100
000000000
060208030
840109027

I have this code:
StreamReader laes = new StreamReader("setup.txt");

while(true)
{
int x = laes.Read();
if(x == -1) break;
Console.Read();
Console.WriteLine(x);
}

The result of this is that the numbers are printed out like this:
57
55

48
51
and so on...

This is the ascii code (decimal) of the integers in my file.
I want to print the actual numbers in the file like:
9
7
0
3
0
4
and so on...

Anyone that know how to do this ?

Regards Adam



Comments

  • IDKIDK Posts: 1,784Member
    : Hi!
    : I am new to C# and hope for little help with a problem I have.
    :
    : I have a text file with the content below:
    : 970304065
    : 020506080
    : 000000000
    : 005802900
    : 002040300
    : 008705100
    : 000000000
    : 060208030
    : 840109027
    :
    : I have this code:
    : StreamReader laes = new StreamReader("setup.txt");
    :
    : while(true)
    : {
    : int x = laes.Read();
    : if(x == -1) break;
    : Console.Read();
    : Console.WriteLine(x);
    : }
    :
    : The result of this is that the numbers are printed out like this:
    : 57
    : 55
    :
    : 48
    : 51
    : and so on...
    :
    : This is the ascii code (decimal) of the integers in my file.
    : I want to print the actual numbers in the file like:
    : 9
    : 7
    : 0
    : 3
    : 0
    : 4
    : and so on...
    :
    : Anyone that know how to do this ?
    :
    : Regards Adam
    :
    :
    :
    :
    this is code written from head:

    [code]
    string output ="";

    for(int i = 0; i< input.length;i+=2)
    {
    string d = (char)input.readByte() + (char)input.readByte();
    char c = char.parse(d);
    output += c;
    }
    [/code]

    Happy coding wishes
    the one and only
    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • data_addedata_adde Posts: 5Member
    [b][red]This message was edited by data_adde at 2006-3-12 6:28:34[/red][/b][hr]
    Thanks Niklas.

    What I want to do is to read every single integer (0-9) from
    the file and put those in an integer array.

    Thanks


    : : Hi!
    : : I am new to C# and hope for little help with a problem I have.
    : :
    : : I have a text file with the content below:
    : : 970304065
    : : 020506080
    : : 000000000
    : : 005802900
    : : 002040300
    : : 008705100
    : : 000000000
    : : 060208030
    : : 840109027
    : :
    : : I have this code:
    : : [code]StreamReader laes = new StreamReader("setup.txt");
    : :
    : : while(true)
    : : {
    : : int x = laes.Read();
    : : if(x == -1) break;
    : : Console.Read();
    : : Console.WriteLine(x);
    : : }[/code]
    : :
    : : The result of this is that the numbers are printed out like this:
    : : 57
    : : 55
    : :
    : : 48
    : : 51
    : : and so on...
    : :
    : : This is the ascii code (decimal) of the integers in my file.
    : : I want to print the actual numbers in the file like:
    : : 9
    : : 7
    : : 0
    : : 3
    : : 0
    : : 4
    : : and so on...
    : :
    : : Anyone that know how to do this ?
    : :
    : : Regards Adam
    : :
    : :
    : :
    : :
    : this is code written from head:
    :
    : [code]
    : string output ="";
    :
    : for(int i = 0; i< input.length;i+=2)
    : {
    : string d = (char)input.readByte() + (char)input.readByte();
    : char c = char.parse(d);
    : output += c;
    : }
    : [/code]
    :
    : Happy coding wishes
    : the one and only
    : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    :
    :



  • IDKIDK Posts: 1,784Member
    : [b][red]This message was edited by data_adde at 2006-3-12 6:28:34[/red][/b][hr]
    : Thanks Niklas.
    :
    : What I want to do is to read every single integer (0-9) from
    : the file and put those in an integer array.
    :
    : Thanks
    :

    OK, then do like this:
    (also written from head...)
    [code]
    int[] array = new int[input.Length];

    for(int i = 0; i < input.Lengt; ++i){
    array[i] = int.parse(input.readbyte());
    }
    [/code]
  • data_addedata_adde Posts: 5Member
    Thanks again.
    I tried this, but does not fix it:

    [code]
    FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    BinaryReader r = new BinaryReader(fs);

    for( int i=0; i<3; i++)
    {
    int x = r.ReadByte();
    Console.WriteLine(x);
    }
    Console.Read();
    [/code]
    The file setup2.txt contains "123".
    What I want is to save the actual integer '1','2' or '3' in an
    integer variable like here x.
    The output of this is
    49
    50
    51

    That is x is not cointaining 1,2 or 3 but the ascii codes for their char values..

    However if I change the code to this:
    [code]
    FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    BinaryReader r = new BinaryReader(fs);

    for( int i=0; i<3; i++)
    {
    char x = r.ReadChar();
    Console.WriteLine(x);
    }
    Console.Read();
    [/code]

    The printout gets:
    1
    2
    3

    But now it is in char, I want the content of the variable x to be in,
    because I will use this value in some other functions.
    I know you can use int.Parse() but I just get error messages.

    The thing is that I am doing a Sudoku solver app.
    So far I have put in the starting numbers manually/hardcoded
    in a 9x9 int array.
    But now I want to read the starting NUMBERS from a file (for example setup.txt) and copy those to the 9x9 array.

    Example of the content of setup.txt:
    0000056789
    1004000090
    2000078001
    3000009012
    4000890123
    5000001234
    6789012340
    7000123456
    0000034567
    9012000078

    Where 0 means empty cell...

    Thanks for your help














    : : [b][red]This message was edited by data_adde at 2006-3-12 6:28:34[/red][/b][hr]
    : : Thanks Niklas.
    : :
    : : What I want to do is to read every single integer (0-9) from
    : : the file and put those in an integer array.
    : :
    : : Thanks
    : :
    :
    : OK, then do like this:
    : (also written from head...)
    : [code]
    : int[] array = new int[input.Length];
    :
    : for(int i = 0; i < input.Lengt; ++i){
    : array[i] = int.parse(input.readbyte());
    : }
    : [/code]
    :



  • IDKIDK Posts: 1,784Member
    : Thanks again.
    : I tried this, but does not fix it:
    :
    : [code]
    : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : BinaryReader r = new BinaryReader(fs);
    :
    : for( int i=0; i<3; i++)
    : {
    : int x = r.ReadByte();
    : Console.WriteLine(x);
    : }
    : Console.Read();
    : [/code]
    : The file setup2.txt contains "123".
    : What I want is to save the actual integer '1','2' or '3' in an
    : integer variable like here x.
    : The output of this is
    : 49
    : 50
    : 51
    :
    : That is x is not cointaining 1,2 or 3 but the ascii codes for their char values..
    :
    : However if I change the code to this:
    : [code]
    : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : BinaryReader r = new BinaryReader(fs);
    :
    : for( int i=0; i<3; i++)
    : {
    : char x = r.ReadChar();
    : Console.WriteLine(x);
    : }
    : Console.Read();
    : [/code]
    :
    : The printout gets:
    : 1
    : 2
    : 3
    :
    : But now it is in char, I want the content of the variable x to be in,
    : because I will use this value in some other functions.
    : I know you can use int.Parse() but I just get error messages.
    :
    : The thing is that I am doing a Sudoku solver app.
    : So far I have put in the starting numbers manually/hardcoded
    : in a 9x9 int array.
    : But now I want to read the starting NUMBERS from a file (for example setup.txt) and copy those to the 9x9 array.
    :

    Simply do as I said, use int.parse() and it will work as you want:
    int x = int.Parse(r.ReadByte());
  • data_addedata_adde Posts: 5Member
    : : Thanks again.
    : : I tried this, but does not fix it:
    : :
    : : [code]
    : : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : : BinaryReader r = new BinaryReader(fs);
    : :
    : : for( int i=0; i<3; i++)
    : : {
    : : int x = r.ReadByte();
    : : Console.WriteLine(x);
    : : }
    : : Console.Read();
    : : [/code]
    : : The file setup2.txt contains "123".
    : : What I want is to save the actual integer '1','2' or '3' in an
    : : integer variable like here x.
    : : The output of this is
    : : 49
    : : 50
    : : 51
    : :
    : : That is x is not cointaining 1,2 or 3 but the ascii codes for their char values..
    : :
    : : However if I change the code to this:
    : : [code]
    : : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : : BinaryReader r = new BinaryReader(fs);
    : :
    : : for( int i=0; i<3; i++)
    : : {
    : : char x = r.ReadChar();
    : : Console.WriteLine(x);
    : : }
    : : Console.Read();
    : : [/code]
    : :
    : : The printout gets:
    : : 1
    : : 2
    : : 3
    : :
    : : But now it is in char, I want the content of the variable x to be in,
    : : because I will use this value in some other functions.
    : : I know you can use int.Parse() but I just get error messages.
    : :
    : : The thing is that I am doing a Sudoku solver app.
    : : So far I have put in the starting numbers manually/hardcoded
    : : in a 9x9 int array.
    : : But now I want to read the starting NUMBERS from a file (for example setup.txt) and copy those to the 9x9 array.
    : :
    :
    : Simply do as I said, use int.parse() and it will work as you want:
    : int x = int.Parse(r.ReadByte());
    :

    Hi Niklas.
    I changed the code to the following:
    [code]
    FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    BinaryReader r = new BinaryReader(fs);

    for( int i=0; i<3; i++)
    {
    //char x = r.ReadChar();
    int x = int.Parse(r.ReadByte());
    Console.WriteLine(x);
    }
    Console.Read();
    [/code]

    I get an error message when using int.Parse();
    [code]
    SudFuncs121.cs(19,18): error CS1502: The best overloaded method match for 'int.Parse(string)' has some invalid arguments
    SudFuncs121.cs(19,28): error CS1503: Argument '1': cannot convert from 'byte' to 'string'[/code]
  • data_addedata_adde Posts: 5Member
    : : : Thanks again.
    : : : I tried this, but does not fix it:
    : : :
    : : : [code]
    : : : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : : : BinaryReader r = new BinaryReader(fs);
    : : :
    : : : for( int i=0; i<3; i++)
    : : : {
    : : : int x = r.ReadByte();
    : : : Console.WriteLine(x);
    : : : }
    : : : Console.Read();
    : : : [/code]
    : : : The file setup2.txt contains "123".
    : : : What I want is to save the actual integer '1','2' or '3' in an
    : : : integer variable like here x.
    : : : The output of this is
    : : : 49
    : : : 50
    : : : 51
    : : :
    : : : That is x is not cointaining 1,2 or 3 but the ascii codes for their char values..
    : : :
    : : : However if I change the code to this:
    : : : [code]
    : : : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : : : BinaryReader r = new BinaryReader(fs);
    : : :
    : : : for( int i=0; i<3; i++)
    : : : {
    : : : char x = r.ReadChar();
    : : : Console.WriteLine(x);
    : : : }
    : : : Console.Read();
    : : : [/code]
    : : :
    : : : The printout gets:
    : : : 1
    : : : 2
    : : : 3
    : : :
    : : : But now it is in char, I want the content of the variable x to be in,
    : : : because I will use this value in some other functions.
    : : : I know you can use int.Parse() but I just get error messages.
    : : :
    : : : The thing is that I am doing a Sudoku solver app.
    : : : So far I have put in the starting numbers manually/hardcoded
    : : : in a 9x9 int array.
    : : : But now I want to read the starting NUMBERS from a file (for example setup.txt) and copy those to the 9x9 array.
    : : :
    : :
    : : Simply do as I said, use int.parse() and it will work as you want:
    : : int x = int.Parse(r.ReadByte());
    : :
    :
    : Hi Niklas.
    : I changed the code to the following:
    : [code]
    : FileStream fs = new FileStream("setup2.txt", FileMode.Open, FileAccess.Read);
    : BinaryReader r = new BinaryReader(fs);
    :
    : for( int i=0; i<3; i++)
    : {
    : //char x = r.ReadChar();
    : int x = int.Parse(r.ReadByte());
    : Console.WriteLine(x);
    : }
    : Console.Read();
    : [/code]
    :
    : I get an error message when using int.Parse();
    : [code]
    : SudFuncs121.cs(19,18): error CS1502: The best overloaded method match for 'int.Parse(string)' has some invalid arguments
    : SudFuncs121.cs(19,28): error CS1503: Argument '1': cannot convert from 'byte' to 'string'[/code]
    :

    Thanks for your time and help.
    I finally fixed it by:
    [code]int x = int.Parse(r.ReadChar().ToString());[/code]




Sign In or Register to comment.