[問題] C語言 fscanf 問題 in Windows & Linux
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
win : Code::Blocks + gcc4.4.1
linux
問題(Question):
在不同平台下,程式要正確執行差了一個fgetc
餵入的資料(Input):
life.txt
8 20
*
*
***
***
預期的正確結果(Expected Output):
-*------------------
--*-----------------
***-----------------
--------------------
--------------------
-------------***----
--------------------
--------------------
錯誤結果(Wrong Output):
在linux下面 如果不多一行 c = fgetc (pFile);
則結果會變成
--------------------
-*------------------
--*-----------------
***-----------------
--------------------
--------------------
-------------***----
--------------------
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
int row, column;
void print(int **mat) //Prints result
{
for(int i = 1 ; i <= row ; i++)
{
for(int j = 1 ; j <= column ; j++)
{
if(mat[i][j] == 0){
//printf("%d " , arr[i][j]);
printf("-");
}else{
printf("*");
}
}
printf("\n");
}
printf("______________________________\n");
printf("______________________________\n\n");
}
int main()
{
FILE * pFile;
int c;
int n = 0;
int star = 0;
int lin = 0;
int count_row = 1, count_col = 1;
int **arr, **tmp;
pFile=fopen ("life.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
{
//get row and column
fscanf(pFile,"%d %d%*c",&row, &column);
//initialize the 2-D array
arr = (int**) malloc( (row+2) * sizeof(int*));
for (int i=0 ; i < row+2 ; i++){
arr[i] = (int*) malloc( (column+2) * sizeof(int));
}
tmp = (int**) malloc( (row+2) * sizeof(int*));
for (int i=0 ; i < row+2 ; i++){
tmp[i] = (int*) malloc( (column+2) * sizeof(int));
}
for(int i = 0 ; i < row+2 ; i++)
{
for(int j = 0 ; j < column+2 ; j++)
{
arr[i][j] = 0;
tmp[i][j] = 0;
}
}
//c = fgetc (pFile); 在linux下面需要有這一行
//start reading life.txt file
do {
c = fgetc (pFile);
if(count_col < column){
if (c == ' '){
count_col++;
n++;
}
else if(c == '*'){
arr[count_row][count_col] = 1;
count_col++;
star++;
}
else if(c == '\n'){
count_row++;
count_col = 1;
lin++;
}
}
} while (c != EOF);
fclose (pFile);
printf ("Row is %d and Column is %d\n",row, column);
}
printf("original life pattern\n");
print(arr);
return 0;
}
補充說明(Supplement):
我主要是要把一個圖形依照像對位置放進陣列裡面
我知道fscanf不處理換行符號
所以我用跳脫字元來把它拿掉
在window下面指標好像會移到下一行的開頭
但是在linux下面似乎就不行了
想請問一下大家有解決的方法嗎??
謝謝大家
--
我不是宅 我只是比較居家
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 69.203.141.188
※ 編輯: rock1985 來自: 69.203.141.188 (09/11 22:01)
→
09/11 22:19, , 1F
09/11 22:19, 1F
→
09/11 22:19, , 2F
09/11 22:19, 2F
→
09/11 22:38, , 3F
09/11 22:38, 3F
→
09/11 22:39, , 4F
09/11 22:39, 4F
→
09/11 22:39, , 5F
09/11 22:39, 5F
→
09/11 22:47, , 6F
09/11 22:47, 6F
Sorry 我剛剛測試了一下還是不行
如果改成用fscanf(pFile,"%d %d%\n",&row, &column);
檔案的第2行的第一個空白會被忽略
結果會變成
*-------------------
--*-----------------
***-----------------
--------------------
--------------------
-------------***----
--------------------
--------------------
第一行前面少了一個空白
應該要是
-*------------------
--*-----------------
***-----------------
--------------------
--------------------
-------------***----
--------------------
--------------------
才對 所以我還在想解決方案
※ 編輯: rock1985 來自: 69.203.141.188 (09/11 23:05)
→
09/11 23:10, , 7F
09/11 23:10, 7F
抱歉 我太懂你的意思
在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)
→
09/11 23:31, , 8F
09/11 23:31, 8F
目前用的版本是
#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)
→
09/11 23:42, , 9F
09/11 23:42, 9F
→
09/11 23:46, , 10F
09/11 23:46, 10F
→
09/12 00:23, , 11F
09/12 00:23, 11F
→
09/12 00:23, , 12F
09/12 00:23, 12F
→
09/12 02:02, , 13F
09/12 02:02, 13F
→
09/12 02:20, , 14F
09/12 02:20, 14F
→
09/12 03:29, , 15F
09/12 03:29, 15F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章