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> Code:
#define MAX 20 Code:
#include <stdio.h> Where's the problem in your opinion? Thanks in advance for answers. |
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 |
Ok, now that problem is solved, but I've added another snippet of code.
Now my program is Code:
#include <stdio.h> Code:
void apri_file(FILE** f, const char* path, const char* mode) { An output example output of my program is Code:
saverio@samsung:~/Scrivania/Esercizio$ ./esercizio Why I insert McPippo, Pippo and 080513245 in elenco.txt and when I read by file the output is McPippo, Pippo and 0805132455080513245? |
You written to file one long concatenated string by:
Code:
fprintf(out, "%s%s%s\n", u1.cognome, u1.nome, u1.telefono); Code:
fprintf(out, "%s\t%s\t%s\n", u1.cognome, u1.nome, u1.telefono); |
Now it works. Why first it didn't work?
|
All times are GMT -5. The time now is 12:17 PM. |