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

使用struct和stdin的C中的输出问题,c,struct,stdin,C,Struct,Stdin,我目前正在做一项计算机科学作业,到了一个我无法理解的地步。我已经被提供了代码,我要编辑这些代码来做某些事情,我到目前为止一直能够做,但我似乎无法得到下一位。代码从标准输入中读取许多学生的姓名和年龄,直到被EOF(控制D或类似)终止,从而构建一个学生的链接列表。从每个输入行读取一个学生。 到目前为止,我的代码是: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cty

我目前正在做一项计算机科学作业,到了一个我无法理解的地步。我已经被提供了代码,我要编辑这些代码来做某些事情,我到目前为止一直能够做,但我似乎无法得到下一位。代码从标准输入中读取许多学生的姓名和年龄,直到被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] = '\0';  // 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 = '\0';  // 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);
    }
}
并返回以下学生名单:

Fred Nurk (21)
Arwen Evensong (92)
但是,我的代码返回:

 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 = '\0';  // null-terminate the name
            student = newStudent(buffer, age);
        }
    }
    return student;
}
根据您的说法,输入应该是:

年龄、姓名

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

char* commaPos = strchr(buffer, ',');
通过以下行提取年龄:

int age = atoi(commaPos + 1);
所以它取逗号分隔符的地址,并递增1

年龄,年龄

因此,你的假设是错误的。输入的顺序应相反

姓名、年龄

输出将是

年龄(姓名)

编辑: 要交换输入表单,只需在Student*readOneStudent(FILE*FILE)中交换到以下内容:

要在void printOneStudent(Student-Student)中交换输出表单,请执行以下操作:


在函数
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] = '\0';
            *commaPos = '\0';  // 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);
}

您的代码假定为
Fred Nurk,21
您在
readOneStudent()
中的代码假定年龄跟在逗号后面,名字跟在逗号前面。这与您所说的您实际使用的输入相矛盾。使输入和代码匹配,您就有机会实现所有功能。与其他网站不同,您可以访问OP和其作者,或者访问与问题无关的内容,如。
printf(“%d(%s)\n”,student.age,student.name)-->
printf(“%s(%d)\n”,student.name,student.age)也许通过调试器运行此操作将帮助您永无止境。我可以在newStudent()中看到一个错误,这不是您的主要问题,-但是我觉得您至少应该试一试。通过调试器跟踪它,这将帮助您了解发生了什么以及出现了什么问题。问题的关键是我们得到了输入,必须重写代码以符合输入。抱歉,我应该在示例中更清楚地说明这一点:)基本上,您希望将输入从“name,age”更改为“age,name”。以及从“年龄(姓名)”到“姓名(年龄)”的输出?您是否尝试执行更改?我的意思是在把这个问题发布到SO之前。因为在你的帖子里我看不到PHi再次感谢您的帮助,它几乎完美地工作:)但是我想知道您是否知道如何更改它使用的输出(\n指示新行出现的位置):
name 1\n
(age 1)name 2\n
(age 2)
并将其以以下格式返回:
name 1(age 1)\n
name 2(age 2)
您将获得这种类型的输出,因为名称末尾有
'\n'
。用
'\0'
字符覆盖它。我已相应地更新了我的答案。
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] = '\0';
            *commaPos = '\0';  // 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);
}