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

时间:2015-08-07 04:53:49

标签: c struct 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] = '\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);
    }
}

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

21, Fred Nurk
92, Arwen Evensong

并按如下方式返回学生名单:

Fred Nurk (21)
Arwen Evensong (92)

但是我的代码返回:

 0 (21)
 0 (92)

我无法理解为什么。输入已设置且无法更改,必须对代码进行任何编辑。

2 个答案:

答案 0 :(得分:1)

在您的函数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);
}

答案 1 :(得分:0)

让我们检查以下代码段

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

  

年龄,[N] AME

因此,您的假设是不正确的。输入应该是颠倒的顺序

  

姓名,年龄

输出将是

  

年龄(姓名)

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

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

并在void printOneStudent(学生)中交换输出表格:

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