1

I have to write an app on C that opens a file, reads what is inside of it, stores that information on a struct and prints all the information. I did it creating "empleados.txt" and placing it on the folder where is main.c. This is the content of the .txt:

Juan Perez Rodriguez 11111111A 1200 Pepa Bueno Ruibal 22222222B 2300 

It is not printing anything, and I don't know why.

#include <stdio.h> #include <stdlib.h> struct Empleado { char nombre[15]; char apellido_uno[30]; char apellido_dos[30]; char DNI[9]; unsigned int sueldo; }; int main() { int i=0, j; struct Empleado num[50]; FILE *idf; idf=fopen("empleados.txt", "rt"); if (idf==NULL){ printf("Error abriendo fichero ..."); exit(1); } else { do{ fgets(idf,"%s",&num[i].nombre); fgets(idf,"%s",&num[i].apellido_uno); fgets(idf,"%s",&num[i].apellido_dos); fgets(idf,"%s",&num[i].DNI); fgets(idf,"%d",&num[i].sueldo); i++; }while(feof(idf)==0); } fclose (idf); for(j=0;j<i;j++){ printf("Nombre: %s",num[i].nombre); printf("Nombre: %s",num[i].apellido_uno); printf("Nombre: %s",num[i].apellido_dos); printf("Nombre: %s",num[i].DNI); printf("Nombre: %o",num[i].sueldo); } return 0; } 
28
  • filemode of fopen is rt ? Commented Nov 10, 2015 at 14:27
  • 2
    Your compiler should be throwing some errors. You are directly converting a pointer to the file into a pointer to a char. Commented Nov 10, 2015 at 14:28
  • 2
    fscanf instead of fgets, and char DNI[9]; --> char DNI[10]; Commented Nov 10, 2015 at 14:31
  • 1
    @PattySelman use j for index. also while(feof(idf)==0); is wrong. Commented Nov 10, 2015 at 14:36
  • 1
    @PattySelman: the longest string in your example data has 9 characters. C strings require a 0 terminator. So your buffer needs to be at least of size 9+1 = 10. Commented Nov 10, 2015 at 15:51

2 Answers 2

2

fgets wants the file as last argument and the target string as first.

The second argument should be the size of the target buffer, not a format string.

It seems you are confusing fgets and fscanf.

Here is an improved version (incorporating also the index fix from BLUEPIXY):

#include <stdio.h> #include <stdlib.h> struct Empleado { char nombre[15]; char apellido_uno[30]; char apellido_dos[30]; char DNI[9]; unsigned int sueldo; }; int main() { int i=0, j; struct Empleado num[50]; FILE *idf; idf=fopen("empleados.txt", "rt"); if (idf==NULL){ printf("Error abriendo fichero ..."); exit(1); } else { do{ fgets(&num[i].nombre, sizeof num[i].nombre, idf); fgets(&num[i].apellido_uno, sizeof num[i].apellido_uno, idf); fgets(&num[i].apellido_dos, sizeof num[i].apellido_dos, idf); fgets(&num[i].DNI, sizeof num[i].DNI, idf); fscanf(idf, "%u", &num[i].sueldo); i++; }while(feof(idf)==0); } fclose (idf); for(j=0;j<i;j++){ printf("Nombre: %s\n",num[j].nombre); printf("Nombre: %s\n",num[j].apellido_uno); printf("Nombre: %s\n",num[j].apellido_dos); printf("Nombre: %s\n",num[j].DNI); printf("Nombre: %o\n",num[j].sueldo); } return 0; } 
Sign up to request clarification or add additional context in comments.

5 Comments

I think those fgets calls are probably meant to be fscanf, given the parameters and the use of a format string.
@Paul R: Probably, yes. But in fact, fgets is the better choice here, isn't it?
@PaulR If I use fscanf I get this: Nombre: Nombre: Nombre: Nombre: Nombre: 0Nombre: Nombre: Nombre: Nombre: Nombre: 0
And if you use fgets it shouldn't even compile because you are giving it invalid arguments.
@BLUEPIXY that's the thing man. You are awesome. Didn't see that.
1

Change fgets to fscanf and remove & from the arguments of in do-while loop. num[i].nombre is converted to a pointer to char and similarly other members except the last member sueldo.

 fscanf(idf,"%s",num[i].nombre); fscanf(idf,"%s",num[i].apellido_uno); fscanf(idf,"%s",num[i].apellido_dos); fscanf(idf,"%s",num[i].DNI); fscanf(idf,"%d",&num[i].sueldo); i++; 

6 Comments

It's not even fscanf in the code, though it should be.
Also passing format strings to fgets is an interesting, but not altogether helpful idea.
@Kevin Still giving some errors: passing argument of 'fgets' from incompatible pointer type
I'm saying you should replace fgets with fscanf in your code, as you are using it as if it's fscanf.
@PattySelman: you have a lot of mistakes in your code - you'll need to fix all of them before you program works properly - fix all the mistakes that people have pointed out already and then go through your code carefully to see if there are any further problems.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.