ユーザーがコマンドラインに 2 つの引数を入力する必要がある C プログラムを作成しようとしています。まず、読み取る値のテキスト ファイルの名前を指定する必要があります。次に、0 または 1 の値を指定する必要があります。この値は、ブール値 (0=false、1=true) として使用される整数として保存されます。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *f;
char *fname;
int boolVal;
if (argc != 2){
printf("This program requires two input types, data file and boolean value of 0 or 1\n");
return 1;
}
fmame = argv[1];
boolVal = argv[2];
printf("The file name is %s and the boolVal is %d.\n", fmame, boolVal);
f = fopen(fname, "r");
if (f == NULL) perror ("Could not open file");
else {
if (fgets(myStr, 1000, f) != NULL )
puts(myStr);
fclose(f);
}
return 0;
}
エラーが発生します:
testArg.c: In function ‘main’:
testArg.c:16: warning: assignment makes integer from pointer without a cast
2 つの質問があります。ファイル名の読み方は正しいですか? 第二に、キャスティングの問題をどのように解決しますか?