Hi,

I have looked on boards, googled, I tried you tube had a look at the C books I have on hand.. but I am confused about this...

What is the best way to write a struct to a file_ And how is the data saved and then read back? Can someone help me out with an explanation? If you know a good page on beginner file reading/writing in C, I would love to know about it...

Thanks,
Nate

One function to write the struct to a file.
Another function to read the struct from a file.

For each field, use the appropriate fprintf or fscanf conversion function.

For extra robustness on reading, use fgets() to begin with.

So with my struct -

typedef struct
{
char firstName[15];
char surname[15];
int age;
float height;
float weight;
float bmi;
float bfpercentage;
char significantInjury;
char unableToExercise;
int seekMedicalApproval;
int hadDrApproval;
int dateOfApproval;

}clientData;

But how is the information saved and read? I can only find information related to strings.. but how do i save the ints etc? and then get them back? Do I need some type of format specifier?

Thanks again.

One way:

/** Comma separated, for example. */
void record_write(FILE *file, const clientData *cd)
{
   fprintf(file, "%s,", cd->firstName);
   fprintf(file, "%s,", cd->surname);
   fprintf(file, "%d,", cd->age);
   fprintf(file, "%f,", cd->height);
   fprintf(file, "%f,", cd->weight);
   fprintf(file, "%f,", cd->bmi);
   fprintf(file, "%f,", cd->bfpercentage);
   fprintf(file, "%c,", cd->significantInjury);
   fprintf(file, "%c,", cd->unableToExercise);
   fprintf(file, "%d,", cd->seekMedicalApproval);
   fprintf(file, "%d,", cd->hadDrApproval);
   fprintf(file, "%d,", cd->dateOfApproval);
   fprintf(file, "\n");
}

One way:

/** Comma separated, for example. */
void record_write(FILE *file, const clientData *cd)
{
   fprintf(file, "%s,", cd->firstName);
   fprintf(file, "%s,", cd->surname);
   fprintf(file, "%d,", cd->age);
   fprintf(file, "%f,", cd->height);
   fprintf(file, "%f,", cd->weight);
   fprintf(file, "%f,", cd->bmi);
   fprintf(file, "%f,", cd->bfpercentage);
   fprintf(file, "%c,", cd->significantInjury);
   fprintf(file, "%c,", cd->unableToExercise);
   fprintf(file, "%d,", cd->seekMedicalApproval);
   fprintf(file, "%d,", cd->hadDrApproval);
   fprintf(file, "%d,", cd->dateOfApproval);
   fprintf(file, "\n");
}

Hey thanks a lot.. I will give it a try tomorrow and see if i can get it to work correctly...

Thanka again guys,
Nate

I am not sure what I am doing wrong (do i need to create the file first?? If so where do I import it into in Netbeans?)... currently I am unable to get the method to save.

int main(int argc, char** argv)
{
    const clientData *cd;
    clientData client;
    FILE *client_file;

    programIntroduction();
    profileWizard(&client);
    healthScreening(&client);
    saveToFile(*client_file, *cd);

    return (EXIT_SUCCESS);
}

saveToFile(FILE *write_client_data, const clientData *cd)
{
    write_client_data = fopen( "mt_client_file.txt", "w" );
    if( write_client_data != NULL )
    {
        fprintf(write_client_data, "%s,", cd->firstName);
        fprintf(write_client_data, "%s,", cd->surname);
        fprintf(write_client_data, "%d,", cd->age);
        fprintf(write_client_data, "%f,", cd->height);
        fprintf(write_client_data, "%f,", cd->weight);
        fprintf(write_client_data, "%f,", cd->bmi);
        fprintf(write_client_data, "%f,", cd->bfpercentage);
        fprintf(write_client_data, "%c,", cd->significantInjury);
        fprintf(write_client_data, "%c,", cd->unableToExercise);
        fprintf(write_client_data, "%d,", cd->seekMedicalApproval);
        fprintf(write_client_data, "%d,", cd->hadDrApproval);
        fprintf(write_client_data, "%d,", cd->dateOfApproval);
        fprintf(write_client_data, "\n");
    }
}

Here is the error message - /Applications/NetBeans/NetBeans 6.7.1.app/Contents/Resources/NetBeans/dlight1/bin/nativeexecution/dorun.sh: line 51: 18665 Bus error /bin/sh "${PIDFILE}.sh"

