2015-10-19 86 views
0

我正試圖通過寫入和從.txt文件讀取/讀取來創建C中的小型數據庫。C後字單詞讀取

#include <stdio.h> 
    #include <math.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    #include <string.h> 

    #define MAX_SIZE      5000 


    int main(void){ 
     static int output[6]; 
     int enabeled_is_there = 0, sum_is_there = 0, max_widraw_is_there = 0, max_insert_is_there = 0; 
     int min_widraw_is_there = 0,min_insert_is_there = 0; 
     char *checker = NULL; 
     char *checker2 = NULL; 
     char *temp; 
     char str[MAX_SIZE]; 
     FILE *fptr; 
     if ((fptr=fopen("data.txt","r"))==NULL){ 
      printf("Did not find file, creating new\n"); 
      Dinmor : fptr = fopen("data.txt", "w"); 
      fputs("//This text file contain information regarding the program 'monies.c'.\n",fptr); 
      fputs("//Feel free to edit the file as you please.\n\n",fptr); 
      fputs("//Settings: \n",fptr); 
      fputs("//Y enabels, while N dissenables everything.\n",fptr); 
      fputs("enabel = Y\n\n",fptr); 
      fputs("//How much money there is in your conto, feel free to edit this number as you please.\n",fptr); 
      fputs("//What you write here, will be rounded down to closes whole number.\n",fptr); 
      fputs("sum = 6000 \n\n",fptr); 
      fputs("//How much money you are allowed to widraw. What you write here, will be rounded down to closes whole number.\n",fptr); 
      fputs("maxWidraw = 500 \n\n",fptr); 
      fputs("//How much money you are allowed to insert. What you write here, will be rounded down to closes whole number.\n",fptr); 
      fputs("maxInsert = 500 \n\n",fptr); 
      fputs("//The smalles cash you can insert. What you write here, will be rounded down to closes whole number.\n",fptr); 
      fputs("minInsert = 50 \n\n",fptr); 
      fputs("//The smalles cash you can widraw. What you write here, will be rounded down to closes whole number.\n",fptr); 
      fputs("minWindraw = 50 \n\n",fptr); 
      fclose(fptr); 

     }else{ 
      if ((fptr=fopen("data.txt","r"))==NULL){ 
       printf("Error reading file\n"); 

      }else{ 
       fptr = fopen("data.txt","r"); 
       printf("Found file, reading data\n"); 
       printf("Settings:\n"); 
       while(fgets(str, MAX_SIZE, fptr)!=NULL){ 
        checker = strstr(str, "//"); 
        if(checker == str){ 
        }else{ 


         checker = strstr(str,"enabel"); 
         if(checker == str){ 
          checker = strstr(str,"enabel = Y"); 
          if(checker == str){ 
           printf("Database enabeled\n"); 
           enabeled_is_there = 1; 
           output[0] = 1; 
          }else{ 
           enabeled_is_there = 1; 
           printf("Database not enabeled\n"); 
           output[0] = 0; 
          } 
         } 
         checker = strstr(str,"sum"); 
         if (checker == str){ 
          printf("Found sum\n"); 
          sum_is_there = 1; 
    output[1] = sum; 
         } 

         checker = strstr(str,"maxWidraw"); 
         if(checker == str){ 
          printf("Found maxWidraw\n"); 
           max_widraw_is_there = 1; 
    output[2] = max_widraw; 
          } 
          checker = strstr(str,"maxInsert"); 
          if(checker == str){ 
           printf("Found maxInsert\n"); 
           max_insert_is_there = 1; 
    output[3] = max_insert; 

         } 
         checker = strstr(str,"minInsert"); 
         if(checker == str){ 
          printf("Found minInsert\n"); 
          min_insert_is_there = 1; 
output[4] = min_insert; 

         } 
         checker = strstr(str,"minWindraw"); 
         if(checker == str){ 
          printf("Found minWidraw\n"); 
          min_widraw_is_there = 1; 
output[5] = min_widraw; 

         } 


         } 

       } 
       if(!enabeled_is_there 
       || !sum_is_there 
       || !max_insert_is_there 
       || !max_widraw_is_there 
       || !min_widraw_is_there 
       || !min_insert_is_there){ 
        printf("Didn't find one or more of the settings.\nReplacing everything to default settings\n"); 
        printf("If you have the txt file open, please close it.\n"); 
        goto Dinmor; 
       } 
       printf("Clear screen in 2 seconds.\n"); 
       sleep(2); 
       system("cls"); 

      } 
     }  
    return output; 
    } 

這是我的測試程序。 所以在文件中會有例如。 sum = 12345,我想讀取「=」後的數字,所以這裏是我的問題,我將如何獲得文本字符串(所以我可以稍後轉換),即「sum =」之後? 正如你所知道的,我已經在使用strstr(),但通過該命令,我不能簡單地打印在某一行之後寫入的內容。

所有幫助表示讚賞

+4

'strtok':http://man7.org/linux/man-pages/man3/strtok.3。 html或'strchr':http://man7.org/linux/man-pages/man3/strchr.3.html – Downvoter

+4

不要使用標籤和'goto'來代替循環,或者不使用函數。 –

回答

1

可以使用strtok像這樣容易單獨字符串:

char d[2] = '='; // delimiter 
char str[100]; // your string 
char *res; 

// logic to read the line into your string 

res = strtok(str, d); // assuming str = "value1=200" this will return "value1" to res 
res = strtok(NULL, d); // assuming str = "value1=200" this will return "200" 

不過要小心。 strtok修改您傳遞給它的原始字符串。

strtok將分割您傳遞給令牌的字符串。每個字符串的第一個呼叫使用strtok(original_string, delimiter)。當你試圖從同一個字符串獲取其他標記時,每次後續的調用都必須如此製作:strtok(NULL, delimiter)

相關問題