如何在2D char数组中存储元素并打印它(从文件中读取时) [英] How to store elements in a 2D char array and print it (while reading from a file)

查看:89
本文介绍了如何在2D char数组中存储元素并打印它(从文件中读取时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常复杂的问题(至少对我而言),在我解释它的时候对我很勉强。



最初的问题是从学生数据库中读取,搜索学生类型(例如欧洲,国际,资助等)并按名称按字母顺序排序。



首先,我从每个单独的文件行中读取使用fgets()并使用sscanf()解析每一行以在所述变量中存储学生姓氏和名字。



最后我使用fprintf()将每个文件写入一个文件,其中包含表格格式的所有学生姓名。



到目前为止很容易。



现在是复杂的部分。



为了保存组合文件中的所有名称,我使用了2D char数组。这是2D,因为我需要存储每个ROW编号和#define BUFFER来指定一行中有多少个字符。



现在我需要将2D数组传递给一个函数搜索学生并对列表进行排序。我试了一下,但我只能显示一个名字或随机字符。



以下是我的代码到目前为止的片段...



我尝试过:



This is quite a complex problem (at least for me) so bare with me while I explain it.

The initial problem is to read from a student database, search for the type of student (e.g. European, International, Funded etc.) and sort alphabetically by name.

To begin with I read from each individual file line by line using fgets() and parsed each line using sscanf() to store the students surname and first name in said variables.

Finally I used fprintf() to write each file to one single file which contains all the names of students in tabular format.

Easy enough so far.

Now comes the complicated part.

To save all the names from the combined file I used a 2D char array. It's 2D because I need to store each ROW number and #define BUFFER to specify how many chars in a line.

Now I need to pass the 2D array to a function to search for a student and sort the list. I have given it a try but I can only get it to display one name or random characters.

Below is snippets of my code so far...

What I have tried:

void parse_file_and_sort(void)
{
    FILE *fp = fopen("Combined.dat", "rb");
    // 2D array
    static char student[SIZE][BUFFER];

    unsigned short total;
    unsigned short i = 0;

    char input;
    char search_key[KEY_ID];

    if (fp != NULL)
    {
        while((fgets(student[i], BUFFER, fp)) != NULL)
        {
            // remove newline 
            if (student[i][strlen(student[i] - 1)] == '\n')
            {
                student[i][(strlen(student[i]) - 1)] = '\0';
                i++;
            }
        } // end while

        total = i;

        for(i = 0; i < total; i++)
        {
            // remove headings
            if (i == 0)
            {
                student[i][i] = '\0';
            }
        } // end for

        // close file stream
        fclose(fp);
        
        // passing array to function which will search for a keyword
        linear_search(student);
}

void linear_search(char student[][BUFFER])
{
    unsigned short i;

    for(i = 0; i < SIZE; i++)
    {
        printf("%-2u | %s\n", i, student[i]);
    }
}

推荐答案

从不使用二维数组开始:创建一个Student结构并构建一个链表(或只有已知最大学生数的数组)才能保存它们。

Student结构有FirstName,LastName,Type的单独指针。

不要费心保留一个名字的长度,只需将null终止并将其倾斜即可。



每次读取一行时,将其解析为一个新的Student实例,并将学生添加到您的收藏中。创建一个接受字符串(行)并返回指向新学生的指针的函数。



排序只是将指针交换到Student Struct的问题关于对象!



搜索只是一个简单的列表查找。



如果你写一个函数需要两个学生指针A& B和A返回-1表示A大于B,0表示相等,1表示A小于B,你可以使用相同的函数进行排序和搜索!



合理?试一试!
Start off by not using a 2d array: create a Student struct and build a linked list (or an array only if there is a known maximum number of students) to hold them.
The Student struct has separate pointers for FirstName, LastName, Type.
Don't bother keeping a length of name, just null terminate each and lean it at that.

Each time you read a line, parse it into a new Student instance, and add the student to your collection. Create a function that takes a string (the line) and returns a pointer to a new Student.

Sorting is then just a matter of swapping the pointers to the Student Struct object about!

Searching is just a simple list lookup.

If you write a function that takes two Student pointers A & B and returns -1 for A greater than B, 0 for equal, and 1 for A less than B, you can use the same function for sorting and searching!

Make sense? Give it a try!


这篇关于如何在2D char数组中存储元素并打印它(从文件中读取时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