Hey all! Spend 3 months since October reading my 700 page book for the second time, now I am on the second to last question before I finish it,...would like to finish the last exercise which includes writing a complete program. Would like to inventory my groceries I have in the freezer, refrigerator, and also in the dry stock shelf.
Just a quick question on this code:
Code:
// commandargs.c--Accessing command-line arguments.
#include <stdio.h>
#include <errno.h>
int file_copy( char *oldname, char *newname );
int main(int argc, char *argv[])
{
int a, b;
int count;
char source[80], destination[80];
/* Get the source and destination names. */
if (argc < 2 || argc > 3)
{
printf("Enter 2 arguments only\n");
}
a = atoi(argv[1]);
b = atoi(argv[2]);
if( file_copy( a, b ) == 0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
return 0;
}
int file_copy( char *oldname, char *newname )
{
FILE *fold, *fnew;
int c;
/* Open the source file for reading in binary mode. */
if ( ( fold = fopen( oldname, "rb" ) ) == NULL )
return -1;
/* Open the destination file for writing in binary mode. */
if ( ( fnew = fopen( newname, "wb" ) ) == NULL )
{
fclose ( fold );
return -1;
}
/* Read one byte at a time from the source; if end of file */
/* has not been reached, write the byte to the */
/* destination. */
while (1)
{
c = fgetc( fold );
if (!feof( fold ) )
fputc( c, fnew );
else
break;
}
fclose ( fnew );
fclose ( fold );
return 0;
}
Here..
Code:
a = atoi(argv[1]);
b = atoi(argv[2]);
Compiler is complaing about using atoi() in C99, not even sure if this is the right function, and was also wondering if strtol() would be.
Also...
Code:
[jamie@bindfix Lesson_22]$ cc -o ex_5_2 ex_5_2.c
ex_5_2.c:21:6: warning: implicit declaration of function 'atoi' is invalid in C99 [-Wimplicit-function-declaration]
a = atoi(argv[1]);
^
ex_5_2.c:24:17: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'char *' [-Wint-conversion]
if( file_copy( a, b ) == 0 )
^
ex_5_2.c:6:22: note: passing argument to parameter 'oldname' here
int file_copy( char *oldname, char *newname );
^
ex_5_2.c:24:20: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'char *' [-Wint-conversion]
if( file_copy( a, b ) == 0 )
^
ex_5_2.c:6:37: note: passing argument to parameter 'newname' here
int file_copy( char *oldname, char *newname );
^
3 warnings generated.