简体   繁体   English

C ++ /读取数据文件

[英]C++/Reading a data file

I had some issues with reading a data file(not a txt file) in c++. 我在用c ++读取数据文件(不是txt文件)时遇到一些问题。 My code looks like this 我的代码看起来像这样

   path.append("/");
   path.append(name);
   path.append("/stat");
   FILE * pFile;
   const char *c = path.c_str();
     long lSize;
     char * buffer;
     size_t result;

     pFile = fopen ( c , "rb" );
     if (pFile==NULL) {fputs (c ,stderr);std::cout<<"Error"<<std::cout; exit (1);}
     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);
     // allocate memory to contain the whole file:
     buffer = (char*) malloc (sizeof(char)*lSize);
     if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

     // copy the file into the buffer:
     result = fread (buffer,1,lSize,pFile);
     if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

     /* the whole file is now loaded in the memory buffer. */

     // terminate
     fclose (pFile);
     return buffer;

As a result i get "ài· " instead of the wished string array. 结果,我得到“ài·”而不是希望的字符串数组。 I think it is some kind of an encoding error. 我认为这是某种编码错误。 To output my code I use fwrite to write it in a new txt file 为了输出我的代码,我使用fwrite将其写入新的txt文件中

catStat[0] is the previous result catStat [0]是上一个结果

     int i = 5;
     FILE * pFile;
     const char * cat=catStat[0].c_str();
     pFile = fopen ("/root/list.txt", "a");
     if(tdi!="")
     {
        fwrite (cat , sizeof(char), sizeof(cat), pFile);
     }
     else
     {
        fwrite(cat,sizeof(char),sizeof(cat),pFile);
        while(i<10){
           cat=catStat[i].c_str();
           fwrite (cat , sizeof(char), sizeof(cat), pFile);
           i++;

        }

If i open the file with an editor or do cat stat in the console i get: 如果我使用编辑器打开文件或在控制台中执行cat stat,则会得到:

29273 (bash) S 2556 29273 2556 1025 29273 4202752 1367 3281 1 1 12 4 2 1 20 0 1 0 49096474 3997696 639 4294967295 134512640 135304128 3217720544 3217717576 3077456932 0 0 3686404 1266761467 3240737915 0 0 17 0 0 0 0 0 0 29273(bash)S 2556 29273 2556 1025 29273 4202752 1367 3281 1 1 12 4 2 1 20 0 1 0 49096474 3997696 639 4294967295 134512640 135304128 3217720544 3217717576 3077456932 0 0 3686404 1266761467 3240737915 0 0 17 0 0 0 0 0 0 0

Thanks in advance, 提前致谢,

Laurenz Laurenz

I don't know how you get that output, but I'm guessing you are using printf. 我不知道您如何获得该输出,但是我猜您正在使用printf。

Considering that you loaded the file as binary, when printing the byte array using printf("%s") the string search will stop at the first '\\0' encountered. 考虑到您已将文件加载为二进制文件,因此在使用printf(“%s”)打印字节数组时,字符串搜索将在遇到的第一个'\\ 0'处停止。

You cannot print a binary as a string, you need to write it down byte by byte. 您无法将二进制文件打印为字符串,需要逐字节记录下来。

The line fwrite (cat , sizeof(char), sizeof(cat), pFile); fwrite (cat , sizeof(char), sizeof(cat), pFile); only writes sizeof(cat) chars, which is the size of the const char pointer. 仅写入sizeof(cat)字符,这是const char指针的大小。 You can't use sizeof to get the size of a dynamically allocated c-string. 您不能使用sizeof来获取动态分配的c字符串的大小。 You should replace that with catStat[0].size() . 您应该将其替换为catStat[0].size()

Also, you should probably learn to code the entire program with c++ constructs instead of c because of caveats like this. 同样,由于这样的警告,您可能应该学习使用c ++构造而不是c编码整个程序。

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

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