- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过写入和读取 .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)
关于C 逐字读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33210719/
我正在尝试返回数组“seats”,它本质上应该从文本文件返回数据 - 15x30 网格“#”。我已经尝试了很多东西,但我感到很沮丧,因为我对 java 的经验很少。我的代码可以编译,但在调用该方法时无
对于任何输入的字符串,我们需要按任意顺序的单词匹配来查找 super 字符串。即输入字符串中的所有单词必须以任何顺序出现在输出字符串中。例如给定数据集:“字符串搜索”“Java 字符串搜索”“手动 C
我有一个文本文件,其中包含一些我想放入二维数组中的内容。该文本文件由等长的句子组成。如何将每个单词放入数组? 文本文件的例子是- This is stackoverflow I am user 这个文
我正在编写一个实用程序,它接受一个 .resx 文件并创建一个包含 .resx 文件中所有名称/值对属性的 javascript 对象。这一切都很好,直到 .resx 中的值之一是 该经销商接受电子订
我输入了大量的数学表达式和方程式,我想为每个表达式和方程式打印出 latex 表示形式。到目前为止,我已经尝试过 Sage 和 sympy,但棘手的部分是不对表达式中的术语重新排序。 所以,如果我的输
我正在尝试通过实现异步任务在 android (java) 中流式传输 chatgpt api 的响应(逐字),但我收到错误。我正在将 java 的 HTTPurlconnection 库与输入和输出
我正在尝试通过实现异步任务在 android (java) 中流式传输 chatgpt api 的响应(逐字),但我收到错误。我正在将 java 的 HTTPurlconnection 库与输入和输出
我是一名优秀的程序员,十分优秀!