Scanset 转换未产生正确的结果?

问题描述 投票:0回答:1

我正在开发一个程序,需要从文件中读取数据,数据以逗号分隔。例如:姓名,姓名。我正在调试,无法解决。下面的代码正是我现在用来测试的代码。

fscanf(input, "%50[a-zA-Z], %50[a-zA-Z], %d, %d, %d, %10[a-zA-Z], %15[a-zA-Z]", newNode->lastName, newNode->firstName, &newNode->birthDay.month, &newNode->birthDay.day, &newNode->birthDay.year, newNode->major, newNode->classStand);

让 newNode 完全为空,而

fscanf(input, "%50[a-zA-Z], %50[a-zA-Z], %d, %d, %d, %10[a-zA-Z], %15[a-zA-Z]", newNode->last, newNode->first, &newNode->birthDay.month, &newNode->birthDay.day, &newNode->birthDay.year, newNode->major, newNode->class);

在我的测试文件上工作得很好。

我什至用与我的实际项目非常相似的代码制作了第二个文件,并且它工作得很好。另外,在损坏的文件上,如果我只是使用“%s”而不是尝试扫描集转换,它会按预期记录整行。 输入示例:

Doe,John,1,23,2000,CS-BS,初级

酒吧,Foo,4,56,2000,CS-BS,初级

这是所有文件。请原谅我的混乱,我还没有开始整理部分

函数.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct bDayType {
    int month;
    int day;
    int year;
} bDay;

typedef struct linkList {
    char lastName[50];
    char firstName[50];
    char major[10];
    char classStand[15];

    bDay birthDay;

    struct linkList *next;
} node_t;


/*Parameters: input - input file containing all student info 
 *            head - the head of the linked list
 *Return: node_t* - a pointer to the list
 *This function will recieve info from the input file and fill out 
the linked list using
 *the "add" function
 */
node_t* createList(FILE* input, node_t** head);

/*Parameters: node - node to be added to the list 
 *                head - the head of the linked list
 *Adds a node to the end of the linked list
 *
 */
void add(node_t** node, node_t** head);

/*Parameters: input - the input file
 *Return: node_t* - a pointer to the newly populated node
 *Reads data from input and fills out nodes with the data.
 *
 */
node_t* readNodeInfo(FILE* input);

/*Parameters: output - the file being written to
 *                head - the head of the linked list
 *Prints the data to the output file, then sends a messgage to the 
terminal when done 
 *
 */
void printList(FILE* output, node_t* head);

/*Parameters: output - the file being written to
 *
 *Prints 80 asterisks to the output file to seperate data entries
 *
 */
void printBorder(FILE* output);

/*Parameters: head - the head of the linked list
 *
 *Deletes all nodes from the list to free memory
 *
 */
void deleteList(node_t** head);

#endif  

函数.c

    #include "functions.h"

    node_t* createList(FILE* input, node_t** head) {
        while (!feof(input)) {
            node_t* newNode = readNodeInfo(input);
            if (newNode != NULL) {
                add(&newNode, head);
            }
        }

        return *head;
    }

    void add(node_t** node, node_t** head){
        if (*head == NULL) {
            *head = *node;
            (*node)->next = NULL;
            return;
        }

        node_t* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }

        temp->next = *node;
        (*node)->next = NULL;
    }

    node_t* readNodeInfo(FILE* input){
        node_t* newNode = (node_t*)malloc(sizeof(node_t));
            if (newNode == NULL) {
                fprintf(stderr, "Error: Memory allocation failed\n");
                exit(EXIT_FAILURE);
        }

        fscanf(input, "%50[a-zA-Z], %50[a-zA-Z], %d, %d, %d, %10[a-zA- Z], %15[a-zA-Z]",
            newNode->lastName, newNode->firstName, &newNode>birthDay.month, &newNode->birthDay.day, 
            &newNode->birthDay.year, newNode->major, newNode->classStand);

        newNode->next = NULL;
        return newNode;
    }

    void printList(FILE* output, node_t* head){
            if (head == NULL) {
            fprintf(stderr, "List is empty\n");
            return;
        }
        printBorder(output);

        fprintf(output, "LIST INFO:\n");
            node_t* current = head;
        while (current != NULL) {
            fprintf(output, "Name:\t%s %s\n", current->firstName, 
    current->lastName);
            fprintf(output, "Date of Birth:\t%d %d, %d\n", current- >birthDay.month, current->birthDay.day, current->birthDay.year);
            fprintf(output, "Major:\t%s\n", current->major);
            fprintf(output, "Year:\t%s\n\n", current->classStand);
    
            printBorder(output);

            current = current->next;
        }
    }

    void printBorder(FILE* output){
        for(int i = 0; i < 80; i++){
            fprintf(output, "*");
        }
        fprintf(output, "\n");
    }

    void deleteList(node_t** head){
        if (*head == NULL) {
            return;
        }

        node_t* current = *head;
        node_t* next;

        while (current != NULL) {
            next = current-> next;
            free(current);
            current = next;
        }

        *head = NULL;
    }

