簡體   English   中英

使用fscanf()從兩列文件中獲取兩個值

[英]Using fscanf() to get two values from a two column file

大家好,這是我第一次發布有關堆棧溢出的信息,我是c的新手,所以讓我知道我做錯了什么。

我正在嘗試使用fscanf將文件中的單詞放入我的兩個字符串變量(currentUID和currentKey)。 但是由於某種原因,當我使用fscanf為currentKey變量提供值時,存儲在currentUID中的值(在我的第一句話中)消失了……我一直在尋找可能發生這種情況的原因,但到目前為止一直無法弄清楚。 您知道為什么會這樣嗎?

while(!feof(fp))
{
    fscanf(fp, "%s", currentUID);
        //1 currentUID has correct value here...
    fscanf(fp, "%s", currentKey);
        //2 But currentUID has lost its value here. 
}

這是文件格式的示例:

421 0123456789abcdef0123456789abcde0

422 0123456789abcdef0123456789abcde1

423 0123456789abcdef0123456789abcde2

423 0123456789abcdef0123456789abcde3

我要做的就是在第二個評論時同時訪問這兩個值,以便將它們傳遞給另一種方法。 任何輸入將不勝感激。

閱讀如下:

    while(fscanf(fp, "%s%s", currentUID,currentKey)==2)
{
   // printf("%s ", currentUID);
        //1 currentUID has correct value here...
   // printf("%s\n", currentKey);
        //2 But currentUID has lost its value here. 
}

編輯您是否正確聲明了變量?

char currentUID[4],currentKey[33];

暫無
暫無

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

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