本文介绍了如何使用fwrite将带填充的字符串写入二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是将"32字节的字符串表示压力单位"写入二进制文件.我要写入的压力单位是"Pa"(32字节字符串).这是我的尝试.

#include <stdio.h>
#include <string>

using namespace std;

int main()
{
  FILE *myFile;
  myFile = fopen ("input_file.dat", "wb");
  //Units
  string units = "Pa";
  //Write Units
  fwrite (&units, 1, 32, myFile);
  fclose (myFile);
  return 0;
}

我期望(将Pa转换为32字节的二进制文件). "00100000"是空格.如何将这些附加到Pa上?

01010000 01100001 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000

但是,我看到了

解决方案

字符串"Pa"只有2个字节(如果计算空终止符,则为3个字节),但是您想向文件中写出32个字节.因此,您需要使用30个空格字符填充字符串.您可以通过多种方法处理此问题:

 char units[32] = "Pa";
memset(units+2, ' ', 30);
fwrite (units, 1, 32, myFile);
 
 std::string units = "Pa";
std::string padding(32-units.size(), ' ');
fwrite (units.c_str(), 1, units.size(), myFile);
fwrite (padding.c_str(), 1, padding.size(), myFile);
 
 std::string units = "Pa";
std::ostringstream oss;
oss << std::setw(32) << std::setfill(' ') << std::left << units;
std::string s = oss.str();
fwrite (s.c_str(), 1, s.size(), myFile);
 

话虽如此,由于您使用的是C ++,因此应该使用C ++样式文件I/O而不是C样式文件I/O,例如:

 #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main() {
    ofstream myFile ("input_file.dat", ios::binary);
    string units = "Pa";
    myFile << setw(32) << setfill(' ') << left << units;
    return 0;
}
 

My requirement is to write a "32-byte string indicating the units of pressure" to a binary file. The units of pressure I am wanting to write is "Pa" as a 32-byte string. And here is my attempt.

#include <stdio.h>
#include <string>

using namespace std;

int main()
{
  FILE *myFile;
  myFile = fopen ("input_file.dat", "wb");
  //Units
  string units = "Pa";
  //Write Units
  fwrite (&units, 1, 32, myFile);
  fclose (myFile);
  return 0;
}

I'm expecting (conversion of Pa to binary in 32 bytes). The "00100000" are spaces. How do I append those to just Pa?

01010000 01100001 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000 00100000

However, I'm seeing

解决方案

The string "Pa" is only 2 bytes (3 if you count the null terminator), but you want to write out 32 bytes to your file. So you need to pad the string with 30 space characters. There are many different ways you can handle this:

char units[32] = "Pa";
memset(units+2, ' ', 30);
fwrite (units, 1, 32, myFile);
std::string units = "Pa";
std::string padding(32-units.size(), ' ');
fwrite (units.c_str(), 1, units.size(), myFile);
fwrite (padding.c_str(), 1, padding.size(), myFile);
std::string units = "Pa";
std::ostringstream oss;
oss << std::setw(32) << std::setfill(' ') << std::left << units;
std::string s = oss.str();
fwrite (s.c_str(), 1, s.size(), myFile);

That being said, since you are using C++, you should be using C++ style file I/O instead of C style file I/O, eg:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main() {
    ofstream myFile ("input_file.dat", ios::binary);
    string units = "Pa";
    myFile << setw(32) << setfill(' ') << left << units;
    return 0;
}

这篇关于如何使用fwrite将带填充的字符串写入二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 08:10