1

I'm making a source file that is supposed to take in 2 directory paths (one in and one out). Then copy the specified file from one directory to the other. I've completed the code, except when I compile it, it prints:

warning: passing argument 3 of ‘fread’ makes integer from pointer without a cast [enabled by default]

This is the part I'm having issues with (I think):

if (fp_in != NULL && fp_out != NULL)
{
    char    line[BUFSIZ];

    while (fread(line, sizeof line, fp_in) != NULL)
...
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
DejaVu
  • 23
  • 1
  • 6
  • Show more code please: for instance, how did you declare `fp_in`? – elnigno Oct 31 '13 at 16:32
  • Are you looking for something like: http://stackoverflow.com/questions/7021725/converting-string-to-integer-c ? – LeeNeverGup Oct 31 '13 at 16:34
  • 1
    you are missing a parameter, check the docs for [fread](http://www.manpagez.com/man/3/fread/) – A4L Oct 31 '13 at 16:34
  • First thing to do in a situation like this is, double check the docs. In this case `man fread` (with google, or at shell prompt). – hyde Oct 31 '13 at 16:34
  • fread takes 4 [parameters](http://www.cplusplus.com/reference/cstdio/fread/). You're missing the third parameter, count. Also the 2nd parameter should be `sizeof(line)` – jmstoker Oct 31 '13 at 16:38
  • 1
    Which compiler do you use? Find it strange if it in addition doesn't say something in the direction of `error: too few arguments to function ‘fread’` and `note: expected ‘size_t’ but argument is of type ‘struct FILE *’` – Runium Oct 31 '13 at 16:47
  • I used cygwin to compile the code. – DejaVu Oct 31 '13 at 17:03
  • cygwin isn't a compiler. You probably used gcc in cygwin, at which the above messages should be reported. A good compile line would be something like `gcc -Wall -Wextra -pedantic -o outfile source.c`. Optionally add `-std=c89` or `-std=c99`. (I tend to use 89 when ever I do not need 99.) – Runium Oct 31 '13 at 23:00

2 Answers2

1

fread() requires 4 arguments and you just need fgets()

Check both prototypes, if you want to read line by line, You can simply use fgets()

 while (fgets(line, sizeof line, fp_in) != NULL)
         {
         //....
         }
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
1

Change usage of fread from

fread(line, sizeof line, fp_in)

to

num_bytes = fread(line, sizeof(char), sizeof line, fp_in);

where num_bytes should be defined of type size_t. Holds the number of bytes (in this case) read from the file .


Read fread() .

While you refer docs whenever required, make it a mandatory practice that you will read the docs for the library functions you use the very first time.

smRaj
  • 1,246
  • 1
  • 9
  • 13
  • I'm pretty sure you need parentheses in `sizeof(char)`. – jwodder Oct 31 '13 at 16:46
  • `fread()` does not return `NULL` . If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). Make correction in your answer. – Gangadhar Oct 31 '13 at 16:46
  • The fgets method seems to be compiling, but I'm yet to test it, since I have to finish off the source file that hands it the correct directory path. Thanks for the answers! I really appreciate it! :D – DejaVu Oct 31 '13 at 16:58