C:简单的CSV读取器/写入器-无限循环行为

时间:2019-01-03 17:43:51

标签: c csv stdio string.h

我的程序可以编译,但是当我运行exe时,没有输出,程序也不会终止。

我试图通过打印“标志”来查找可能导致问题的代码行。但是,没有打印任何标志,即使是main中第一个语句的标志也没有打印。如果未调用populateLinkedList函数,程序将正常运行。

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

//Declare Node
struct TransactionNode{
    char id[11];
    char accountDebit[11];
    char accountCredit[11];
    char debit[11];
    char credit[11];
    char date[11];
    char message[101];

    struct TransactionNode *nextPtr; // pointer to next node
};

//Rename Node and Node Pointer
typedef struct TransactionNode Node; //ListNode is synonym for struct listNode
typedef Node *NodePtr; //ListNodePtr is a pointer to ListNode

//Declare starting transaction node
NodePtr tSPtr = NULL;

//prototypes
void writeLinkedList(NodePtr sPtr);
void populateLinkedList(void);

int main(){
    printf("hello");
    writeLinkedList(tSPtr);
    populateLinkedList(); 
    writeLinkedList(tSPtr);
    return 0;
}

void writeLinkedList(NodePtr sPtr){
    FILE *fPtr;
    NodePtr currentPtr;
    currentPtr = sPtr; //assign currentPtr to startingPtr
    fPtr = fopen("records.dat", "w");

    while(currentPtr != NULL){//while currentPtr exists
        //write structure
        fprintf(fPtr, "%s;%s;%s;%s;%s;%s;%s;", currentPtr -> id, currentPtr -> accountDebit, 
        currentPtr -> accountCredit, currentPtr -> debit, currentPtr -> credit, currentPtr -> date, currentPtr ->message);
        //walk... 
        currentPtr = currentPtr -> nextPtr;
    }//end while

    fclose(fPtr);
}

void populateLinkedList(void){
    FILE *fPtr;
    NodePtr currentPtr;
    NodePtr previousPtr;
    char temp[101];
    //check to see if file can be opened
    if (( fPtr = fopen("records.dat", "r")) == NULL){
        printf("populateLinkedList error: Cannot open file"); //error
        return;
    } //end if

    //create first node
    currentPtr = malloc(sizeof(Node));
    if(currentPtr == NULL){
        fclose(fPtr);
        printf("populateLinkedList error: Not enough memory");
        return;
    }//end if
    else tSPtr = currentPtr; //end else: assign first node to sPtr

    //FOR FIRST TRANSACTION:
    //flush and scan and assign id
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    printf("%s", temp);
    if(temp[0] == EOF){
        fflush(stdin);
        fclose(fPtr);
        return;
    }; //end if: if EOF is found break function
    strcpy(currentPtr -> id, temp);
    //flush and scan and assign debit account id
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> accountDebit, temp);
    //flush and scan and assign credit account id
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> accountCredit, temp);
    //flush and scan and assign debit amount
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> debit, temp);
    //flush and scan and assign credit amount
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> credit, temp);
    //flush and scan and assign date
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> date, temp);
    //flush and scan and assign message
    fflush(stdin);
    fscanf(fPtr, "%100[^;]", temp);
    strcpy(currentPtr -> credit, temp);
    //assign nextPtr
    currentPtr -> nextPtr = NULL;

    //assign currentPtr to previousPtr
    previousPtr = currentPtr;

    while(temp[0]!= EOF){

        //create first node
        currentPtr = malloc(sizeof(Node));
        if(currentPtr == NULL) break;

        //flush and scan and assign id
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        if(temp[0] == EOF) break; //end if: if EOF is found break function
        strcpy(currentPtr -> id, temp);
        //flush and scan and assign debit account id
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> accountDebit, temp);
        //flush and scan and assign credit account id
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> accountCredit, temp);
        //flush and scan and assign debit amount
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> debit, temp);
        //flush and scan and assign credit amount
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> credit, temp);
        //flush and scan and assign date
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> date, temp);
        //flush and scan and assign message
        fflush(stdin);
        fscanf(fPtr, "%100[^;]", temp);
        strcpy(currentPtr -> credit, temp);
        //assign nextPtr
        currentPtr -> nextPtr = NULL;

        //assign currentPtr to previousPtr
        previousPtr = currentPtr;

        //assign currentPtr to previousPtr -> nextPtr
        previousPtr -> nextPtr = currentPtr;
    }//end while

    fflush(stdin);
    fclose(fPtr);
    return;
}



I expect the file to remain the same, the flags be printed, and the program to terminate; however, the flags are not printed and the program does not terminate.

1 个答案:

答案 0 :(得分:0)

populateLinkedList 中的

您在内存中创建一个圆圈:

previousPtr = currentPtr;

//assign currentPtr to previousPtr -> nextPtr
previousPtr -> nextPtr = currentPtr;

currentPtr-> nextPtr == currentPtr之后,如果您尝试遍历列表,则程序将无限期循环,则圆圈无止境

必须交换线路:

//assign currentPtr to previousPtr -> nextPtr
previousPtr -> nextPtr = currentPtr;

previousPtr = currentPtr;