My input file contain:
1
2
3
4
5
6

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;
          }employee;
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);
         }
         do{
         printf ("FNAME: %s \n ",emp.fname);
         printf ("LNAME: %s  \n",emp.lname);
         printf ("Salary:  %d  \n",&emp.salary);
         count++;
         }while (count< max);
          getch ();
          
          return 0;}

Recommended Answers

All 8 Replies

My input file contain:
1
2
3
4
5
6

Are the file values integers or ascii text?

Are the file values integers or ascii text?

integers

stil its only readin the last three

integers

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);

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
where
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)
etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.