MED LDN
MED LDN

Reputation: 669

Fill a file with an array of struct type in C

I need to feel a file with a list of purchase I declared a type struct name purchase that must contain the information : item, price and date of purchase in day/month/year ordered by date of purchase. But it doesn't work and I don't know why.

This is the code source:

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

typedef struct Date {
  int day,month,year;
} Date;

typedef struct purchase{
  char item[20];
  float price;
  Date date_purchase;
} purchase;


void fillFile(char Name_F[30] , purchase T[30] , int N) {

  FILE * P_Fichier;
  P_Fichier=fopen(Name_F,"w");
  int i=1;
  while (i< N )
    {
      fprintf(P_Fichier,T[i].item);
      fprintf(P_Fichier,T[i].price);
      fprintf(P_Fichier,T[i].date_purchase);
      i++;
    }
}

Upvotes: 0

Views: 364

Answers (2)

gsamaras
gsamaras

Reputation: 73444

You need to specify the format specifier in fprintf(), like this:

fprintf(P_Fichier, "%s\n", T[i].item);
fprintf(P_Fichier, "%f\n", T[i].price);
fprintf(P_Fichier, "%d/%d/%d\n", T[i].date_purchase.day, T[i].date_purchase.month, T[i].date_purchase.year);

These are the typical format specifiers that'd use with printf() too, so %s for string, %f for float and %d for int.

I'd suggest you to check if the file is opened before attempting to write into it (file pointer is going to be NULL if it didn't open). You also need to close the file when you are done writing your data, like fclose(P_Fichier);.


Moreover, i should start from 0, otherwise you'll skip your first element. However, since you already know how many times you want the loop to be executed, I'd advise you to use a for-loop, like this:

for (int i = 0; i < N; ++i)

Putting everything together, you should get:

void fillFile(char Name_F[30], purchase T[30], int N) {
  FILE* P_Fichier;
  P_Fichier = fopen(Name_F, "w");
  if (P_Fichier == NULL) {
    # file failed to open
    fprintf(stderr, "Unable to open %s file.\n", Name_F);
    return;
  }
  for (int i = 0; i < N; ++i) {
    fprintf(P_Fichier, "%s\n", T[i].item);
    fprintf(P_Fichier, "%f\n", T[i].price);
    fprintf(P_Fichier, "%d/%d/%d\n", T[i].date_purchase.day, T[i].date_purchase.month, T[i].date_purchase.year); 
  }
  fclose(P_Fichier);
}

Upvotes: 4

MikeCAT
MikeCAT

Reputation: 75062

You have to use format specifiers to use fprintf().

Also results of fopen() should be checked and files opened should be closed.

void fillFile(char Name_F[30] , purchase T[30] , int N) {

  FILE * P_Fichier;
  P_Fichier=fopen(Name_F,"w");
  if (P_Fichier == NULL) return; /* check if file open is successful */
  int i=1;
  while (i< N )
    {
      fprintf(P_Fichier,"%s\n",T[i].item); /* %s for strings */
      fprintf(P_Fichier,"%f\n",T[i].price); /* %f for float */
      fprintf(P_Fichier,"day:%d, month:%d, year:%d\n", /* %d for int */
        T[i].date_purchase.day, T[i].date_purchase.month, T[i].date_purchase.year);
      i++;
    }
  fclose(P_Fichier); /* close file when done */
}

Upvotes: 3

Related Questions