ops and yes there is a void on the front of the saveToFile Method... sorry i missed it out when cutting and pasting..

> saveToFile(*client_file, *cd);
Neither client_file or cd are initialised.

Something like this

client_file = fopen("data.txt","w");
if ( client_file != NULL ) {
    saveToFile(*client_file, &client);
    fclose( client_file );
}

Oh, and make sure you prototype your functions before calling them.
Had you done so, this would have generated warnings.

Actually, I'm surprised it didn't anyway.

if you do not have problem with binary file then you can try saving and reading struct using fwrite and fread function

// Saving structure in file
//fwrite(structure variable address,size of structure,number of records,File pointer);


fwrite(&clientData_var,sizeof(clientData),1,FILE_POINTER);

same signature for fread...

I am closer but i still can't get it to work.. I am new to pointers and I am finding them very confusing!!

Here is the code -

typedef struct
{
    char  firstName[15];
    char  surname[15];
    int   age;
    float height;
    float weight;
    float bmi;
    float bfpercentage;
    char  significantInjury;
    char  unableToExercise;
    int   seekMedicalApproval;
    int   hadDrApproval;
    int   dateOfApproval;
    
}clientData;

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

void saveToFile(FILE *write_client_data, const clientData *cd);

int main(int argc, char** argv)
{
    const clientData *cd;
    clientData client;

    FILE *client_file = fopen( "mt_client_file.txt", "w" );

    programIntroduction();
    profileWizard(&client);
    healthScreening(&client);
    saveToFile(*client_file, &client);
    fclose(client_file);

    return (EXIT_SUCCESS);
}

void saveToFile(FILE *write_client_data, const clientData *cd)
{
    printf("Into file nethod...\n");
    //write_client_data = fopen( "mt_client_file.txt", "w" );
    if( write_client_data != NULL )
    {
        fprintf(write_client_data, "%s,", cd->firstName);
        fprintf(write_client_data, "%s,", cd->surname);
        fprintf(write_client_data, "%d,", cd->age);
        fprintf(write_client_data, "%f,", cd->height);
        fprintf(write_client_data, "%f,", cd->weight);
        fprintf(write_client_data, "%f,", cd->bmi);
        fprintf(write_client_data, "%f,", cd->bfpercentage);
        fprintf(write_client_data, "%c,", cd->significantInjury);
        fprintf(write_client_data, "%c,", cd->unableToExercise);
        fprintf(write_client_data, "%d,", cd->seekMedicalApproval);
        fprintf(write_client_data, "%d,", cd->hadDrApproval);
        fprintf(write_client_data, "%d,", cd->dateOfApproval);
        fprintf(write_client_data, "\n");
    }
    else
    {
        printf("Error reading file...");
    }
}

here is the error message from the function call in the main()-

ICT106_A1.c:45: error: incompatible type for argument 1 of ‘saveToFile’

Change this line:

saveToFile(*client_file, &client);

to this:

saveToFile(client_file, &client);

In addition to this, you should also check that the call to fopen() is successful - Salem gave you the code for this check in post #8.
Good luck!

awesome that is working now...

Do read it in do I just reverse all the statements and user fscanf?

Since the whole line will be a string, you won't want to use %s for the strings with fscanf ; instead you'd probably want %14[^,] . The arguments to fscanf will be pointers, so watch out for that too.

Do read it in do I just reverse all the statements and user fscanf?

And I'm not quite sure what you mean by "reverse", so I'll say 'no'. The fscanf code will look a lot like the fprintf code -- with the exceptions of the strings and the pointer arguments.

You called from main() like
saveToFile(*client_file, &client); //at 33 line
but you should call this function as
saveToFile(client_file, &client);

this may be solve your problem..........

You called from main() like
saveToFile(*client_file, &client); //at 33 line
but you should call this function as
saveToFile(client_file, &client);

this may be solve your problem..........

It already did solve the problem sunshine. Refer to post #11!

It already did solve the problem sunshine. Refer to post #11!

But I need to check in case he is right... as I ran the program in Netbeans and it compiled and saved to file.. but there is the chance that if i ran it in with a a lot of extra checking like -Wall -Werrer that it would produce warnings... so I will do that and get back to the thread,.. I am just working on some HTML right now.

Thanks again guys..
Nate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.