简体   繁体   English

如何在.txt文件C中搜索特定的字符串和整数并使用它们?

[英]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 为了做到这一点,你必须这样做

  • open the file in a mode which allows reading ( a+ instead of a ) 以允许读取的模式打开文件( a+而不是a
  • read the saved names and numbers eg with fscanf() 读取保存的名称和数字,例如使用fscanf()
  • compare them to the entered names with strcmp() 使用strcmp()将它们与输入的名称进行比较
  • assign the saved number if the names match 如果名称匹配,则分配保存的号码

- 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.

相关问题 如何使用C从.txt文件的特定点读取整数? - How to read integers from a specific point of a .txt file using C? 从.txt文件获取整数并在c中对它们执行操作 - Getting integers from .txt file and perfoming actions on them in c 如何从 .t​​xt 文件中读取整数并将它们存储在 C 中的整数类型数组中 - How to read integers from .txt file and store them in a integer type array in C 如何从 C 中的整数和字符串文件中读取整数 - How to read integers from file with integers and strings in C 读取 .txt 文件并将单词/整数作为字符串获取 - Reading a .txt File and getting words/integers as strings C中如何忽略txt文件的特定部分 - How to ignore a specific part of txt file in C 如何在C中编辑txt文件的特定行 - How to edit a specific line of a txt file in C 如何在 c 中将堆栈(数组)用于字符串而不是字符或整数? - How to use a stack(array) for strings instead of characters or integers in c? 从C中的txt文件读取整数 - Reading integers from txt file in C 如何在 txt 文件中的字符串上正确限制 fscanf? (需要读取用空格和“-”分隔的字符串和整数) - How to restrict fscanf properly on a string from a txt file? (need to read strings and integers seperated with whitespace AND “-”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM