Linux 将 n 字节的数据 x 复制到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8262758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:33:34  来源:igfitidea点击:

Copy n bytes of data x to file

linuxdd

提问by Mike Egren

How we can copy for example 10 bytes of '7' to a file?

我们如何将例如 10 个字节的 '7' 复制到文件中?

How can I generate those 10 bytes of 7?

我怎样才能生成那 10 个字节的 7?

For example for n bytes of zero I'm doing dd if=/dev/zero of=myFile bs=1 count=10.

例如,对于我正在做的 n 个零字节dd if=/dev/zero of=myFile bs=1 count=10

采纳答案by mokalan

You can send the zeros to stdout and translate them to 7, or what ever you like.

您可以将零发送到标准输出并将它们转换为 7,或者您喜欢的任何值。

dd if=/dev/zero bs=1 count=10 | tr "
#include<stdio.h>

#define MY_FILE "7";

char my_data[] = {
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa
};

int
main (int argc, char *argv[])
{  
  FILE *fp = open (MY_FILE, "wb");
  if (!fp) {
    perror ("File open error!");
    return 1;
  }
  fwrite (my_data, sizeof (my_data), fp);
  fclose (fp);
  return 0;
}
" "" > file.bin

回答by paulsm4

Q: How we can copy for example 10 bytes of '7' to a file?

问:我们如何将例如 10 个字节的 '7' 复制到文件中?

A: "dd" is certainly on option. One of many :)

答:“dd”当然是可以选择的。其中之一 :)

How can I generate those 10 bytes of 7?

我怎样才能生成那 10 个字节的 7?

A: However you want. For example, you can write a C program:

答:随便你。例如,您可以编写一个 C 程序:

echo 7777777777 | dd of=myFile bs=1 count=10

回答by xmoex

redirect an echooutput to dd

echo输出重定向到dd

echo -e '\x7\x7\x7\x7\x7\x7\x7\x7\x7\x7' | dd of=myFile bs=1 count=10

or

或者

##代码##

if you need the binary representation of 7

如果你需要 7 的二进制表示