Troubles with "cout << (float) iFile.get() << " ";"

Hi,

I am reading an ASCII file. The file contain a matrix of values like

11.33 12.33 13.33
14.33 15.33 16.33
17.33 18.33 19.33

When I use
 
cout << (float) iFile.get() << " ";

to output the values, it give me

-1 -1 -1
-1 -1 -1
-1 -1 -1

That's strange. I tried (double), but it does not work too.

Any help will be apprecited.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <istream>
#include <sstream>

using namespace std;

int matrix_lines(istream &iFile) {
// Método para obter o número linhas da matriz.
    int lines = 0;
    string line;
    while(!iFile.eof()) {
        getline (iFile, line);
        lines++;
    }
    cout << "Número de linhas: " << lines << endl;
    return lines;
}

int matrix_columns(istream &iFile) {
// Método para obter o número de colunas da matriz.
    string line;
    double n;
    int cols = 0;

    getline (iFile, line);
//    cout << line << endl;

    stringstream is(line);
    n = 0;
    while (is >> n) {
//        cout << n << endl;
        cols++;
    }


//     
    cout << "Número de colunas: " << cols << endl;
    return cols;
}

void matrix_read(istream &iFile, int lines, int cols) {
// Método para ler a matriz.
    float value;
    iFile.seekg(0);
    for(int i = 0; i < lines; i++){
        for(int j = 0; j < cols; j++){
            cout << (float) iFile.get() << " ";
        }
        cout << "\n";
    }
}

int main(int argc, char**argv) {

//    cout << "Using file " << argv[ 1 ] << endl;

    ifstream iFile;
    iFile.open("/home/cristiano/matrix.dat");

    if(!iFile) { // file couldn't be opened
        cerr << "Error: file could not be opened" << endl;
        exit(1);
    }

    int cols = matrix_columns(iFile);
    int lines = matrix_lines(iFile);
    matrix_read(iFile, lines, cols);


    iFile.close();

    return 0;
}
Last edited on
iFile.get() returns a character
eg:
your file: 123.456
your code:cout << (float) iFile.get() << " ";
your output:49
meaning:
1
2
iFile.get() -> returns '1'
(float) '1' -> converts ASCII '1' to 49.0f

-1 values may mean end of file
It will be good if it was possible to do

 
iFile.seekg(0);


to go to the begin of the stream. But is not so easy, I made a try(below) but it does not work

1
2
3
4
5
6
7
8
9
10
11
void matrix_read(istream &iFile, int lines, int cols) {
// Método para ler a matriz.
    iFile.seekg(0);
    float value;
    for(int i = 0; i < lines; i++){
        for(int j = 0; j < cols; j++){
            cout << iFile.get() << " ";
        }
        cout << "\n";
    }
}


There is an easy way to go back to the begin of the stream?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
void matrix_read(istream &iFile, int lines, int cols) {
// Método para ler a matriz.
    float value;
    iFile.clear(); //Try this here <<=====
    iFile.seekg(0);

    for(int i = 0; i < lines; i++){
        for(int j = 0; j < cols; j++){
            cout <<  iFile.get() << " ";
        }
        cout << "\n";
    }
}
Thanks guestgulkan,

I think it works. I want to say, now it was not outputing

-1 -1 -1
-1 -1 -1
-1 -1 -1

but is outputing

49 49 46
51 51 32
49 50 46

The file contents are double values like this below

11.33 12.33 13.33
14.33 15.33 16.33
17.33 18.33 19.33

this is what I want to recover.

Can you think in some way for doing this?
impress your friends with your knowledge of C++ operators,
and do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void matrix_read(istream &iFile, int lines, int cols) {
// Método para ler a matriz.
    float value;
    iFile.clear(); 
    iFile.seekg(0);

    for(int i = 0; i < lines; i++){
        for(int j = 0; j < cols; j++){
            cout <<  (iFile >> value,value)  << " ";
        }
        cout << "\n";
    }
}
Last edited on
You will notice that you have one more line than you need (and it consists of the last number repeated).
This is due to the way you are calculating the number of lines in the file (in the matrix_lines function. )
Just checking for end of file is not enough.
Mission accomplished guestgulkan,

Special thanks for your solution, It works in simple way, that's perfect!

Topic archived. No new replies allowed.