My input file contain:

why is it not reading properly, i tried implementing fget and fread but failed even after reading dani web and other tutorials.

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

typedef struct{
        char lname[20];
          char fname[20];
          int salary;
const int max = 2;          
 int main (){ 
          int count = 0;
          int j = 0;
          employee emp;
          FILE *input;

          input = fopen("input.txt","r");
        while (!feof(input) ){
         fscanf (input,"%s\n ",emp.fname);
         fscanf (input," %s  \n",emp.lname);
         fscanf (input," %d  \n",&emp.salary);
         printf ("FNAME: %s \n ",emp.fname);
         printf ("LNAME: %s  \n",emp.lname);
         printf ("Salary:  %d  \n",&emp.salary);
         }while (count< max);
          getch ();
          return 0;}

Edited 6 Years Ago by cyberguy007: n/a


so if I opened this file in a hex editor I would see something like below?

00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 04
00 00 00 05 00 00 00 06

Because if that's the case, then use

fread(&varname, sizeof(unsigned int), 1, input);

Edited 6 Years Ago by gerard4143: n/a

can u apply fread to my code because i'm confuse with fread even after reading tutorials

No, you cannot use fread . The proper answer to gerard4143's question was "text", althought the proper question should have been "Is the file a binary or text file?"

Without knowing what actually happened using fget (whatever that is) we can't really tell what you did wrong.

Maybe you should try to explain the entire problem you are having since we aren't at your computer watching over your shoulder.

Details == understanding.
Vague phrases == guessing (and usually wrong, too)

wel basically i tryin to read a text file in array
Record 1 "emp[1]"
fname =1st line in file
lname =2nd line in file
salary = 3rd line in file
Record 2 "emp[2]"
fname =4th line in file
lname =5th line in file
salary = 6th line in file

and so on
anyway i can do this?

Of course.

Create an array of your structure.
Open the file.
Read 3 lines into fname, lname, salary for person 1 (index 0)
Read the next 3 lines into fname, lname, salary for person 2 (index 1)

This article has been dead for over six months. Start a new discussion instead.