【问题标题】:Read first and third column of numbers in a ¿binary/text? file (.GRF) in C++读取 ¿ 二进制/文本中的第一列和第三列数字? C++ 中的文件 (.GRF)
【发布时间】:2013-11-13 19:08:16
【问题描述】:

我想从文件 .GRF 中读取第一列和第三列数字。如果我用 openoffice 打开文件,我可以看到正确的值:

 0,9415818      0      0      0      5,927979      0     -2,397739      0     -5,052835     
 0,9792913      0     -1,915871E-03      0      0,032      0     -2,397739      0     -5,052835     
 1,149846      0     -1,220657E-03      0      1,229596      0     -2,397739      0     -5,052835     
 1,445044      0      4,183967E-07      0      0,2993191      0     -2,397739      0     -5,052835     
 1,825022      0      2,269486E-03      0      0,362183      0     -2,397739      0     -5,052835     
 2,320204      0      4,960121E-03      0      0,1981004      0     -2,397739      0     -5,052835     
 2,719088      0      7,585314E-03      0      0,1872567      0     -2,397739      0     -5,052835     
 3,125158      0      0,0103124      0      0,1614714      0     -0,4537098      0     -4,165784     
 3,494056      0      1,281744E-02      0      0,1635456      0     -0,4537098      0     -4,165784

[... more rows]



507,287      0      0,6060305      0      4,375222      0      219,7832      0      7,147406     
 453,6478      0      0,585957      0      4,592038      0      219,7832      0      7,147406     
 357,4476      0      0,5346767      0      5,044852      0      219,7832      0      7,147406     
 226,6828      0      0,4426026      0      5,784178      0     -211,0841      0     -4,519847     
 135,4868      0      0,3230323      0      6,631006      0     -163,2869      0     -7,731806     
 75,26476      0      0,1839096      0      7,556637      0     -78,28072      0     -8,599443     
 1,750205      0      4,814587E-02      0      8,456      0     -11,77379      0     -10,12357     
 0,1572775      0     -0,1142587      0      9,47      0     -2,074077      0     -9,702723     
 0,2199499      0     -0,2657275      0      10,343      0      3,000968E-02      0     -10,00545 

我试图读取文件的代码是这样的(之前我计算了文件的大小和行数)

    //Matrix ROWSx2 where I save the values
    double **mat = new double * [numRows]; 
    for (int i = 0; i < numRows; i++) 
       mat[i] = new double [numCol];

    double dataNumber = 00.00;
    std::ifstream fileStream(nombreFich, std::ios::in | std::ios::binary);
    fileStream.seekg (0, ios::beg);
    if (fileStream.is_open()) {
                    for(int i=0; i<numRows; i++) {
                            for(int j=0; j<numCol; j++) {
                                    fileStream.read((char*)&dataNumber, sizeof(double));  //read and move the pointer 8bytes
                                    mat[i][j] = dataNumber;
                                    fileStream.seekg (sizeof(double), ios::cur); //move 8bytes more (read third column)
                            }
                            //put the pointer at the beginning of the next row 
                            // 5 columns + 2bytes (\n=CR+LF)
                            fileStream.seekg ((sizeof(double)*5)+2, ios::cur);
                    }
                    fileStream.close();
    } else
      cout << "Error opening the file"; 

我曾尝试使用浮点数来实现,但它不起作用。我使用的是 windows64bits,我认为双精度的大小是正确的,8 字节。 主要问题是我不写文件,它是其他程序的输出,我必须用 c++ 获取第一列和第三列的值。我只知道该文件有 273 行,其中 CR+LF 和 9 列。

