使用 struct 和 stdin 在 C 中输出的问题

Issues with outputs in C using struct and stdin

我目前正在完成一项计算机科学作业,但已经到了无法解决的地步。我已经获得了代码,我要编辑这些代码来做一些我到目前为止能够做的事情,但我似乎无法进行下一步。该代码从标准输入中读取一些学生的姓名和年龄,直到被 EOF(控件 D 等)终止,构建它们的链表。从每个输入行读取一名学生。 到目前为止我的代码是:

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

#define MAX_LINE_LENGTH 80      // The longest line this program will accept
#define MAX_NUM_STUDENTS 500    // The maximum number of students this program can handle
#define MAX_NAME_SIZE 50        // The maximum allowable name length

// The declaration of the student record (or struct). Note that
// the struct contains the name as an array of characters, rather than
// containing just a pointer to the name as before.

typedef struct student_s Student;

struct student_s {
    char name[MAX_NAME_SIZE];
    int age;
    Student* next;              // Pointer to next student in a list
};

// Create a pool of student records to be allocated on demand

Student studentPool[MAX_NUM_STUDENTS];  // The student pool
int firstFree = 0;

// Return a pointer to a new student record from the pool, after
// filling in the provided name and age fields. Returns NULL if
// the student pool is exhausted.
Student* newStudent(const char* name, int age) {
    Student* student = NULL;
    if (firstFree < MAX_NUM_STUDENTS) {
        student = &studentPool[firstFree];
        firstFree += 1;
        strncpy(student->name, name, MAX_NAME_SIZE);
        student->name[MAX_NAME_SIZE - 1] = '[=10=]';  // Make sure it's terminated
        student->age = age;
        student->next = NULL;
    }
    return student;
}

// Read a single student from a csv input file with student name in first column,
// and student age in second.
// Returns: A pointer to a Student record, or NULL if EOF or an invalid
// student record is read. Blank lines, or lines in which the name is
// longer than the provided name buffer, or there is no comma in the line
// are considered invalid.
Student* readOneStudent(FILE* file)
{
    char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
    Student* student = NULL;       // Pointer to a student record from the pool

    // Read a line, extract name and age

    char* cp = fgets(buffer, MAX_LINE_LENGTH, file);
    if (cp != NULL) {           // Proceed only if we read something
        char* commaPos = strchr(buffer, ',');
        if (commaPos != NULL && commaPos > buffer) {
            int age = atoi(commaPos + 1);
            *commaPos = '[=10=]';  // null-terminate the name
            student = newStudent(buffer, age);
        }
    }
    return student;
}

// Reads a list of students from a given file. Input stops when
// a blank line is read, or an EOF occurs, or an illegal input
// line is encountered.
// Returns a pointer to the first student in the list or NULL if no
// valid student records could be read.
Student* readStudents(FILE *file)
{
    Student* first = NULL;     // Pointer to the first student in the list
    Student* last = NULL;      // Pointer to the last student in the list
    Student* student = readOneStudent(file);
    while (student != NULL) {
        if (first == NULL) {
            first = last = student;   // Empty list case
        }
        else {
            last->next = student;
            last = student;
        }
        student= readOneStudent(file);
    }
    return first;
}

// printOneStudent: prints a single student, passed by value
void printOneStudent(Student student)
{
    printf("%d (%s)\n", student.age, student.name);
}


// printStudents: print all students in a list of students, passed
// by reference
void printStudents(const Student* student)
{
    while (student != NULL) {
        printOneStudent(*student);
        student = student->next;
    }
}


int main(void)
{
    FILE* inputFile = stdin;
    if (inputFile == NULL) {
        fprintf(stderr, "File not found\n");
    }
    else {
        Student* studentList = readStudents(inputFile);
        printStudents(studentList);
    }
}

代码需要做的是获取输入列表,例如:

21, Fred Nurk
92, Arwen Evensong

和return学生名单如下:

Fred Nurk (21)
Arwen Evensong (92)

但是我的代码 returns:

 0 (21)
 0 (92)

我不知道为什么。输入已设置且无法更改,对问题的任何编辑都必须专门对代码进行。

让我们检查以下代码片段

Student* readOneStudent(FILE* file)
{
    char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
    Student* student = NULL;       // Pointer to a student record from the pool

// Read a line, extract name and age

    char* cp = fgets(buffer, MAX_LINE_LENGTH, file);
    if (cp != NULL) {           // Proceed only if we read something
        char* commaPos = strchr(buffer, ',');
        if (commaPos != NULL && commaPos > buffer) {
            int age = atoi(commaPos + 1);
            *commaPos = '[=10=]';  // null-terminate the name
            student = newStudent(buffer, age);
        }
    }
    return student;
}

根据您的说法,输入应该是:

age,name

下一行查找逗号分隔符的地址

char* commaPos = strchr(buffer, ',');

并通过以下行提取年龄:

int age = atoi(commaPos + 1);

因此它采用逗号分隔符的地址并递增 1

age,[n]ame

所以你的假设是不正确的。输入应该是相反的顺序

name,age

输出将是

age (name)

编辑: 要交换输入形式,只需在 Student* readOneStudent(FILE* file) 中交换以下内容:

int age = atoi(buffer);
student = newStudent(commaPos + 1, age);

并交换 void printOneStudent(Student student) 中的输出形式:

printf("%s (%d)\n", student.name, student.age);

在您的函数 Student* readOneStudent(FILE* file); 中,您希望以这种格式输入:

Fred Nurk,21
Arwen Evensong,92

如果您希望您的输入采用这种形式:

21, Fred Nurk
92, Arwen Evensong

那么这可能会有所帮助:

Student* readOneStudent(FILE* file)
{
    char *buffer;  // Buffer into which we read a line from stdin
    buffer = (char *)malloc(MAX_LINE_LENGTH);
    Student* student = NULL;       // Pointer to a student record from the pool

    // Read a line, extract name and age

    char* cp = fgets(buffer, MAX_LINE_LENGTH, file);
    if (cp != NULL) {           // Proceed only if we read something
        char* commaPos = strchr(buffer, ',');
        if (commaPos != NULL && commaPos > buffer) {
            buffer[strlen(buffer)-1] = '[=12=]';
            *commaPos = '[=12=]';  // null-terminate the age part
            int age = atoi(buffer);
            student = newStudent(commaPos+2, age);
        }
    }
    return student;
}

编辑:

void printOneStudent(Student student)
{
    printf("%s (%d)\n", student.name, student.age);
}