0

所以这就是文件中的文本的样子:

    locked y
    locked n
    position 90
    position 2
    audio-language "english"
    audio-language "spanish"

每个文本文件只包含上面显示的一种设置,区别在于选项(y 或 n,1 到 999,“english”“spanish”“whatever else”)它前面有 4 个空格和一个新行每行的末尾,所以我需要一个 fscanf 来提取 char 数组字符串中的参数,但我似乎无法理解如何正确使用分隔符。我必须制作一个可以扫描该文件并打印不同设置以及在文件中找到它们的次数的程序,例如:

audio-language is found with setting: "spanish" 40 times

audio-language is found with setting: "english" 78 times

比你们所有人的答案!在这里,我可以向您展示我是如何使它工作的:

1 - 首先你使用一个功能(我在互联网上找到)消除设置前面的空格:

char* ltrim(char* s) 
{
    char* newstart = s;

  while (isspace( *newstart)) {
    ++newstart;
   }

   // newstart points to first non-whitespace char (which might be '\0')
   memmove( s, newstart, strlen( newstart) + 1); // don't forget to move the '\0' terminator

return s;
 }

2 - 然后 fscanf 没有问题得到正确的行:

while(!feof(fpoo)) {
    fscanf(fpoo,"%s %[^\n]",&first, &sec);

3 - 然后你比较 fscanf 得到的和用户输入的

就是这样,显然fscanf的整个问题是因为每行前面的空格。

4

4 回答 4

2

我编译了这段代码并对其进行了测试。完美运行。你也可以测试它:)

int main()
{
    int bytes_read = 0, english_count = 0, spanish_count = 0;
    char file_buffer[256], setting[127], option[32], fileName[32] = "FileName.txt";
    FILE *fp;
    if((fp = fopen("FileName.txt", "rb")) <=0)
    {
       printf("Unable to open the File\n");
    }
    //rewind(fp);
    while(fgets (file_buffer, 50, fp))
    {
       sscanf(file_buffer,"%s %s",setting,option);
       printf("Setting: ");
       printf("%s",setting);
       printf("\nOption: ");
       printf("%s",option);
       printf("\n\n");
       if(memcmp(setting,"audio-language",14) == 0)
       {
           if(memcmp(option,"english",7) == 0)
           {
               english_count = english_count+1;
           }
           if(memcmp(option,"spanish",7) == 0)
           {
               spanish_count = spanish_count+1;
           }
       }
           //you can add more checks here.
      }
    printf("\naudio-language is found with setting: english ");
    printf("%d",english_count);
    printf("\naudio-language is found with setting: spanish ");
    printf("%d",spanish_count);
    printf("\n");

return 0;
}
于 2013-07-26T09:16:46.273 回答
1

您可以首先将字符串读取为:

scanf("%[^\n]s",buffer)

现在您可以使用 C 中的分词器通过strtok获取语言的值

char *token="\n";
token = strtok(line, token);

现在令牌将包含所有拆分词,您可以将其与 strcmp 与可能的语言进行比较并保持计数

于 2013-07-26T08:44:21.833 回答
0

检查算法,你必须编码

  1. 逐行读取文件,例如一次说 1024 个字节。
  2. 然后扫描您想要的行,例如在您的情况下音频语言“英语”或音频语言“西班牙语”。
  3. 获取语​​言类型的字符串
  4. 然后计算你找到这条线的次数。

我认为这可以解决您的问题。

于 2013-07-26T08:21:23.433 回答
0

你评论里的人是对的。你真的应该向我们展示你的尝试。如果这是家庭作业,那么您就不能作弊,因为您的讲师可以通过简单的谷歌搜索将您淘汰。期望我们在这里猜测您的理解并涵盖您已经涵盖的内容是浪费的,因此请向我们展示您所做的事情。

qsort,bsearchstrcmp为以字符串开头的任何内容(例如,以字符串为前缀的结构)提供简单、相对有效的排序和搜索机制。格式字符串fscanf中的空格表示尽可能多地使用空格。fscanf返回一个指示成功和失败模式的值,这意味着您不一定需要将每一行读入一个字符数组来测试输入是否与特定格式匹配。但是,您在设计文件时确实需要小心,以使所有命令都没有与其他命令相同的前缀。在这种情况下,你是安全的。

这是我的尝试。调试愉快:)

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct language_table { char language[64]; size_t count; };
struct language_table *parse(FILE *f, size_t *s);

int main(void) {
    size_t s;
    struct language_table *t = parse(stdin, &s);

    while (s-- > 0) {
        printf("audio-language is found with setting: %s %zu times\n", t[s].language, t[s].count);
    }

    free(t);
}

struct language_table *parse(FILE *f, size_t *s) {
    char locked, language[64];
    unsigned int position;

    int c = 0;

    struct language_table *table = NULL, *entry;
    size_t size = 0;

    do {
        if (fscanf(f, " locked %c", &locked) == 1 && strchr("ynYN", locked)) {
            /* XXX: Do something with locked. */
        }
        else if (fscanf(f, " position %u", &position) == 1) {
            /* XXX: Do something with position. */
        }
        else if (fscanf(f, " audio-language %63s", language) == 1) {
            /* search for the language in table. */
            entry = bsearch(language, table, size, sizeof *table, strcmp);

            /* insert the language if it's not found. */
            if (entry == NULL) {
                if ((size & (size + 1)) == 0) {
                    void *new = realloc(table, (size * 2 + 1) * sizeof *table);
                    if (new == NULL) {
                        fprintf(stderr, "failed to allocate new entry for %s\n", language);
                        free(table);
                        return NULL;
                    }
                    table = new;
                }

                entry = table + size++;
                strcpy(entry->language, language);
                entry->count = 0;

                /* sort & search */
                qsort(table, size, sizeof *table, strcmp);
                entry = bsearch(language, table, size, sizeof *table, strcmp);
            }

            entry->count++;
        }
        else {
            /* consume an invalid line */
            do {
                c = fgetc(f);
            } while (c >= 0 && c != '\n');
        }
    } while (c >= 0);

    *s = size;
    return table;
}
于 2013-07-27T01:39:28.033 回答