#include <stdio.h>
FILE *fl;
char content[BUFSIZ];
int main() {
int i;
fl = fopen ("data.txt", "rt");
content = fgetc(fl);
for (i=0;i <= sizeof(content); i++ ){
printf("%c",content[i]);
}
fclose(fl);
}
I’m trying to open a file and put the content of the text file into content[] array. But when I tried to compile I get the following errors.
y:~/homework1: gcc -o hw1_2 hw1_2.c
hw1_2.c: In function 'main':
hw1_2.c:11:11: error: incompatible types when assigning to type 'char[1024]' from type 'int'
The function
fgetcreads a single character and returns it. So you’re trying to assign a single integer to an array which isn’t going to work.You should be using
freadfor this.However, since you tagged it homework, you may want something like this: