LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with opening/writing file in C (https://www.linuxquestions.org/questions/programming-9/problem-with-opening-writing-file-in-c-4175463499/)

shifter 05-26-2013 10:34 AM

Problem with opening/writing file in C
 
Hi all.
I've a problem with a simple C program that opens a file and write within it.
The code of main.c is the following:

Code:

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

#include "esercizio.h"

int main() {

  FILE* out;
  utente u1, u2;
  int risposta = 1;

  apri_file(out, "elenco.txt", "w");
  while (risposta) {
       
    printf("Inserisci il cognome: ");
    scanf("%s", u1.cognome);
    printf("Inserisci il nome: ");
    scanf("%s", u1.nome);
    printf("Inserisci il numero di telefono: ");
    scanf("%s", u1.telefono);
     
    fprintf(out, "%s%s%s\n", u1.cognome, u1.nome, u1.telefono);
    printf("Vuoi continuare, se si inserisci 1, altrimenti 0: ");
    scanf("%d", &risposta);
  }
  fclose(out);
  return 0;
}

whereas the esercizio.h file is:
Code:

#define MAX 20

typedef struct {
  char cognome[MAX];
  char nome[MAX];
  char telefono[MAX];
} utente;

void apri_file(FILE*, char*, char*);

and esercizio.c file is
Code:

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


void apri_file(FILE* f, const char* path, const char* mode) {
 
  f = fopen(path, mode);
  if (f == NULL) {
    printf("errore nell'apertura del file\n");
    exit(1);
  }

}

The program crashes after reading from standard input but it doesn't write within elenco.txt file!
Where's the problem in your opinion?

Thanks in advance for answers.

eSelix 05-26-2013 11:22 AM

In function apri_file() when you pass "out" variable (which is a pointer) it is passed by copy. You assigned to this copy a value (by "f" variable), but it will not affect "out" variable. You should pass back "f" as function return, or get it by reference or pointer (to FILE pointer). For example:
Code:

// definition
void apri_file(FILE** f, const char* path, const char* mode)
  {
  *f = fopen(path, mode);
  // rest of function
  }

// and calling by
apri_file(&out, "elenco.txt", "w");


shifter 05-26-2013 11:50 AM

Ok, now that problem is solved, but I've added another snippet of code.
Now my program is
Code:

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

#include "esercizio.h"

int main() {

  FILE* out;
  utente u1, u2;
  int risposta = 1;

  apri_file(&out, "elenco.txt", "w");

  while (risposta) {
       
    printf("Inserisci il cognome: ");
    scanf("%s", u1.cognome);
    printf("Inserisci il nome: ");
    scanf("%s", u1.nome);
    printf("Inserisci il numero di telefono: ");
    scanf("%s", u1.telefono);
     
    fprintf(out, "%s%s%s\n", u1.cognome, u1.nome, u1.telefono);
    printf("Vuoi continuare, se si inserisci 1, altrimenti 0: ");
    scanf("%d", &risposta);
  }
  fclose(out);

  apri_file(&out, "elenco.txt", "r");

  while (!feof(out)) {
    fscanf(out, "%s%s%s\n",u1.cognome, u1.nome, u1.telefono);
     
    printf("%s%s%s\n", u1.cognome, u1.nome, u1.telefono);
  }
  fclose(out);

  return 0;
}

with apri_file

Code:

void apri_file(FILE** f, const char* path, const char* mode) {
 
  (*f) = fopen(path, mode);
  if (f == NULL) {
    printf("errore nell'apertura del file\n");
    exit(1);
  }

}

but the output is not the same that records I write in elenco.txt file.
An output example output of my program is

Code:

saverio@samsung:~/Scrivania/Esercizio$ ./esercizio
Inserisci il cognome: McPippo 
Inserisci il nome: Pippo
Inserisci il numero di telefono: 080513245
Vuoi continuare, se si inserisci 1, altrimenti 0: 0
McPippoPippo0805132455080513245

What's the problem?
Why I insert McPippo, Pippo and 080513245 in elenco.txt and when I read by file the output is McPippo, Pippo and 0805132455080513245?

eSelix 05-26-2013 12:29 PM

You written to file one long concatenated string by:
Code:

fprintf(out, "%s%s%s\n", u1.cognome, u1.nome, u1.telefono);
Look into file to see what was written. You need to separate it by character which scanf recognize as delimiter. For example:
Code:

fprintf(out, "%s\t%s\t%s\n", u1.cognome, u1.nome, u1.telefono);

shifter 05-26-2013 01:26 PM

Now it works. Why first it didn't work?


All times are GMT -5. The time now is 12:17 PM.