我有一个内容为:
的文件 000000000000000000000000
00000000000000000000000000000000
0000000000000000000000001f000000
01000000060000000000000034000000
3f000000000000000000000004000000
000000001b0000000900000000000000
00000000600300001000000009000000
010000000400000008000000
我想通过指定col的数量来重新排列其内容。例如,如果我说col的值为80,则输出应该是:
00000000000000000000000000000000000000000000000000000000000000000000000000000000
1f000000010000000600000000000000340000003f00000000000000000000000400000000000000
1b000000090000000000000000000000600300001000000009000000010000000400000008000000
任何人都可以帮我这个吗? xxd可以在这里服务吗?感谢。
答案 0 :(得分:6)
答案 1 :(得分:1)
由于问题也标记为C,因此这是一种C方式:
#include <stdio.h>
int main(void)
{
FILE* fp;
int c, col;
fp = fopen("datafile.txt", "r");
col = 0;
while ((c = fgetc(fp)) != EOF)
{
if (c == ' ' || c == '\n') // ignore spaces and newline
continue;
putchar(c); // output to stdout
++col;
if (col == 80) // newline at column 80
{
putchar('\n');
col = 0;
}
}
fclose(fp);
return 0;
}