C でファイルを開こうとしていますが、常にファイルを開くことができません。次のコードがあります。
int i = 0;
char delims[] = " ";
char *result = NULL;
char * results[10];
result = strtok( cmdStr, delims );
while( result != NULL ) {
results[i] = result;
i++;
result = strtok(NULL, " ");
}
printf(results[1]); // it defo shows the name file here
FILE *fp;
char ch;
if((fp = fopen(results[1],"r")) == NULL) {
printf("Cannot open file.\n");
} else {
while((ch = fgetc( fp )) != EOF) {
printf("%c", ch);
}
}
fclose(fp);
Results[1] はファイルの名前です。したがって、「show file.txt」のようなものがある場合、結果[0]は表示され、結果[1]はfile.txtになります。
ただし、fopenでは開きません。しかし、コードを挿入するとfopen("file.txt", "r")
...動作します。