しばらくCプログラミングを行っていませんが、文字列を出力する次のものがありますが、それを配列に入れて操作できるようにしたいと思います(コード)。配列のサイズを宣言する必要があると思いますが、可変量を処理する必要があります。のようなことを考えていましsystem('wc -l filename')
たが、それは本当にひどいですね。より良い方法でなければなりません:
#include <stdio.h>
int main()
{
char *inname = "test.txt";
FILE *infile;
char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
char line_number;
infile = fopen(inname, "r");
if (!infile) {
printf("Couldn't open file %s for reading.\n", inname);
return 0;
}
printf("Opened file %s for reading.\n", inname);
line_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
++line_number;
/* note that the newline is in the buffer */
// ---------------------
// would like to put into an array of strings here rather than just printf'ing out out
printf("%4d: %s", line_number, line_buffer);
}
printf("\nTotal number of lines = %d\n", line_number);
return 0;
}