正如 Lukas Koestler 在他的回答中所说的:
在
char data[fsize+1];
fsize 是未知的,因此行为是未定义的,请像 fname 一样使用固定大小。
始终保护您的 (f)scanf,限制读取的长度以留在缓冲区中
还有:
检查fopen结果。
避免使用可变数组维度,实际上你不需要data2。
说实话我不明白你为什么要在文件中写知道输入数据的大小,它是一个字符串,用strlen
您错过了在文件中写入数据的前半部分,并且 data2 不是以 null 结尾的,因此 fprintf(fp,"%s",data2); 具有未定义的行为
您没有正确管理只有 1 个字符的空数据。
你的程序可以是:
#include <stdio.h>
#include <string.h>
int main()
{
char fname[15];
char data[100];
printf("Enter the name of file you want to create: ");
scanf("%14s", fname);
printf("Enter data for that file: ");
scanf("%99s", data);
size_t sz = strlen(data);
if (sz < 2) {
puts("data too small");
return -1;
}
FILE *fp = fopen(fname, "w+");
if (fp == NULL) {
printf("cannot open %s\n", fname);
return -1;
}
size_t m = sz/2;
size_t i;
for (i = 0; i != m; ++i) {
putchar(data[i]);
fputc(data[i], fp);
}
for (i = sz - 1; i >= m; --i) {
putchar(data[i]);
fputc(data[i], fp);
}
putchar('\n');
fputc('\n', fp);
fclose(fp);
return 0;
}
编译和执行:
pi@raspberrypi:~ $ gcc -g -pedantic -Wextra f.c
pi@raspberrypi:~ $ ./a.out
Enter the name of file you want to create: aze
Enter data for that file: a
data too small
pi@raspberrypi:~ $ ./a.out
Enter the name of file you want to create: aze
Enter data for that file: az
az
pi@raspberrypi:~ $ cat aze
az
pi@raspberrypi:~ $ ./a.out
Enter the name of file you want to create: aze
Enter data for that file: aze
aez
pi@raspberrypi:~ $ cat aze
aez
pi@raspberrypi:~ $ ./a.out
Enter the name of file you want to create: aze
Enter data for that file: azerty
azeytr
pi@raspberrypi:~ $ cat aze
azeytr
pi@raspberrypi:~ $ ./a.out
Enter the name of file you want to create: aze
Enter data for that file: abcdefgh
abcdhgfe
pi@raspberrypi:~ $ cat aze
abcdhgfe
在valgrind下执行,有空就用它
pi@raspberrypi:~ $ valgrind ./a.out
==5321== Memcheck, a memory error detector
==5321== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5321== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5321== Command: ./a.out
==5321==
Enter the name of file you want to create: aze
Enter data for that file: az
az
==5321==
==5321== HEAP SUMMARY:
==5321== in use at exit: 0 bytes in 0 blocks
==5321== total heap usage: 4 allocs, 4 frees, 6,496 bytes allocated
==5321==
==5321== All heap blocks were freed -- no leaks are possible
==5321==
==5321== For counts of detected and suppressed errors, rerun with: -v
==5321== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:~ $ valgrind ./a.out
==5322== Memcheck, a memory error detector
==5322== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5322== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5322== Command: ./a.out
==5322==
Enter the name of file you want to create: aze
Enter data for that file: azertyu
azeuytr
==5322==
==5322== HEAP SUMMARY:
==5322== in use at exit: 0 bytes in 0 blocks
==5322== total heap usage: 4 allocs, 4 frees, 6,496 bytes allocated
==5322==
==5322== All heap blocks were freed -- no leaks are possible
==5322==
==5322== For counts of detected and suppressed errors, rerun with: -v
==5322== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:~ $