简体   繁体   English

从\\到缓冲区读取/写入文件

[英]Reading/Writing files from\to Buffers

Simply I have a text file which contains binary numbers.只是我有一个包含二进制数的文本文件。 I need to read these numbers (without converting to integers or ASCII) and store them properly in an unsigned char Buf.我需要读取这些数字(不转换为整数或 ASCII)并将它们正确存储在无符号字符 Buf 中。 After processing these data on an electronic board, a method reads the output from the board and store it in a Buf as well.在电子板上处理这些数据后,一种方法从板上读取输出并将其存储在 Buf 中。 So, I also need to write the data from the Buf to an output file.因此,我还需要将 Buf 中的数据写入输出文件。

first, I have doubt about the size of the buffer.首先,我怀疑缓冲区的大小。 The declaration is as shown bellow:声明如下所示:

#define Buff_size (long) 255;
unsigned char buf[Buff_size];
  • What does 255 represent here? 255在这里代表什么? bits, bytes or words of unsigned char?位、字节或无符号字符的字?

  • My data size is: 2048 (number) * 32 (bits) = 65536 bits.我的数据大小是:2048(数字)* 32(位)= 65536 位。 However, the size of the input data file is 68 KB.但是,输入数据文件的大小为 68 KB。 Which size is actually the data size and I should use as the buffer size?哪个大小实际上是数据大小,我应该用作缓冲区大小?

I have tried some codes, and I can run the full code with no errors.我已经尝试了一些代码,我可以运行完整的代码而没有错误。 however, when I print out the buffer I get incorrect data.但是,当我打印出缓冲区时,我得到了不正确的数据。 This is the declaration of the input\\output files:这是输入\\输出文件的声明:

// Open input and output files.
f_in.open(infilename, std::ios::out);
if (false == f_in.is_open()) {
    printf("Error: Input file could not be opened.\n");
    return(false);
}
f_out.open(outfilename, std::ios::in);
if (false == f_out.is_open()) {
    printf("Error: Output file could not be opened.\n");
    return(false);
}

I tried to read the input data from the input file f_in using the two following commands:我尝试使用以下两个命令从输入文件 f_in 中读取输入数据:

1) f_in.read((char*)buf, Buff_size);
2) //f_in >> buf;

When I tried to read the buf as: cout << buf << endl;当我尝试将 buf 读为: cout << buf << endl; I got the correct data with some dummy data at the end using the first reading command.我使用第一个读取命令在最后获得了带有一些虚拟数据的正确数据。 I'm assuming the size of the buffer is not approperate so I get these dummy data (very strange random characters).我假设缓冲区的大小不合适,所以我得到了这些虚拟数据(非常奇怪的随机字符)。 When I use the second command I get only first line of the input data file is printed out.当我使用第二个命令时,我只打印出输入数据文件的第一行。

To summarise I need to read some data from a text file and store it in a buffer.总而言之,我需要从文本文件中读取一些数据并将其存储在缓冲区中。 Then write the output data line by line into an output file.然后将输出数据逐行写入输出文件。

The bits are clearly written as ASCII: you'll want to read them formatted line by line and convert them to you unsigned char s by decoding them individually.这些位清楚地写为 ASCII:您需要逐行读取它们的格式,并通过单独解码它们将它们转换为unsigned char s。 For example:例如:

for (std::string line; getline(in, line); ) {
    if (!std::all_of(line.begin(), line.end(),
                     [](char c){ return c == ‘0’ || c == ‘1’; })) {
        // deal with unexpected line
        continue;
    }
    for (int offset = 0; offset + 8 <= line.size(); offset += 8) {
        unsigned char c = std::bitset<8>(line.substr(offset, 8)).value();
        // do something with c
    }
}

The declarations you wondered about are primarily a display of not understanding C++: there is no place in C++ to define constants using #define .您想知道的声明主要是不理解 C++ 的表现:C++ 中没有使用#define定义常量的地方。 The semantic of the number clearly depends on how it is being used.数字的语义显然取决于它的使用方式。 As it is used on the line just following it it should be straight forward to determine what it is used for.由于它用于紧随其后的线路,因此应该直接确定它的用途。 Of course, there is also little place in C++ for fixed size arrays...当然,C++ 中固定大小的数组也没有什么地方...

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

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