一番下にコードを示します.
コマンドライン引数はargv[0]にはファイル名が入って,その後ろから実行時に入れたものが文字列として入っていると思うのですが,以下のコードでコンパイルを行うと次のようなwarningが出ます.
17行目のwarningはなぜ,argv[0]がintだと言われているのでしょうか.実際,このまま実行してみてもこの部分のprintfでは何も表示されず,%dに変更して実行してみると-41という値が表示されました.
splitfile.c: In function ‘main’: splitfile.c:17:22: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] 17 | printf("argv[0]:%s\n", argv[0]); | ~^ ~~~~~~~ | | | | char * int | %d splitfile.c:28:31: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast [-Wint-conversion] 28 | int split_byte = atoi(argv[4]); | ~~~~^~~ | | | char In file included from splitfile.c:2: /usr/include/stdlib.h:104:30: note: expected ‘const char *’ but argument is of type ‘char’ 104 | extern int atoi (const char *__nptr) | ~~~~~~~~~~~~^~~~~~ splitfile.c:32:25: warning: passing argument 1 of ‘open’ makes pointer from integer without a cast [-Wint-conversion] 32 | if( (ifd = open(argv[1], O_RDONLY) ) == -1 ){ | ~~~~^~~ | | | char In file included from splitfile.c:5: /usr/include/fcntl.h:168:30: note: expected ‘const char *’ but argument is of type ‘char’ 168 | extern int open (const char *__file, int __oflag, ...) __nonnull ((1)); | ~~~~~~~~~~~~^~~~~~ splitfile.c:36:26: warning: passing argument 1 of ‘open’ makes pointer from integer without a cast [-Wint-conversion] 36 | if( (ofd1 = open(argv[2], O_WRONLY | O_TRUNC, 0666)) == -1){ | ~~~~^~~ | | | char In file included from splitfile.c:5: /usr/include/fcntl.h:168:30: note: expected ‘const char *’ but argument is of type ‘char’ 168 | extern int open (const char *__file, int __oflag, ...) __nonnull ((1)); | ~~~~~~~~~~~~^~~~~~ splitfile.c:40:26: warning: passing argument 1 of ‘open’ makes pointer from integer without a cast [-Wint-conversion] 40 | if( (ofd2 = open(argv[3], O_WRONLY | O_TRUNC, 0666)) == -1){ | ~~~~^~~ | | | char In file included from splitfile.c:5: /usr/include/fcntl.h:168:30: note: expected ‘const char *’ but argument is of type ‘char’ 168 | extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
###コード
C
1#include<stdio.h> 2#include<stdlib.h> 3#include<sys/types.h> 4#include<sys/stat.h> 5#include<fcntl.h> 6#include<unistd.h> 7 8/* 9*argv[1] = Input_file 10*argv[2] = Output_file1 11*argv[3] = Output_file2 12*argv[4] = split_byte 13*/ 14int main(int argc, char *argv){ 15 16 printf("before argument check\n"); 17 printf("argv[0]:%s\n", argv[0]); 18 19 //check command line arguments 20 if(argc != 5 ){ 21 //printf("arguments:%d\n",argc); 22 perror("command line arguments"); 23 exit(1); 24 } 25 26 int ifd, ofd1, ofd2; 27 char buf[1024]; //want to change actively 28 int split_byte = atoi(argv[4]); 29 ssize_t cc; 30 31 32 if( (ifd = open(argv[1], O_RDONLY) ) == -1 ){ 33 perror("open IN_file"); 34 exit(1); 35 } 36 if( (ofd1 = open(argv[2], O_WRONLY | O_TRUNC, 0666)) == -1){ 37 perror("open Out_file1"); 38 exit(1); 39 } 40 if( (ofd2 = open(argv[3], O_WRONLY | O_TRUNC, 0666)) == -1){ 41 perror("open Out_file2"); 42 exit(1); 43 } 44 int num_read = 0; 45 //printf("before while\n"); 46 while( ( cc = read(ifd, buf, sizeof(buf))) > 0){ 47 if(num_read <split_byte){ 48 if(write(ofd1, buf, cc) == -1){ 49 perror("write Out_file1"); 50 exit(1); 51 } 52 } 53 else{ 54 if(write(ofd2, buf, cc) == -1){ 55 perror("write Out_file2"); 56 exit(1); 57 } 58 } 59 num_read++; 60 } 61 if(cc == -1){ 62 perror("read"); 63 exit(1); 64 } 65 66 if(close(ifd) == -1){ 67 perror("close IN_file"); 68 exit(1); 69 } 70 if(close(ofd1) == -1){ 71 perror("close Out_file1"); 72 exit(1); 73 } 74 if(close(ofd2) == -1){ 75 perror("close Out_file2"); 76 exit(1); 77 } 78} 79 80
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/20 06:47