The code is –
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *filevar;
filevar = fopen("file", "r");
char copy [100];
int i = 0;
while(1)
{
char ch = fgetc(filevar);
if(ch==EOF)
{
break;
}
copy[i] = ch;
i++;
}
printf("\n%s", copy);
fclose(filevar);
return 0;
}
When I run it the out put I get is
textblabla■a
file content is –
textblabla
Changing the file content changes the random charecters at end
>Solution :
Two main issues.
chhas to beint- You do not null character terminate the string
int ch = fgetc(filevar);
if(ch==EOF)
{
copy[i] = 0;
break;
}
copy[i] = ch;
i++;