0

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

Edited by cyberguy007: n/a

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by WaltP
0

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

Edited by gerard4143: n/a

0

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

1

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)

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.