我试图将字符数组复制到文本文件,但它拒绝复制。我添加了一个要写入的示例文本,该文本适用于 wine,所以问题可能出在数组 payload[256]
上?
(char Payload[256]
是我尝试复制的数组):
printf("new value of payload-----> \n");
for (int x2 = 0; x2 < 256; x2++) //PRINTING THE ARRAY. IT IS FILLED UP
{
printf("%x",payload[x2]);
//if ((x2 + 1) % 16 == 0) printf("\n");
}
getchar();
FILE *fptr= fopen("program.txt", "w");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
const char*p = payload;
fprintf(fptr, "%s", p); //DOESNT PRINT
fprintf(fptr, "%s", &payload[0]); //DOESNT PRINT
fprintf(fptr, "%s", payload); //DOESNT PRINT
const char *text = "Write this to the file";
fprintf(fptr, "Some text: %s\n", text); //WORKS FINE
int results = fputs(payload, fptr); //DOESNT WORK
fclose(fptr);
请帮忙。
最佳答案
不要使用fprintf(fptr, "%s", p);
,试试这个:
for (int x2 = 0; x2 < 256; x2++)
{
fprintf(fptr,"%x",payload[x2]);
}
正如其他人所说,%s 用于可显示字符的 NULL 终止字符串。看来你没有这个。
关于c - 用 C 将字符数组写入文件(给定方法不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294305/