正如您在输出中看到的,我保存在 mat 中的值比文件值小得多:

    size of the file (20266)
    position   value        position   value
    Row:0   0    num 6.22783e-038 16   num 1.00408e-264
    Row:1   74   num 1.20267e-153 90   num 1.00482e-264
    Row:2   148  num 1.20267e-153 164  num 1.00482e-264
    Row:3   222  num 1.00506e-264 238  num 1.26392e-076
    Row:4   296  num 1.83054e-076 312  num 5.94315e-038
    Row:5   370  num 3.42701e-062 386  num 1.72238e-047
    Row:6   444  num 2.9507e-260 460   num 2.34245e-154
    Row:7   518  num 2.00661e-052 534  num 2.34247e-154
    Row:8   592  num 9.51165e-043 608  num 1.65633e-153
    Row:9   666  num 2.31282e-057 682  num 5.87605e-062
    Row:10   740  num 2.34247e-154 756  num 2.9507e-260
    Row:11   814  num 2.00359e-076 830  num 6.82018e-038
    Row:12   888  num 5.15436e-062 904  num 2.31696e-052
    Row:13   962  num 9.6363e-092 978  num 9.6363e-092
    Row:14   1036  num 1.00482e-264 1052  num 1.00604e-264
    Row:15   1110  num 1.83087e-076 1126  num 6.52861e-038
    Row:16   1184  num 7.54927e-096 1200  num 2.71478e-033
    Row:17   1258  num 1.00408e-264 1274  num 1.00408e-264
    Row:18   1332  num 1.00458e-264 1348  num 2.34246e-154
    [...]

但是...如果我完整读取文件,将所有信息保存在 char * 中,结果是这样的:

    20266buffer
    0,9415818       0       0       0       5,927979        0      -2,397739        0      -5,052835
    0,9792913       0      -1,915871E-03    0       0,032   0      -2,397739        0      -5,052835
    1,149846        0      -1,220657E-03    0       1,229596        0      -2,397739        0      -5,052835
    1,445044        0       4,183967E-07    0       0,2993191       0      -2,397739        0      -5,052835
    1,825022        0       2,269486E-03    0       0,362183        0      -2,397739        0      -5,052835
    2,320204        0       4,960121E-03    0       0,1981004       0      -2,397739        0      -5,052835
    2,719088        0       7,585314E-03    0       0,1872567       0      -2,397739        0      -5,052835
    3,125158        0       0,0103124       0       0,1614714       0      -0,4537098       0      -4,165784
    3,494056        0       1,281744E-02    0       0,1635456       0      -0,4537098       0      -4,165784
    [...more rows]
    0,2199499       0      -0,2657275       0       10,343          0       3,000968E-02    0      -10,00545
    ²²²²½½½½½½½½¯■
    File complete in memory

那么,我怎样才能正确阅读呢?只有第一列和第三列的数字 编辑:目前我正在尝试将文件作为文本文件读取。

【问题讨论】:

  • 你在 C++ 中使用callocnew 有什么问题?
  • 感谢您的回答。我在代码里改了

标签: c++ file stream binary byte


【解决方案1】:

这个文件好像不是二进制文件,而是文本文件。 所以不要像二进制文件一样读取它。 您可以逐行阅读它

while (std::getline(f, line)) {
...
}

并解析它。

【讨论】:

  • 我会逐行阅读,但我必须看看如何只选择我想要的值。谢谢,如果它有效,我会在这里写我的代码。
  • 我无法读取文件。数字是用逗号写的,我现在不知道如何阅读它们。如果我用像 12.45 这样的数字运行我的代码,它可以工作,但如果文件是原始文件,带有逗号,我的代码就不能正常工作。
  • 看看 std::locale 并灌输cplusplus.com/reference/ios/ios_base/imbue
  • 是的,昨晚我阅读了有关 locale 和 boost.locale 的信息。问题是我只看到带有 cout 的打印数字的示例。但是如何使用 std::ifstream 文件更改语言环境以读取小数;浮动 n; file.open("numbers.txt"); if(file.is_open()) { while (!file.eof()) { 文件>> n; } }
猜你喜欢
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
相关资源
最近更新 更多