-1

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

mch
  • 9,424
  • 2
  • 28
  • 42
Arjun
  • 3
  • 1

1 Answers1

0

Two main issues.

  1. ch has to be int
  2. You do not null character terminate the string
        int ch = fgetc(filevar);
        if(ch==EOF)
        {
            copy[i] = 0;
            break;
        }
        
        copy[i] = ch;
        i++;
0___________
  • 60,014
  • 4
  • 34
  • 74