读取同名.txt文件,或者命令行指定文件名对应的文件并输出控制台,你可以给exe改名为任意的名称
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
FILE *file;
char filename[256];
if (argc > 1) {
strncpy(filename, argv[1], sizeof(filename));
} else {
strncpy(filename, argv[0], sizeof(filename));
strcat(filename, ".txt");
}
file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file %s\n", filename);
return 1;
}
char line[256];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
return 0;
}
用法: