前提・実現したいこと
コマンドライン引数にファイル名data1.txtを与えてその中身の文字数と行数を出力するプログラムを作成したいです。
該当のソースコード
#include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { int n; n= atoi(argv[1]); #define BUF_SIZE 256 char buf[BUF_SIZE]; char* filename = "n"; FILE* fp; int contentsLen; int line = 0; fp = fopen(filename, "r"); if(fp == NULL) exit(1); fseek(fp,0,SEEK_END); contentsLen = ftell(fp); fseek(fp,0,SEEK_SET); while (fgets(buf, BUF_SIZE, fp) != NULL) { line++; } fclose(fp); printf("%.d\n%.d\n", contentsLen, line); return 0; }
![guest](/img/icon/icnUserSample.jpg)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。