473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
3 17078
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
3,652 Expert 2GB
Good to see you got it on your own. :)
Mar 9 '07 #3
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

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

Similar topics

27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
4
by: Dawn Minnis | last post by:
Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
2
by: francescomoi | last post by:
Hi. I'm trying to compile this piece of source: ------------------------------------------- int id; while(row1 = mysql_fetch_row(rs1)) { id = atoi((int)row1);...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
7
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'...
10
drhowarddrfine
by: drhowarddrfine | last post by:
warning: assignment makes pointer from integer without a cast I get that when I compile a C file where the function is defined as this: char **getvars() and the calling function has this...
1
by: woods1313drew | last post by:
The following code in c+ gives me the warning assignment makes integer from pointer without a cast. destination is set as char destination to limit the input string to 10 characters. name is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.