【问题标题】:How to search for specific strings and integers in a .txt file C and use them?如何在 .txt 文件 C 中搜索特定字符串和整数并使用它们?
【发布时间】:2016-10-28 02:31:46
【问题描述】:

这段代码是一个关于文件 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;
}

基本上,用户必须在其中输入 2 个名称并与随机数一起保存在 .txt 文件中。但是在保存名称之前,程序必须检查它们是否以前以相同的顺序使用过并且已经在文件中。如果之前确实使用过,则程序不得生成新号码,而是将保存的号码与 2 个名称一起使用并在屏幕上打印出来。

我已经阅读了一整天,但我只是不知道如何以及如何实现以使程序能够读取以前保存的名称和数字并在找到匹配项时使用它们。

【问题讨论】:

    标签: c string file search integer


    【解决方案1】:

    我一整天都在阅读,但我就是不知道如何阅读以及阅读什么 实现使程序能够读取以前保存的名称和 数字并在找到匹配项时使用它们。

    您已经正确描述了需要做什么。为了做到这一点,你必须

    • 以允许读取的模式打开文件(a+ 而不是a)
    • 读取保存的姓名和号码 e. G。与fscanf()
    • 将它们与输入的名称与strcmp() 进行比较
    • 如果名称匹配,则分配保存的号码

    -所以改变

        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);
    

    到

        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);
    

    【讨论】:

      【解决方案2】:

      您可以将您的位置保存在文件中,从头开始扫描,然后返回保存的位置。

      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)
      

      希望我理解你的正确:)

      【讨论】:

        【解决方案3】:

        您要做的是解析文件。为了对文本文件中的字符串执行此操作,您需要某种方式知道在哪里可以找到名称以及为名称存储的结果。

        正如前面的答案所建议的那样,使用职位是您真正应该考虑的事情。除此之外,也许您想用空格或字符分隔名称和结果,并用换行符分隔每个新条目。您可以将其与已保存的位置结合使用,以判断您在哪个条目,以及您正在阅读的是名称还是结果。

        进一步,您可能应该阅读“fopen”文档,例如this

        我希望这就是你要找的。​​p>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-14
          • 2015-09-25
          • 1970-01-01
          • 1970-01-01
          • 2021-10-20
          • 1970-01-01
          • 2014-02-06
          • 1970-01-01
          相关资源
          最近更新 更多