Search code examples
creadfilefile-copying

How to put the characters from a File in to a char string?


So here is my attempt at it but I'm getting a few errors which I don't know how to fix. 17.2 Warning : passing argument 2 of putc makes pointer from integer without a cast. C:\mingw ....... note expected Struct FILE* but' but argument is of type int.

  #include <stdio.h>
  #include <stdlib.h>

  int main (void) {
FILE *fp;
int c;
char copywords;

fp = fopen("gues20.txt", "r");
if (fp == NULL)
exit(1);

c = getc(fp);
while(c != EOF)
{
putc(c, copywords);
c = getc(fp);
}
printf("%d", copywords);
}

Solution

  • Second argument to putc is a file stream. But you pass a plain charcter. Use:

    while(c != EOF)
    {
    putc(c, stdout);
    c = getc(fp);
    }
    

    to print in stdout.