LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-26-2013, 10:34 AM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
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.

Last edited by shifter; 05-26-2013 at 10:36 AM.
 
Old 05-26-2013, 11:22 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,214

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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");
 
Old 05-26-2013, 11:50 AM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
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?

Last edited by shifter; 05-26-2013 at 12:06 PM.
 
Old 05-26-2013, 12:29 PM   #4
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,214

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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);

Last edited by eSelix; 05-26-2013 at 12:30 PM.
 
Old 05-26-2013, 01:26 PM   #5
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Now it works. Why first it didn't work?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem With Opening WMV File hitmen Linux - Newbie 14 10-10-2012 12:59 PM
problem in opening .csv file shamjs Programming 3 11-03-2011 02:54 AM
Help: Opening GenICs binary file, writing to binary file for GADGET-2 (in C) parallax147 Programming 0 10-05-2010 11:06 AM
Problem for Opening any file with VIM ? raviteja_s Linux - Newbie 1 10-02-2010 03:35 AM
Axcrypt file encryption install error. Error opening file for writing ( axcrypt.css) Skyxxxsurfer General 1 04-26-2008 03:27 PM


All times are GMT -5. The time now is 08:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration