잠시 후에 C 프로그래밍을 수행하지는 않았지만 다음과 같이 문자열을 출력하지만 배열에 넣을 수 있습니다. 코드의 주석에서 ---------을 조작하십시오. 배열의 크기를 선언해야하지만 가변 양을 처리해야한다고 생각합니다. system('wc -l filename')
과 같은 것을하려고 생각했지만 정말 끔찍한 것 같습니다. 모든입력 파일에서 C로 문자열의 동적 배열을 만드는 방법
#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;
}