【问题标题】:Read file .txt and save it to struct in c读取文件 .txt 并将其保存到 c 中的 struct
【发布时间】:2014-01-15 04:33:24
【问题描述】:

我想问一下 C 语言中的文件处理和结构,我从我的讲座中得到一个作业,并且对 C 编程中的字符串操作感到非常困惑。这是任务。

  1. mhs.txt 获取数据
  2. 存储在 struct
  3. 排序按名称升序

这是 mhs.txt

1701289436#ANDI#1982
1701317124#WILSON#1972
1701331734#CHRISTOPHER STANLEY#1963
1701331652#SHINVANNI THEODORE#1962
1701331141#MUHAMMAD IMDAAD ZAKARIA#1953
1701331564#MARCELLO GENESIS DRIET J.#1942
1701322282#ANANDA AULIA#1972
1701329175#LORIS TUJIBA SOEJONOPOETRO#1983
1701301422#DEWI JULITA#1993
1701332610#HARRY HUTALIANG#1982

# 之前的第一个是 NIM, 在第一个 # 之后是名称 # 之后的最后一个是年份

这就是我所做的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student{
    char nim[11];
    char name[50];
    int year;
}s[10];
int main(){
    FILE *fp;
    int c,i,n;

    printf("Read mhs.txt...");
    getchar();

    fp  = fopen("mhs.txt", "r");
    c   = getc(fp);
    i   = 0;
    while(c!=EOF){
        printf("%c", c);
        c           = getc(fp);
        i++;
    }
    fclose(fp);
    getchar();
    return 0;
}

首先,我可以将数据保存在 struct 上,但在这里我很困惑如何分隔字符串。

这就是我对结构和文件处理的全部了解,有没有人可以帮助我?我在互联网上四处游历,找不到正确的结果。

如果有重复的问题,请见谅,如果我的英语太差,请见谅。

【问题讨论】:

  • 提示:使用 strtok 分隔这些字符串。并使用 strcpy 放入您的结构 s[i].name 等。
  • @Digital_Reality 好的谢谢,我会尝试
  • 你应该逐行阅读.. getline 可能是..现在你一次只阅读一个字符。
  • 更好的提示:忘记你曾经听说过strtok。尤其是在 C++ 中,基本上总是有更好的方法(即使在 C 中,实际上总是有更好的方法)。

标签: c file struct


【解决方案1】:

这是纯C代码,你应该新增三个导入函数:strtok & qsort & fsan

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
    char nim[11];
    char name[50];
    int year;
};

#define BUFFER_SIZE 100

struct student saveToStruct (char* str)
{
    struct student res;
    int flag = 0;
    char *token = strtok(str, "#"); 

    while( token != NULL )
    {
        if (0 == flag)
            strcpy(res.nim, token);
        else if (1 == flag)
            strcpy(res.name, token);
        else
            res.year = atoi(token);

        flag++;
        token = strtok( NULL, "#" ); 
    }
    return res;
}
void print(struct student* arr, int size)
{
    for (int i = 0; i < size; i++)
    {
        printf("%s, %s, %d\n", arr[i].nim, arr[i].name, arr[i].year);
    }
}
int cmp(const void* l, const void* r)
{
    return strcmp(((const student*)l)->name, ((const student*)r)->name);
}
int main()
{
    struct student arr[10];
    FILE* file = fopen("mhs.txt", "r");
    if (!file)
        return -1;

    char buffer[BUFFER_SIZE];
    int flag = 0;
    while (fgets(buffer, BUFFER_SIZE, file))
    {
        arr[flag] = saveToStruct(buffer);
        flag++;
    }
    print(arr, 10);

    qsort(arr, 10, sizeof(struct student), cmp);
    printf("After sort by name!\n");
    print(arr, 10);
    return 0;
}

【讨论】:

    【解决方案2】:

    由于您已将其标记为 C++,因此我将使用 C++:

    #include <iostream>
    #include <string>
    #include <algorithm>
    
    struct student { 
        std::string nim;
        std::string name;
        int year;
    
        bool operator<(student const &other) { 
            return name < other.name;
        }
    
        friend std::istream &operator>>(std::istream &is, student &s) { 
            std::getline(is, s.nim, '#');
            std::getline(is, s.name, '#');
            return is >> s.year;
        }    
    };
    
    int main() { 
        std::ifstream in("mhs.txt");
    
        std::vector<student> students{
            std::istream_iterator<student>(in),
            std::istream_iterator<student>()
        };
    
        std::sort(students.begin(), students.end());
    }
    

    如果您想在 C 中完成大致相同的事情,使用扫描集转换使用fscanf 进行读取可能是最简单的,例如:

    fscanf(infile, "%10[^#]#%49[^#]#%d", student.nim, student.name, &student.year);
    

    扫描集转换为您提供类似于正则表达式子集的东西,因此%[^#] 将字符串转换为(但不包括)#。在这种情况下,我将每个的长度限制为比您在结构定义中为数组指定的长度小一,以防止缓冲区溢出。

    然后您可以使用qsort 进行排序。您需要编写一个比较函数,但正确地做到这一点并不总是显而易见的:

    int cmp(void const *aa, void const *bb) {
        student const *a = aa;
        student const *b = bb;
    
        return strcmp(a->name, b->name);
    }
    

    【讨论】:

    • 简单明了,显然是最好的。
    【解决方案3】:

    这里有一些提示,不是完整的答案。希望对你有帮助。

    首先您需要逐行读取文件,而不是逐字符读取。你需要fgets()的功能。您可以从 www.cplusplus.com/reference/cstdio/fgets/ 找到参考资料

    其次,您可以使用strtok() 来分隔字符串。这是一个例子。

    char str[] = "now # is the time for all # good men to come to the # aid of their country";
    char delims[] = "#";
    char *result = NULL;
    result = strtok( str, delims );
    while( result != NULL ) {
        printf( "result is \"%s\"\n", result );
        result = strtok( NULL, delims );
    }  
    

    您可以从http://www.cplusplus.com/reference/cstring/strtok/ 中找到对strtok() 的引用

    第三,使用qsort()对结构体数组进行排序。您可以从http://www.cplusplus.com/reference/cstdlib/qsort/ 找到它的参考。也可以在那里找到示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多