c - 用 C 将字符数组写入文件(给定方法不起作用)

标签 c file-io

我试图将字符数组复制到文本文件,但它拒绝复制。我添加了一个要写入的示例文本,该文本适用于 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/

相关文章:

c - 可执行加壳程序(解压/解密 stub )

c - 如何使用 GCC 自动矢量化跨步写入?

c++ - 序列化 vector 的 vector 结构

java - 从文件开头跳过字符

php - 开源语言识别库?

c - 加速大型开关和 if-else

java - 奇怪的 file.nextLine() 行为

Java - 将 jar 中的 dll 文件写入硬盘?

javascript - 在nodejs中使用writeFile时未创建文件

c++ - C/C++ 代码的等效 asm 代码