C 一个字一个字地读

C reading word after word

我正在尝试通过写入和读取 to/from .txt 文件在 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() 但使用该命令我不能简单地打印某行之后写的内容。

感谢所有帮助

您可以使用 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)