파일이 더 많습니다. 이제는 char에서 char로 3 번째를 스캔하고 싶습니다. 이 노력하고 있습니다,하지만 난 첫 번째 하나 (onerow) 배열로 문자를 할당 할 수 없습니다 :C에서 fgetc를 사용하여 파일 읽기
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#endif
int main(void) {
#if defined(WIN32) || defined(_WIN32) //for latin2
SetConsoleCP(1250);
SetConsoleOutputCP(1250);
#endif
//char* onerow=(char*) malloc(num*sizeof(char));
char* onerow[250];
char c;
int row=3, j=0;
FILE *fp;
fp = fopen("proba.txt", "r");
for (int i=0; i<=row; i++) {
if(i!=row-1) { //not the row we need, jump over it
while(c!='\n')
c=fgetc(fp);
c='a'; //to make true while next time
}
if(i==row-1) {
while(c!='\n') { //this is what we need
//onerow[j]=fgetc(fp);
//onerow = (char*)realloc(onerow, ++num*sizeof(char));
c=fgetc(fp);
printf("%c", c); //this is working well (prints everyth.)
onerow[j++]=c;
}
}
}
onerow[j-1]='\0';
fclose(fp);
printf("%s", onerow); //prints only the first charachter
//free(onerow);
}
먼저 출력 (% C는) 좋은, 즉 전체 행입니다. 그러나 두 번째 출력 (% s)은 행의 첫 번째 문자 (파일의 숫자 ... 파일이 latin2 txt 임)입니다.
'onerow'는 문자의 배열이 아닌 포인터의 배열입니다. – Barmar
'onerow [j ++] = c;'에서 경고를받지 못했습니까? – Barmar
'fgets()'을 사용하여 줄을 읽지 않는 이유는 무엇입니까? – Barmar