我试图通过写入和读取.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我希望在&#34; =&#34;之后读取数字,所以这是我的问题,我怎样才能获得文本字符串(以后我可以将其转换) #34; sum =&#34;?
正如你可以告诉我已经使用strstr()
但是使用那个命令我不能简单地打印在某一行之后写的内容。
感谢所有帮助
答案 0 :(得分: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)