By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

warning: passing arg 1 of `fopen' makes pointer from integer without a cast

P: 5
Hello,

Here is a program I'm playing around with for fun in the process of learning C. The objective is to create a function
Expand|Select|Wrap|Line Numbers
  1. filesize()
and call it from within the
Expand|Select|Wrap|Line Numbers
  1. main()
section to retrieve the size in bytes of a file that I give it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. long filesize(f)
  4. {
  5.         FILE *fp;
  6.         if((fp = fopen(f, "r")) == NULL)
  7.         {
  8.                 printf("unable to open %s\n", f);
  9.                 exit(1);
  10.         }
  11.         fseek(fp, 0, SEEK_END);
  12.         long filesize = ftell(fp);
  13.         rewind(fp);
  14.         fclose(fp);
  15.         return(filesize);
  16. }
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.         if(argc < 2)
  21.         {
  22.                 printf("usage: %s <logfile>\n", argv[0]);
  23.                 exit(1);
  24.         }
  25.         long fsize = filesize(argv[1]);
  26.  
  27.         FILE *fp;
  28.         if((fp = fopen(argv[1], "r")) == NULL)
  29.         {
  30.                 printf("unable to open %s\n", argv[1]);
  31.                 exit(1);
  32.         }
  33.         printf("filesize of %s: %d bytes\n", argv[1], fsize);
  34.  
  35.         fclose(fp);
  36.         return 0;
  37. }
  38.  
Everything compiles just fine except for one annoying little warning I keep getting in reference to the file handle I'm trying to open in the
Expand|Select|Wrap|Line Numbers
  1. filesize()
function. When I go to compile the program, it gives me this error:

Expand|Select|Wrap|Line Numbers
  1. log.c: In function `filesize':
  2. log.c:6: warning: passing arg 1 of `fopen' makes pointer from integer without a cast
I'm wondering if it's possible to eliminate that warning by making whatever change is necessary. The two lines that I'm aiming to make possible by declaring this particular function are as follows:

Expand|Select|Wrap|Line Numbers
  1. long fsize = filesize(argv[1]);
  2. printf("filesize of %s: %d bytes\n", argv[1], fsize);
  3.  
Any help would be appreciated! The program will still compile and run just fine, but it'd be nice to compile and not have to see that warning.

By the way, I'm only interested in programming in C at this time.
Mar 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: 5
Ah, nevermind, I found it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. long filesize(char f[])
  4. {
  5. ...
  6.  
Should've seen it before :-P
Mar 9 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
Good to see you got it on your own. :)
Mar 9 '07 #3

P: 5
Here's my new function...header include even!

Expand|Select|Wrap|Line Numbers
  1. /* filesize.h - returns size in bytes of filename */
  2. long filesize(char f[])
  3. {
  4.         FILE *fp;
  5.         if((fp = fopen(f, "r")) == NULL)
  6.         {
  7.                 fprintf(stderr, "failed to open %s\n", f);
  8.                 exit(1);
  9.         }
  10.         fseek(fp, 0, SEEK_END);
  11.         long filesize = ftell(fp);
  12.         fclose(fp);
  13.         return(filesize);
  14. }
  15.  
Mar 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.