質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

コマンドライン

コマンドライン(別名:Command Line Interface)は、ユーザに命令の入力を促す(プロンプト)文字列の表示を行い、すべての操作をキーボードを用いて文字列を打ち込む事でプログラムを走らせるユーザインターフェースです。

コンパイル

コンパイルとは、プログラミング言語のテキストソース(ソースコード)をコンピュータ上で実行可能な形式(オブジェクトコード)に変換することをいいます

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Q&A

解決済

1回答

768閲覧

コマンドライン引数の型

grape_ll

総合スコア83

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

コマンドライン

コマンドライン(別名:Command Line Interface)は、ユーザに命令の入力を促す(プロンプト)文字列の表示を行い、すべての操作をキーボードを用いて文字列を打ち込む事でプログラムを走らせるユーザインターフェースです。

コンパイル

コンパイルとは、プログラミング言語のテキストソース(ソースコード)をコンピュータ上で実行可能な形式(オブジェクトコード)に変換することをいいます

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

0グッド

0クリップ

投稿2021/06/20 06:20

一番下にコードを示します.
コマンドライン引数は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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

int main(int argc, char *argv){ではなく
int main(int argc, char **argv){ですね。

投稿2021/06/20 06:24

LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

grape_ll

2021/06/20 06:47

確かにそうでした,基本的なところに気づきませんでした. ご指摘ありがとうございました.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問