简体   繁体   English

读取C ++中的文本文件

[英]Read a text file in C++

I am working in Ubuntu on OpenCV. 我在Ubuntu上的OpenCV上工作。 I am trying to read a text file which contains numbers, but I keep getting garbage values and the same value repeats every time the function loops. 我试图读取一个包含数字的文本文件,但是我不断获取垃圾值,并且每次函数循环时都重复相同的值。 Here is that part of the code: 这是代码的一部分:

FILE* fid = fopen("/trial","r");
while (fgetc(fid) != EOF)
{
    fscanf(fid, "%f", &trainsample);
    cout << trainsample << endl;
    cvSetReal2D(traindata, i, j, trainsample);
    j = j + 1;
    if (j == 6)
        i = i + 1;
}

Why don't you use C++ ifstream for this task? 您为什么不使用C ++ ifstream来完成此任务?

#include <iostream>
#include <fstream>

int main(){
    std::ifstream fileStream("/trail");
    double trainsample;
    if(!fileStream.good()){
        std::cerr << "Could not open file." << std::endl;
        return 1;
    }
    while(fileStream >> trainsample){           
        std::cout << trainsample << std::endl;
    }
    if(fileStream.fail())
        std::cerr << "Input file stream error bit is set, possible read error on file." << std::endl;               
}

If you prefer C file handling try 如果您喜欢C文件处理,请尝试

#include <cstdio>

int main(){
    FILE *fid = fopen("/trail","r");
    double trainsample;
    if(fid){
        while(!feof(fid)){
            fscanf(fid,"%lf",&trainsample); // Please notice "lf" when using double. Using "f" will result garbage.
            printf("%lf\n",trainsample);
        }
    }
}

See also cstdio and ifstream . 另请参见cstdioifstream

fgetc() reads a character from a file. fgetc()从文件中读取字符。 It is equivalent to getc. 它等效于getc。 You can use !feof(fid) as your condition instead. 您可以使用!feof(fid)作为条件。 The garbage values are because you are not at the correct position while reading the float values from the file and as a result, other characters/special characters are influencing the values being read in by C. 垃圾值是因为在从文件读取浮点值时您不在正确的位置,因此,其他字符/特殊字符正在影响C读取的值。

Is there any particular reason you're using C-style I/O routines instead of C++ stream operators? 您使用C样式I / O例程而不是C ++流运算符有什么特殊原因吗?

#include <iostream>
#include <stdexcept>
#include <fstream>
...
std::ifstream fid("/train");
if (!fid.good())
    // could not open file, panic here
else
{
  float trainsample;
  while (fid >> trainsample)
  {
    std::cout << trainsample << std::endl;
    ...
  }
  if (fid.eof())
    std::cout << "Hit end of file" << std::endl;
  else if (fid.fail())
    std::cout << "Read error on file" << std::endl;
}

ASSUMPTION: 假设:

  • The /trail file contains only one float value per line /trail文件每行仅包含一个浮点值

Having said the above, you don't need the while loop with fgetc() , instead you need the following 上面已经说了,您不需要使用fgetc()while循环,而是需要以下内容

while (fscanf(fid,"%f",&trainsample) == 1) {
    cout<<trainsample<<endl;
    /* you can put all you logic here */

}

我的猜测是fopen失败,因此fscanf失败(不读取任何内容),而实际上您获得的垃圾值是trainsample变量的未初始化内容,当然,该内容永远不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM