As it what said by Lukas Koestler in his answer :
in
char data[fsize+1];
fsize is unknown, so the behavior is undefined, use a fixed size like you do for the fname.
Always protect your (f)scanf limiting the length read to stay in the buffer
But also :
Check fopen result.
Avoid the use of variable array dimension, and in fact you do not need data2.
To be frank I do not understand why you write in the file to know the size of the input data, it is a string, just use strlen
You missed to write the first half of the data in the file, and data2 is not null terminated so fprintf(fp,"%s",data2);
has an undefined behavior
You do not manage properly empty data of data having only 1 char.
Your program can be :
#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;
}
Compilation and execution :
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
Execution under valgrind, always use it it when you can
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:~ $