简体   繁体   English

使用数组读取和写入文本文件

[英]Reading from and writing to text files using arrays

I am working on a program in the C language for a class and I am stuck.我正在为一个班级使用 C 语言编写程序,但我被卡住了。 I have been researching everywhere (textbooks, Internet, classmates, class notes) and I can't seem to find the problem.我到处搜索(教科书、互联网、同学、课堂笔记),似乎找不到问题所在。 Here is the following assignment:这是以下任务:

Write a function that reads an input file and creates a sorted output file.编写一个函数来读取输入文件并创建一个排序的输出文件。 The function prototype is void file_sort(char *infile, char *outfile).函数原型是 void file_sort(char *infile, char *outfile)。 The function takes the file names as parameters.该函数将文件名作为参数。 The input file contains student information and has the following format.输入文件包含学生信息并具有以下格式。 The first line contains the number of students in the file.第一行包含文件中的学生人数。 Each subsequent line contains information about one student.随后的每一行都包含有关一名学生的信息。 Each line contains three fields namely student ID, grade, and GPA in that order separated by blanks.每行包含三个字段,即学生 ID、成绩和 GPA,按顺序由空格分隔。 The student ID is a positive integer.学生 ID 是一个正整数。 The grade is a character.等级是一个字符。 The GPA is a double type value. GPA 是双精度值。 The function sorts the student information in the ascending order of student ID.该函数按学生ID升序对学生信息进行排序。 The ordered student information is written to the output file.有序的学生信息被写入输出文件。 The output file has the same format as the input file.输出文件的格式与输入文件的格式相同。 The function dynamically allocates three arrays and stores the student information into the arrays.该函数动态分配三个数组并将学生信息存储到数组中。 The three arrays are simultaneously sorted.三个数组同时排序。 The sorted array information is written to the output file.排序后的数组信息将写入输出文件。 The dynamic arrays are freed when the function completes its work.当函数完成其工作时,动态数组被释放。 Structures should not be used.不应使用结构。

Just a couple of notes: (1) I have been programming in the Java language for approximately 3 years.只是一些注意事项: (1) 我已经使用 Java 语言进行编程大约 3 年了。 Programming isn't necessarily new to me.编程对我来说并不陌生。 The C language is however.但是C语言是。 (2) If I have not properly presented my question, you may certainly say so but please be considerate. (2) 如果我没有恰当地提出我的问题,你当然可以这样说,但请体谅。 After all, I still consider myself a "newbie".毕竟,我仍然认为自己是一个“新手”。

Here is my current code.这是我当前的代码。 The first block is my calling of the function file_sort() in main.第一个块是我在 main 中调用 file_sort() 函数。 It is menu driven (1-11 options for the user. This is option 9).它是菜单驱动的(用户有 1-11 个选项。这是选项 9)。

else if (response == 9)
    {
        char *input_file_name;
        char *output_file_name;
        printf("Enter input file name: ");
        scanf("%s", input_file_name);
        printf("Enter output file name: ");
        scanf("%s", output_file_name);
        file_sort(input_file_name, output_file_name);
        printf("File sorted and rewritten\n");
    }

Here is the function declared outside of main.这是在 main 之外声明的函数。

void file_sort(char *infile, char *outfile)
{
    FILE *input;
    FILE *output;
    input = fopen(infile, "r");
    output = fopen(outfile, "w");
    int size = 100;
    int *id_array;
    id_array = (int*)malloc(size*sizeof(int));
    char *grade_array;
    grade_array = (char*)malloc(size*sizeof(char));
    double *gpa_array;
    gpa_array = (double*)malloc(size*sizeof(double));
    char grade;
    int id;
    double gpa;
    int number_students, j;
    if (input == NULL)
    {
        printf("File could not be opened\n");
    }
    fscanf(input, "%d", &number_students);
    while ((fscanf(input,"%d %c %lf", &id, &grade, &gpa)) == 3)
    {
        j = number_students;
        while((j >= 0) && (id_array[j] > id))
        {
            if (id_array[0] == 0)
            {
                id_array[0] = id;
                break;
            }
            id_array[j + 1] = id_array[j];
            j--;
        }
        if (j < 0)
        {
            j = 0;
        }
        id_array[j] = id;
        gpa_array[j] = gpa;
        grade_array[j] = grade;
    }

    int i;
    for (i = 0; i <= number_students; i++)
    {
        fprintf(output, "%d %c %lf\n", id_array[i], grade_array[i], gpa_array[i]);
    }
    free(id_array);
    free(grade_array);
    free(gpa_array);
    fclose(input);
    fclose(output);
}

If there is any information I need to include to help answer this question better, please let me know.如果我需要提供任何信息以帮助更好地回答这个问题,请告诉我。 Thank you very much.非常感谢。

It appears that the sorting is not working.似乎排序不起作用。 Below I have posted the file which needs to be sorted along with the final output.下面我发布了需要与最终输出一起排序的文件。

To be sorted (first number declares number of students):要排序(第一个数字声明学生人数):

5
6029 A 3.5
3920 B 2.3
9287 A 3.1
1029 C 1.7
8391 B 2.9

"Sorted" file after running:运行后“排序”文件:

1029 C 1.700000
8391 B 2.900000
9287 £ 0.000000
9287   0.000000
10696720 ¸ 0.000000
10696720  0.000000'

??? ???

I am not sure about exact problem.我不确定确切的问题。

one visible issue is in else if (response == 9) section一个明显的问题是在else if (response == 9)部分

char *input_file_name;
char *output_file_name;

are char pointers.是字符指针。 These pointers used to get the file names.这些指针用于获取文件名。 But there is no memory is allocated for this.但是没有为此分配内存。

else if (response == 9)
    {
        char *input_file_name; <------
        char *output_file_name;
        printf("Enter input file name: "); 
        scanf("%s", input_file_name);<--------------
        printf("Enter output file name: ");
        scanf("%s", output_file_name);<--------------
        file_sort(input_file_name, output_file_name);
        printf("File sorted and rewritten\n");
    }

Either allocate dynamically or make it as char array.动态分配或将其设为字符数组。

char input_file_name[50];
char output_file_name[50];

i dont know but maybe you can do this too?我不知道,但也许你也可以这样做?

int i;国际我; for (i = 0; i <= number_students; i++, i--) { for (i = 0; i <= number_students; i++, i--) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM