fwrite() and fread()

This is a discussion on fwrite() and fread() within the C Programming forums, part of the General Programming Boards category; Once again, I'm struggling with writing into my binary file!! I've got one funtion to enter employee info... Thanks to ...

  1. #1
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    fwrite() and fread()

    Once again, I'm struggling with writing into my binary file!!
    I've got one funtion to enter employee info... Thanks to Prelude I actually got to this funtion, but then I stuck AGAIN!!
    Code:
    void newEmployee(EMP emp, FILE * empfPtr) {
      int i_temp;
      char s_temp[MAXLEN];
      float f_temp = 0;
    
      printf ("Enter new employee ID no.:");
      scanf("%d", &i_temp);
      printf("%d", i_temp);
      validateID(i_temp, emp, empfPtr);
      emp.ID = i_temp;
      fscanf(stdin, "%d", emp.ID);
      fseek(empfPtr, (emp.ID-1)*sizeof(EMP), SEEK_SET);
    
      puts("Enter employee last name:");
      scanf("%s", emp.last_name);
      upperCase(emp.last_name);
      fputs(emp.last_name, stdin);
    
      puts("Enter employee first name:");
      scanf("%s", emp.first_name);
      upperCase(emp.first_name);
      fputs(emp.first_name, stdin);
    
      puts("Enter employee department:");
      scanf("%s", s_temp);
      upperCase(s_temp);
      validateDept(s_temp);
      strcpy(emp.dept, s_temp);
      fputs(emp.dept, stdin);
    
      puts("Enter employee salary:");
      scanf("%f", &f_temp);
      validateSalary(f_temp);
      emp.salary = f_temp;
      fscanf(stdin, "%d", emp.salary);
    
      fwrite(&emp, sizeof(EMP), 1, empfPtr);
    
      puts("Next command (0 to print menu): ");
      return;
    }
    The printf() at line 7 is just a test, but this doesn't even work!!
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What doesn't work? Compiling errors? Logical errors?

    Are you checking if the file opens correctly?
    Are you aware that any changes you do to the Emp variable you pass to the function will only be local in this function?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    The program compiles fine
    what happens is, when I get to the newEmployee() funtion, it asks for the ID No., and after I enter it, it doesn't go to the next part (last name, first name, etc.)
    I enter a 1 (or whatever number) as an ID No. and it creates a newline, but nothing else
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  4. #4
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    You know.....
    I don't really know!!!
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    fscanf(stdin, "%d", emp.salary); -> scanf("%d", &emp.salary);

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: fwrite() and fread()

    Originally posted by Lau

    Code:
      ...
      fputs(emp.first_name, stdin);
      ...
      fputs(emp.dept, stdin);
      ...
    why are you attempting to write to a read-only stream?
    hello, internet!

  7. #7
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Once again, Monster and moi:
    I don't know what I was doing there!! the fscanf() and fputs() are gone...
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 02:45 PM
  2. Replies: 2
    Last Post: 12-26-2008, 03:38 PM
  3. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 02:07 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21