【问题标题】:Duplicate Elimination in CC中的重复消除
【发布时间】:2014-05-27 20:23:18
【问题描述】:

我正在尝试从 clients.txt 中进行重复消除(其中有 7 个姓名和姓氏,其中一些是重复的)。在文件末尾,它将输出写入 output.dat 文件。在编译过程中我没有收到任何错误,但是当我尝试运行它时,它会给出“003.exe 停止工作”错误。 (003.c 是 C 项目名称)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct names
{
    char name[25];
    char surname[25];
};


int main()
{
    int i, j;
    char a[1] = {""};
    struct names name[200];
    FILE *file;
    FILE *file2;
    file = fopen("clients.txt", "r");
    if (ferror(file))
    {
        printf ("File could not be opened");
    }

    while (fscanf(file, "%s", a) == 2)
    {
        i = 0;
        fscanf(file, "%s %s", name[i].name, name[i].surname);
        i++;
    }
    for (i = 0; i < 200; i++)
    {
        for (j = 0; j < 200; j++)
        {
            if (i != j && strcmp(name[i].name, name[j].name) == 0 && strcmp(name[i].surname, name[j].surname) == 0 )
            {
                strcpy(name[j].name, a);
                strcpy(name[j].surname, a);
            }
        }
    }
    fclose(file);
    file2 = fopen("output.dat", "w");
    {
        for (i = 0; i < 200; i++)
        {
            if ( strcmp(name[i].name, "") == 1 )
            {
                fprintf(file, "%s %s\n", name[i].name, name[i].surname);
            }
        }
    }
    fclose(file2);

    system("pause");
    return 0;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。请尽快阅读About 页面。你的fgetc() 应该被while (fscanf(file, "...", ...) == 2) 取代。你也使用free(name[j].name),但你从不使用malloc();这无条件是一个错误。即使只找到 i 条目,您的双嵌套循环也会从 0..200 开始两次。您将需要修复循环索引(或使用int n = i; 并针对n 进行测试)。当您找到重复项时,您需要考虑您想要做什么。从逻辑上讲,您希望将复制后的所有内容移至一行。
  • 我编辑了一下。我将free() 切换为strcpy(),以便将空字符串复制到重复的字符串。对于双嵌套循环,我尝试将一个名称数据与所有其他名称数据进行比较。如果它们匹配,则释放字符串。在下一个循环中,我尝试将那些不为空的文件打印到文件中。现在它编译并运行良好,但 output.dat 包含尴尬的符号。
  • 这是怎么回事:fscanf(file, "%s", a) == 2曾经会是真的吗?

标签: c file struct duplicates double-elimination


【解决方案1】:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct names {
    char name[25];
    char surname[25];
};

int isEqual(struct names *a, struct names *b){
    return strcmp(a->name, b->name) == 0 && strcmp(a->surname, b->surname)==0;
}

int main(){
    int i, j;
    struct names name[200], a;
    FILE *file;

    file = fopen("clients.txt", "r");
    if (!file){//ferror can't use to fopen
        printf ("File could not be opened");
        return -1;
    }

    i=0;
    while (fscanf(file, "%24s %24s", a.name, a.surname) == 2){
        int dup = 0;
        for(j=0; j < i ;++j){
            if(dup=isEqual(&a, &name[j]))
                break;
        }
        if(!dup)//!dup && i<200
            name[i++] = a;
    }
    fclose(file);

    file = fopen("output.dat", "w");
    for (j = 0; j < i; ++j){
        fprintf(file, "%s %s\n", name[j].name, name[j].surname);
    }
    fclose(file);

    system("pause");
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2017-09-21
    • 2018-08-07
    相关资源
    最近更新 更多