私はもっと多くの行を持つファイルを持っていますが、今は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)が良好である、すなわち、行全体です。しかし、2番目の出力(%s)は行の最初のchar(ファイル内の数字です...ファイルはlatin2のtxtです)
'onerow'は文字の配列ではなく、ポインタの配列です。 – Barmar
あなたは 'onerow [j ++] = c;'から警告を受けていませんか? – Barmar
'fgets()'を使って行を読んでみませんか? – Barmar