→ tropical72:fscanf(fp1, "%d %d%*[^\n]\n",&a,&b); 09/11 22:19
→ tropical72:fscanf(fp1, "%d %d\n",&a,&b); 都試試. 09/11 22:19
→ rock1985:好像解決了,我再測試一下 感謝大大 09/11 22:38
→ rock1985:我本來用fscanf(fp1, "%d %d\n",&a,&b);有問題 09/11 22:39
→ rock1985:之前可能也有寫錯某些地方 非常感謝 09/11 22:39
→ tropical72:可問一下解決方案嗎? 09/11 22:47
Sorry 我剛剛測試了一下還是不行
如果改成用fscanf(pFile,"%d %d%\n",&row, &column);
檔案的第2行的第一個空白會被忽略
結果會變成
*-------------------
--*-----------------
***-----------------
--------------------
--------------------
-------------***----
--------------------
--------------------
第一行前面少了一個空白
應該要是
-*------------------
--*-----------------
***-----------------
--------------------
--------------------
-------------***----
--------------------
--------------------
才對 所以我還在想解決方案
※ 編輯: rock1985 來自: 69.203.141.188 (09/11 23:05)
→ firejox:那應該是你搭配了fgetc... 09/11 23:10
抱歉 我太懂你的意思
在windows下面
用 fscanf(pFile,"%d %d\n",&row, &column);
少一個空白
用 fscanf(pFile,"%d %d%*c",&row, &column);
正確
在linux
用 fscanf(pFile,"%d %d\n",&row, &column);
少一個空白
用 fscanf(pFile,"%d %d%*c",&row, &column);
整整會多一行
用 fscanf(pFile,"%d %d%*c",&row, &column);
+ c = fgetc (pFile);
正確
※ 編輯: rock1985 來自: 69.203.141.188 (09/11 23:21)
→ tropical72:第一行推文後,不再加上 fgetc, 這樣也不行嗎? 09/11 23:31
目前用的版本是
#include <stdio.h>
#include <stdlib.h>
int row, column;
FILE * outFile;
void printScreen(int **mat) //Prints result
{
int i,j;
for(i = 1 ; i <= row ; i++)
{
for(j = 1 ; j <= column ; j++)
{
if(mat[i][j] == 0){
//printf("%d " , curr[i][j]);
printf("-");
}else{
printf("*");
}
}
printf("\n");
}
printf("______________________________\n");
printf("______________________________\n\n");
}
int main(int argc, char *argv[])
{
FILE * inFile;
int c;
//int n = 0, star = 0, line = 0;
int countingRow = 1, countingColumn = 1;
int **curr, **next;
//printf("%s\n",*argv);
if(argc < 2){
inFile=fopen ("life.txt","r");
printf("Default pattern\n");
}else{
inFile=fopen (argv[1],"r");
printf("The pattern you choose is %s\n",argv[1]);
}
//inFile=fopen ("life.txt","r");
outFile = fopen("out_test.txt","w");
if (inFile==NULL) perror ("Error opening file");
else
{
/* get row and column */
//fscanf(inFile,"%d %d\n",&row, &column);
fscanf(inFile,"%d %d%*[^\n]",&row, &column);
//fscanf(inFile,"%d %d%*c",&row, &column);
/* initialize the 2-D array */
curr = (int**) malloc( (row+2) * sizeof(int*));
int i,j;
for (i = 0 ; i < row+2 ; i++){
curr[i] = (int*) malloc( (column+2) * sizeof(int));
}
next = (int**) malloc( (row+2) * sizeof(int*));
for (i = 0 ; i < row+2 ; i++){
next[i] = (int*) malloc( (column+2) * sizeof(int));
}
for(i = 0 ; i < row+2 ; i++)
{
for(j = 0 ; j < column+2 ; j++)
{
curr[i][j] = 0;
next[i][j] = 0;
}
}
c = fgetc (inFile);
/* start reading life.txt file */
do {
c = fgetc (inFile);
if(countingColumn < column){
if (c == ' '){
countingColumn++;
//n++;
}
else if(c == '*'){
curr[countingRow][countingColumn] = 1;
countingColumn++;
//star++;
}
else if(c == '\n'){
countingRow++;
countingColumn = 1;
//line++;
}
}
} while (c != EOF);
fclose (inFile);
printf ("Row is %d and Column is %d\n",row, column);
}
//system("pause");
/* start playing life game */
printf("original life pattern\n");
printScreen(curr);
fclose (outFile);
return 0;
}
利用
fscanf(inFile,"%d %d%*[^\n]",&row, &column);
c = fgetc (inFile);
可以同時在windows 和 linux都執行正確
但是一整個混亂 不知道會什麼這樣會是對的
※ 編輯: rock1985 來自: 69.203.141.188 (09/11 23:39)
→ tropical72:跳tone,你最好再確認一下你mem index,起始是 0/1 在切. 09/11 23:42
→ rock1985:我覺得問題還是在讀檔的部分 array那邊我覺得沒有問題 09/11 23:46
→ kaomark:大概是\r\n的問題吧 fscanf用"%d %d "把空白字元都讀掉 09/12 00:23
→ kaomark:後面就不用自己再去fgetc()了 09/12 00:23
→ firejox:fscanf "%d%d\n" .... 09/12 02:02
→ rock1985:請恕小弟愚昧,可以請你示範一下嗎? 09/12 02:20
→ firejox:對了可以附一下檔案嗎? 09/12 03:29