这是司机。c

#include "functions.h"

int main(int argc, char* argv[]) {
    assert(argc == 3 && "Incorrect number of command line 
arguments");

    FILE* input = fopen(argv[1], "r");
    assert(input != NULL && "Error opening input file");

    FILE* output = fopen(argv[2], "w");
    assert(output != NULL && "Error opening output file");

    node_t* head = NULL;
    head = createList(input, &head);

    printList(output, head);

    deleteList(&head);

    fclose(input);
    fclose(output);

    return 0;
}

这是“完全”运行的测试文件:

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

    typedef struct bDay {
        int day;
        int month;
        int year;
    }date;

    typedef struct LinkList {
        date birthDay;

        char last[50];
        char first[50];
        char major[10];
        char class[15];

        struct Node *next;
    }Node;

    Node* createNode(FILE* input, FILE* output){
        Node* newNode = (Node*)malloc(sizeof(Node));
        
        fscanf(input, "%50[a-zA-Z], %50[a-zA-Z], %d, %d, %d, %10[a-zA-Z], %15[a-zA-Z]", 
        newNode->last, newNode->first, &newNode->birthDay.month, &newNode->birthDay.day, &newNode->birthDay.year, 
        newNode->major, newNode->class);
        
        newNode->next = NULL;

        fprintf(output, "%s, %s, %d/%d/%d, %s, %s\n", newNode->first, newNode->last, newNode->birthDay.month, 
        newNode->birthDay.day, newNode->birthDay.year, newNode->major, newNode->class);

        return newNode;
    }

    int main(int argc, char *argv[]) {
        FILE* fp = fopen(argv[1], "r");
        FILE* fp2 = fopen(argv[2], "w");

    Node* newNode = createNode(fp, fp2);
    
    

      // fclose(fp);

    return 0;
}
c gcc vim
1个回答
0
投票

您的代码中似乎存在一个小问题,可能导致它无法按预期工作。问题在于如何访问 node_t 结构中的birthDay结构的成员。

在readNodeInfo函数中,应该使用箭头运算符(->)来访问birthDay结构体的月份成员,但它被写成了(>):

fscanf(input, "%50[a-zA-Z], %50[a-zA-Z], %d, %d, %d, %10[a-zA- Z], %15[a-zA-Z]",
            newNode->lastName, newNode->firstName, &newNode>birthDay.month, &newNode->birthDay.day, 
            &newNode->birthDay.year, newNode->major, newNode->classStand);

修改如下:

fscanf(input, "%50[a-zA-Z], %50[a-zA-Z], %d, %d, %d, %10[a-zA- Z], %15[a-zA-Z]",
            newNode->lastName, newNode->firstName, &newNode->birthDay.month, &newNode->birthDay.day, 
            &newNode->birthDay.year, newNode->major, newNode->classStand);
© www.soinside.com 2019 - 2024. All rights reserved.