0

So I'm stuck in this piece of code that I'm writing and I want to save data that I receive from a binary file and save it in a array of structure I already tried something but I can't get any good results.

#define MAX_ACCOUNTS 5
#define COUNTER 0
typedef struct t_account {
    int id;
    char name[50];
    char provider[40];
    char org[40];
    char domain[50];
    float balance;
} t_account;


    t_account account[MAX_ACCOUNTS];
    char *item;
    FILE *file_accounts;
    char string[0];
    int i = COUNTER;

    file_accounts = fopen("../accounts.dat", "rb");

    if (file_accounts == NULL) {
        printf("Error, %s\n", strerror(errno));
        exit(1);
    } else {
        printf("Testing fread() function: \n\n");
        //fread(string, sizeof(string), MAX_ACCOUNTS, file_accounts );
        while(fgets(string, sizeof(struct t_account), file_accounts )){
            printf("%s\n", string);
            item = strtok(string, ";");

            printf("ID: %i\n", strtol(item,NULL,10));
            item = strtok(NULL, ";");

            printf("Name: %s\n", item);
            item = strtok(NULL, ";");

            printf("Provider: %s\n", item);
            item = strtok(NULL, ";");

            printf("ORG: %s\n", item);
            item = strtok(NULL, ";");

            printf("Domain: %s\n", item);
            item = strtok(NULL, ";");

            printf("Balance: %.2f Eur\n", strtof(item, NULL));
            i++;
        }
    }


    fclose(ficheiro_contas);

I already tried to save the data inside the while loop like this but for each field in the structure

account[i].id = atoi(item);

but the program crashes in the line that I do that piece of code to "save" the array.

10
  • strtok(string returns a pointer into string. It does not return a copy. Commented Jan 19, 2020 at 14:48
  • Yes I know, but in that case it returns the first string before the ";" so it I convert it to an integer. Commented Jan 19, 2020 at 14:50
  • char string[0]; is a string of zero length. I certainly is not sizeof(struct t_account). And it does not make sense to read sizeof(struct t_account) chars from the file. Commented Jan 19, 2020 at 14:50
  • so how would you do it ? Commented Jan 19, 2020 at 14:51
  • What about char string[512];? And then read the documentation of fgets Commented Jan 19, 2020 at 14:52

1 Answer 1

1

Your main problem is that your string is zero size, yet you try to read sizeof(struct t_account) bytes into it. That overwrites other data, causing undefined behavior and your crash.

Try:

   char string[512];
   int k= 0;
   while(fgets(string, sizeof(string), file_accounts )){
        item = strtok(string, ";");
        strcpy(account[k].name, item);
        //
        // etc...
        //
        k++;
   }
Sign up to request clarification or add additional context in comments.

6 Comments

Okay I understand it now, please get in the chat so we can talk better.
it is now working... finally, how can I add \n inside the .dat file after one string behind ";"?
like this: "1;Conta1_Amazon\n;AWS\n;IPLeiria\n;ipleiria.pt\n;1213.65;"
From gets documentation: "The line [gets reads] consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0')". But if there are no newline characters int the file, then that's a whole different problem.
it only saves in the first position of the array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.