C: Простая программа чтения / записи CSV - поведение бесконечного цикла - PullRequest
0 голосов
/ 03 января 2019

Моя программа скомпилируется, но когда я запускаю exe, выходных данных нет, и программа не завершается.

Я попытался найти строку кода, которая может вызывать проблему, напечатав «flags»;однако ни один из моих флагов не напечатан, даже флаг, который является первым оператором в 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 голосов
/ 03 января 2019

в populateLinkedList вы создаете круг в памяти:

previousPtr = currentPtr;

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

после этого currentPtr-> nextPtr == currentPtr, если вы пытаетесь пройти по списку, ваша программа зацикливается бесконечно, у круга нетend

Необходимо заменить строки:

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

previousPtr = currentPtr;
...