Generation

generate functionSun, 04 Jun 2023

コマンドラインで指定されたテキストファイルの内容を1行ずつ表示するプログラム 1行表示するごとに次の行を表示するか尋ねる

#include <stdio.h> #include <stdlib.h> int main(int argc, char const *argv[]) { FILE *file; char line[128]; if (argc < 2) { printf("%s: no file name\n", argv[0]); return 1; } if ((file = fopen(argv[1], "r")) == NULL) { printf("%s: cannot open %s\n", argv[0], argv[1]); return 1; } while (fgets(line, sizeof(line), file) != NULL) { printf("%s", line); printf("continue ? (y or n) > "); char answer[3]; fgets(answer, sizeof(answer), stdin); if (answer[0] == 'n') { break; } } return 0; }

Want to kickstart your project?Use the new AI Studio to create your code