2017-06-29 301 views
-1

我有這個Members.txt與此內容:如何在C語言中處理10位以上的小數點INT?

3 
Rebaz salimi 3840221821 09188888888 
4 
95120486525 
95120482642 
95120541325 
95120860452 

即時閱讀它變成數組對象從struct booksstruct MEMBERS並打印出的screen.here是我的代碼:

struct books 
{ 
    long long int id; 
    char stname[20]; 
    char name[20]; 
    char Author[20]; 
    int quantity; 
    float Price; 
    int count[100]; 
    int code[100]; 
    int rackno; 
    char *cat; 
    struct meroDate issued; 
    struct meroDate duedate; 
}; 
struct MEMBERS 
{ 
    long long int code; 
    char Fname[40]; 
    char Lname[40]; 
    long long int Pnum; 
    float bedeh; 
    float credit; 
    struct meroDate issued; 
    struct meroDate duedate; 
    }; 

struct MEMBERS cur_member[100]; 
struct books cur_book[100]; 

int main() 
{ 

    getdataMEMBERS(); 

    return 0; 
    } 

    int getdataMEMBERS() 
    { 
     FILE *myfile = fopen("Members.txt", "r"); 
     FILE *myfile1 = fopen("Members.txt", "r"); 
    if (myfile == NULL) { 
        printf("Cannot open file.\n"); 
     return 1; 
         } 
        else { 
          char ch; 
          int count = 0; 
     do 
     { 
      ch = fgetc(myfile); 
       if (ch == '\n') count++; 
      } while (ch != EOF); 
      rewind(myfile); 

     //Since you put 2 earlier in the member.txt we need to dump it 
     //so that it wont affect the scanning process 
     int temp; 
     fscanf(myfile, "%d", &temp); 
     printf("%d\n", temp); 
     //Now scan all the line inside the text 
     int i; 
     for (i = 0; i < 1; i++) { 
     fscanf(myfile, "%s %s %lld %lld\n %d", cur_member[i].Fname, 
     cur_member[i].Lname, &cur_member[i].code, &cur_member[i].Pnum 
     ,&cur_book[i].quantity); 

       printf("%s %s %lld %lld\n 
     %d\n",cur_member[i].Fname,cur_member[i].Lname, 
     cur_member[i].code,cur_member[i].Pnum , cur_book[i].quantity); 

     int j; 
     for(j=0;j<cur_book[i].quantity;j++) 
     { 
      fscanf(myfile,"%d\n",&cur_book[j].id); 
      printf("%d\n",cur_book[j].id); 
     } 


     } 

    } 
     fclose(myfile); 

} 

一旦我printf這代碼:

int j; 
for(j=0;j<cur_book[i].quantity;j++) 
{ 
    fscanf(myfile,"%d\n",&cur_book[j].id); 
    printf("%d\n",cur_book[j].id); 
} 

它給我:

631206013 
    631202130 
    631260813 
    631579940 

應該想給我:

95120486525 
    95120482642 
    95120541325 
    95120860452 

我已經改變了數據類型在struct bookslong long double id;但仍然gaves我這三個輸出:

631206013 
    631202130 
    631260813 
    631579940 

會有人知道在哪裏問題可能是?數據類型還是別的?

+1

有沒有必要對這些數字進行計算?如果沒有,只需存儲*字符串*。 'double'不會使整數產生任何*意義,只會失去精度。爲更廣泛的'int'嘗試'long long'。 –

+0

爲什麼'long long double'(它不是作爲一個類型存在,你的意思是'long double'')而不是'long long int',它在支持的系統上至少有64位? –

+1

您正在用'%d'轉換說明符打印(並掃描)一個'long long int'? –

回答

2

您在printf調用中使用錯誤的格式說明符代替long long,因此您的代碼的行爲實際上是未定義的。

使用%lld輸出id成員。

真的,如果您使用的數字是標識符而不是正在執行數值計算的東西,那麼使用char[]類型會更自然。這將允許你保存前導零點& c。

+0

tnx哥們,它的作品! – moh89

0

如果你有一個編譯器支持C99,你能希望它實現<stdint.h>uint64_t,這會給你足夠精確到整數處理超過18個十進制數字。

您將不得不修改代碼,當然,您不能使用%d來掃描uint64_t;您必須使用"%" SCNu64,然後使用"%" PRIu64來打印它。是的,這些都是正確書寫的,它是兩個相鄰的字符串文字,將被編譯成單個字符串。

但是,如果這些東西只是標識符,你可能會把它們當作字符串。

+0

我試過了,給了我相同的結果!那麼,它沒有工作! – moh89