Writing structs into a file C

David Tamrazov

I'm just starting to learn files. For this eI'm trying to create a program that keeps track of transaction records in a store. The first step is to record a day's transactions. I create a file trans.c that I can have open alongside my main.c file, so as to keep track of it. I then fill in trans.c first with empty structs to allocate memory for the transaction records, then I fill it in with structs containing client data using fwrite.

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

/*
 * 
 */
struct clientData {
    int num;
    char fName [20];
    char lName[20];
    long double balance;

};

void createFiles (FILE *fNew, FILE *fOld, FILE *fTrans, struct clientData x);
void fillTrans (FILE *fTrans, struct clientData x);

int main() {

    FILE *fTrans; //transaction file
    FILE *fNew;  //new master file
    FILE *fOld;  //old master file 

    struct clientData blankClient = {0, "", "", 0.0 };


    if ((fTrans = fopen("trans.c", "wb")) == NULL) {
        printf("Error. File could not be opened.\n");
    }

    else {
        int i;
        for (i = 1; i <= 100; i++) {
         fwrite(&blankClient, sizeof(struct clientData), 1, fTrans);  
        }
    }

    fillTrans(fTrans, blankClient);

}

void fillTrans(FILE *fTrans, struct clientData x) {

    if ((fTrans = fopen("trans.c", "a+")) == NULL) {
        printf("File could not be opened.");
    }

    else {
        printf("Enter the account info, or 0 to quit.\n");
        scanf("%d%s%s%Lf", &x.num, x.fName, x.lName, &x.balance);

        while (x.num!= 0) {

            fseek(fTrans, (x.num - 1) * sizeof(struct clientData), SEEK_SET);
            fwrite(&x, sizeof(struct clientData), 1, fTrans);
            fscanf(stdin, "%d%s%s%Lf", &x.num, x.fName, x.lName, &x.balance);

        }

        fclose(fTrans);
    }
}

When I go back to trans.c to see if it had worked, the file remains empty, and yet my code compiles without issue, and in run time I don't seem to have trouble filling in the structs and writing them in. Is the file being filled in elsewhere, or am I missing something/doing this wrong?

Dmitri

You're opening "trans.c" in main, then calling fillTrans() without closing it first -- so you have the file open twice while you write the data in fillTrans(). If the setup you're running this on allows both opens to succeed (on some, file locking would prevent it), then likely the last one to close determines the final contents of the file. Since main() doesn't close it (so its copy closes when the program ends), you end up with whatever main() had put in the file and discard what fillTrans() wrote through the second FILE *.

It may be possible to end up with some of the writes from fillTrans() in the file (if it weren't appending), but the file size would still be fixed by what main() had, and which parts of the file would have whose data is not easily predictable.

Also, if the writes from fillTrans() did remain, they would have been added to the end of the file, after anything main() wrote that had been flushed to disk already (if any), because fillTrans() opens in append mode.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Trying to read a text file into an array of structs in C++

分類Dev

c# windows app writing text to file

分類Dev

C# programming writing to a file user data

分類Dev

c++ multiple processes writing to the same file - Interprocess mutex?

分類Dev

Unexpected rounding of double types in C++ writing to file

分類Dev

C program over-writing file contents in if statement

分類Dev

Writing to a file in C++ space acts as a new line

分類Dev

C Programming - Structs, Pointers, and Initialization

分類Dev

Writing to a file in Apache Spark

分類Dev

Writing to a file in Angular 6

分類Dev

Qt: Writing umlaut to file

分類Dev

Writing an Observable to file

分類Dev

Writing to a file py

分類Dev

Data not writing to the file in iOS

分類Dev

Writing a File to the Specified Folder

分類Dev

writing string to file in java

分類Dev

Reading/Writing to binary file

分類Dev

Writing to the file in unix

分類Dev

Help with writing awk in a file

分類Dev

Replacing a substring from an .txt file and writing to a new .txt file stops after 1 line IN C

分類Dev

Reading binary file data into List of Structs

分類Dev

Writing C# in Debian

分類Dev

C# - Double spaces in DB field causing unwanted newlines when writing to a text file

分類Dev

Creating C structs using the RubyInline gem

分類Dev

C Programming: Structs and typedefs for Graph with adjacency list

分類Dev

Representing an AST in C with different structs for types of nodes

分類Dev

Scraping and writing output to text file

分類Dev

Writing post values to a text file

分類Dev

Writing objects to file while appending