[英]How to search for specific strings and integers in a .txt file C and use them?
This code is a small learning experience about File I/O which i can't quite finish yet: 这段代码是一个关于文件I / O的小型学习经验,我还没有完成:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
FILE *fP;
char so_name[64];
char user_name[64];
int number;
char continue_button;
time_t t;
system("TITLE Love-0-meter");
system("COLOR 4D");
srand((unsigned) time(&t));
number = (rand() %100);
printf("Hi and welcome to the love-o-meter, please fill in the details and see how much you and your SO"
" love each other!\n\nHave you used this program before?(Y/N)");
do
{
scanf(" %c", &continue_button);
}while(continue_button != 'n' && continue_button != 'N' && continue_button != 'Y' && continue_button != 'y');
system("CLS");
printf("Enter your SO's name: ");
scanf(" %s", so_name);
printf("Enter your name: ");
scanf(" %s", user_name);
printf("\n%s and %s, you love each other %d%%", so_name, user_name, number);
if(continue_button == 'N' || continue_button == 'n')
{
fP = fopen("Love-o-meter.txt", "w");
}
else
{
fP = fopen("Love-o-meter.txt", "a");
}
fprintf(fP, "%s %s %i\n", so_name, user_name, number);
fclose(fP);
return 0;
}
Basically the user must enter 2 names in and be saved in a .txt file along with a random number. 基本上用户必须输入2个名称并将其与随机数一起保存在.txt文件中。 But before the names get saved the program must check if they've been used before in the same order and are already in the file.
但是在名称保存之前,程序必须检查它们之前是否以相同的顺序使用过,并且已经在文件中。 If they have indeed been used before, the program must not generate a new number but use the one saved together with the 2 names and print it out on the screen.
如果之前确实使用过它们,程序不能生成新的数字,而是使用与2个名称一起保存的数字并将其打印出来。
I've been reading all day but i just can't figure out how and what to implement to be able the program to read previously saved names and numbers and use them if it finds a match. 我一直在阅读,但我无法弄清楚如何以及如何实现该程序能够读取以前保存的名称和数字,并在找到匹配时使用它们。
You can save your position in file, scan it from beginning and then return to saved position. 您可以将您的位置保存在文件中,从头开始扫描,然后返回到保存位置。
char name1[256], name2[256];
int val;
size_t pos = ftell(fP);
rewind(fP);
while(fscanf(fp, "%s %s %d", name1, name2, &val) == 3) {
// compare everything you need
}
fseek(fP, pos, SEEK_SET)
I hope I understood you right :) 我希望我理解你的权利:)
What you want to do is to parse the file. 你想要做的是解析文件。 In order to do this with strings in a text file, you need some way of knowing where to find names and results stored for the names.
为了在文本文件中使用字符串执行此操作,您需要一些方法来了解在何处查找为名称存储的名称和结果。
As the previous answer proposed, using positions is something you should really look into. 正如前面提到的答案,使用职位是你应该真正研究的问题。 In addition to this, perhaps you want to separate names and results with a space or a character, and each new entry with a newline.
除此之外,您可能希望使用空格或字符分隔名称和结果,并使用换行符分隔每个新条目。 You can use this, combined with a saved position, to tell at what entry you are, and whether you are reading a name or a result.
您可以使用此功能,结合保存的位置,告诉您输入的内容,以及您是否正在阅读姓名或结果。
Further on, you should probably do some reading on the 'fopen' documentation, eg this 另外,你可能应该做对“的fopen”文档一些阅读,如本
I do hope this was what you looked for. 我希望这就是你所寻求的。
I've been reading all day but i just can't figure out how and what to implement to be able the program to read previously saved names and numbers and use them if it finds a match.
我一直在阅读,但我无法弄清楚如何以及如何实现该程序能够读取以前保存的名称和数字,并在找到匹配时使用它们。
You already described correctly what needs to be done. 您已经正确描述了需要完成的工作。 In order to do it, you have to
为了做到这一点,你必须这样做
a+
instead of a
) a+
而不是a
) fscanf()
fscanf()
strcmp()
strcmp()
将它们与输入的名称进行比较 - so change - 所以改变
printf("\n%s and %s, you love each other %d%%", so_name, user_name, number);
if(continue_button == 'N' || continue_button == 'n')
{
fP = fopen("Love-o-meter.txt", "w");
}
else
{
fP = fopen("Love-o-meter.txt", "a");
}
fprintf(fP, "%s %s %i\n", so_name, user_name, number);
to 至
if(continue_button == 'N' || continue_button == 'n')
{
fP = fopen("Love-o-meter.txt", "w");
}
else
{
char so_used[64];
char user_used[64];
int nuused;
fP = fopen("Love-o-meter.txt", "a+"); // append + read
while (fscanf(fP, "%s %s %i\n", so_used, user_used, &nuused) == 3)
if (!strcmp(so_used, so_name) && !strcmp(user_used, user_name))
{
number = nuused;
goto out;
}
}
fprintf(fP, "%s %s %i\n", so_name, user_name, number);
out:
printf("%s and %s, you love each other %d%%\n", so_name, user_name, number);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.