簡體   English   中英

使用 fscanf 將文本文件中的整數讀取兩次以存儲到數組中

[英]Ints from text file are being read twice using fscanf to store into arrays

int main(int argc, char **argv)
{
    FILE *file  = fopen("Offices.txt", "r");
    char officeArray[10];
    int yCoordinate[10];
    int xCoordinate[10];

    int i=0;
    int x, y;
    char office;

    while(fscanf(file, "%c,%d,%d", &office, &x, &y) > 0)
    {
        officeArray[i] = office;
        xCoordinate[i] = x;
        yCoordinate[i] = y;
        printf("%c,%d,%d \n", officeArray[i], xCoordinate[i], yCoordinate[i]);
        i++;
    }

    fclose(file);

    return 0;
}

我有一個節點字母和坐標的文本文件:

A,1,1
B,1,5
C,3,7
D,5,5
E,5,1

我的輸出是:

A,1,1 
,1,1
B,1,5 
,1,5
C,3,7 
,3,7 
D,5,5 
,5,5 
E,5,1 
,5,1

我不知道為什么我從文本文件中讀取了兩倍的整數。

我需要在 fscanf 調用中包含一個換行命令。

while(fscanf(file, "%c,%d,%d\n", &office, &x, &y) > 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM