到底邊到錯?c program 入野落array入唔到

2007-10-17 4:53 am
我本身有篇文"text"係個program同一個file入面
我想把each word塞入去一個2-D array入面
我寫左下面呢d野,但係入唔到落個array到,,,
到底邊到錯?
please help.....

main()
{
char cuttext[1000][50];
int i,j,c;
FILE *file;
char ch;
file=fopen("text.txt","r");
ch=fgetc(file);
i=0,j=0;
while(ch!=EOF){
if(ch!=' '){
cuttext[i][j]=ch;
j++;
ch=fgetc(file);}
else
i++;
j=0;
ch=fgetc(file);}

system("PAUSE");
fclose(file);}

回答 (1)

2007-10-17 6:59 pm
✔ 最佳答案
I have modified a few different places to make it work.
The main changes are:
1. Replaced an if by a while statement.
2. added end of string to the end of each word
3. print words to verify
4. add an EOF check to terminate program

int main()
{
char cuttext[1000][50];
int i,j,c;
FILE *file;
char ch;
file=fopen("text.txt","r");
ch=fgetc(file);
i=0,j=0;
while(ch!=EOF){
while(ch!=' ' && ch!=EOF)// while to continue reading
{
cuttext[i][j]=ch;
j++;
ch=fgetc(file);
}
cuttext[i][j]='\0';// end word
printf("%s\n",cuttext[i]);
if(ch==EOF)break;// if EOF, get out
i++;
j=0;
ch=fgetc(file);
}
system("PAUSE");
fclose(file);}


收錄日期: 2021-04-13 13:57:29
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071016000051KK03505

檢視 Wayback Machine 備份