简体   繁体   中英

How to write a subprogram to read from a binary file and write in a text file?

#include<stdio.h>

typedef struct {
  char floare[40];
  char culoare[30];
  float pret;
  int codf;
}FLOARE;


void creare_fisier(char*nume) {
  FILE*g;
  FLOARE f;
  fopen_s(&g, nume, "wb");
  if (!g)
    printf("Eroare");
  else
  {
    printf("Cod floare:"); scanf_s("%d", &f.codf);
    while (!feof(stdin)) {
        getchar();
        printf("Nume floare:"); gets(f.floare);
        printf("Culoare floare:"); gets(f.culoare);
        printf("Pret:"); scanf_s("%f", &f.pret);
        fwrite(&f, sizeof(FLOARE), 1, g);
        printf("Cod floare:"); scanf_s("%d", &f.codf);
    }
    fclose(g);
  }
}

void afisare_fisier(char*nume) {
  FILE*h;
  FLOARE f;
  fopen_s(&h, "lista.dat", "rb");
  if (!h)
    printf("Eroare");
  else {
    FILE*g;
    fopen_s(&g, nume, "w");
    fread(&f, sizeof(FLOARE), 1, g);
    while (!feof(h)) {
        fprintf(g, "%d %s %s %f", f.codf, f.floare, f.culoare, f.pret);
        fread(&f, sizeof(FLOARE), 1, g);
    }
    fclose(g), fclose(h);
  }
}

void main() {
  char numef[] = "lista.dat";
  creare_fisier(numef);
  char numetxt[] = "raport.txt";
  afisare_fisier(numetxt);
}

The subprogram which creates the binary file is working, but the listing of the information into a text file is not working. When I run the code it's not happening anything and the space occupied by the text file is growing as I keep open the console. I don't really have experience with the subprograms, but I know the structure of listing into a text file.

This is for a homework task.

When I run the code it's not happening anything and the space occupied by the text file is growing as I keep open the console.

This is because you use feof and you mix gets and scanf

Note you also (try to) read in g rather than in h in afisare_fisier

Here a proposal :

I always use fgets to read on stdin , first because that allows to indicate the max size to read while gets can overflow the received buffer, and to not have to do a getchar() or equivalent in the hope to bypass a newline etc.

The newline are part of the read strings so in fields floare and culoare , probably you have to remove them.

In case floare and culoare are words and do not contain space(s) you can replace the fgets (+ sscanf ) by fscanf to not have the newlines in floare and culoare (do not forget to indicate the max size when you fscanf a string).

Note that using the result of fgets to detect EOF (rather than feof ) I do not have to put it two times in the source.

I added the check all time a file is open

I give the name of the two files in argument to afisare_fisier because there is no reason that one knows the name of the input file.

Because EOF is not easy from stdin I also stop to read when it is not possible to sscanf the Cod floare , that allows to terminate on an empty line (the user just hit <enter> without digit before)

typedef struct {
  char floare[40];
  char culoare[30];
  float pret;
  int codf;
}FLOARE;


void creare_fisier(char*nume) {
  FILE*g;
  FLOARE f;
  fopen_s(&g, nume, "wb");
  if (!g)
    printf("Eroare");
  else
  {
    char s[32];

    for (;;) {
      printf("Cod floare:");
      if ((fgets(s, sizeof(s), stdin) == NULL) ||
          (sscanf(s, "%d", &f.codf) != 1))
        break;
      printf("Nume floare:"); 
      if (fgets(f.floare, sizeof(f.floare), stdin) == NULL)
        break;
      printf("Culoare floare:"); 
      if (fgets(f.culoare, sizeof(f.culoare), stdin) == NULL)
        break;
      printf("Pret:");
      if ((fgets(s, sizeof(s), stdin) == NULL) ||
          (sscanf(s, "%f", &f.pret) != 1))
        break;
      fwrite(&f, sizeof(f), 1, g);
    }
    fclose(g);
  }
}

void afisare_fisier(char * in, char * out) {
  FILE*h;
  FLOARE f;
  fopen_s(&h, in, "rb");
  if (!h)
    printf("Eroare (in)");
  else {
    FILE*g;
    fopen_s(&g, out, "w");
    if (!g)
      printf("Eroare (out)");
    else {
      while (fread(&f, sizeof(f), 1, h) == 1) { /* read in h, not in g */
        fprintf(g, "%d %s %s %f\n", f.codf, f.floare, f.culoare, f.pret);
      }
      fclose(g);
    }
    fclose(h);
  }
}

int main() {
  char numef[] = "lista.dat";
  creare_fisier(numef);
  char numetxt[] = "raport.txt";
  afisare_fisier(numef, numetxt);
}

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra f.c
pi@raspberrypi:/tmp $ ./a.out
Cod floare:123
Nume floare:nume1
Culoare floare:culoare1
Pret:12.34
Cod floare:456
Nume floare:nume2
Culoare floare:culoare2
Pret:21.4
Cod floare:
pi@raspberrypi:/tmp $ cat raport.txt
123 nume1
 culoare1
 12.340000
456 nume2
 culoare2
 21.400000

Note : I hit <enter> when the third Cod floare was requested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM