File I/O, fgets()? scanf()? I'm lost...

This is a discussion on File I/O, fgets()? scanf()? I'm lost... within the C Programming forums, part of the General Programming Boards category; Problem: go though a file and find the corresponding last name, with corresponding info and DISPLAY!! I'm going crazy with ...

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

    File I/O, fgets()? scanf()? I'm lost...

    Problem: go though a file and find the corresponding last name, with corresponding info and DISPLAY!!

    I'm going crazy with figuring out that program.
    The process is more than simple: get the user to put in employee info (ID, last name, first name, dept, and salary), and store those in a file.
    Here's what I got:
    Code:
    void newEmployee(EMP *emp, FILE *empfPtr) {
      static int i=0;
      int i_temp=0;
      char s_temp[MAXLEN];
      float f_temp=0;
      rewind(empfPtr);
    
      printf ("Enter new employee ID no.:");
      scanf("%d", &i_temp);
      validateID(i_temp, emp);
      emp[i].ID = i_temp;
      fprintf(empfPtr, "%d", emp[i].ID);
    
      puts("Enter employee last name:");
      scanf("%s", emp[i].last_name);
      upperCase(emp[i].last_name);
      fprintf(empfPtr, "%d", emp[i].last_name);
    
      puts("Enter employee first name:");
      scanf("%s", emp[i].first_name);
      upperCase(emp[i].first_name);
      fprintf(empfPtr, "%d", *emp[i].first_name);
    
      puts("Enter employee department:");
      scanf("%s", s_temp);
      upperCase(s_temp);
      validateDept(s_temp);
      strcpy(emp[i].dept, s_temp);
      fprintf(empfPtr, "%s", emp[i].dept);
    
      puts("Enter employee salary:");
      scanf("%f", &f_temp);
      validateSalary(f_temp);
      emp[i].salary = f_temp;
      fprintf(empfPtr, "%f", emp[i].salary);
    
      i++;
      puts("Next command (0 to print menu): ");
    }
    this seems to work fine.
    Now, there's an option that allows the user to view employee info (once entered), and here's what I've got:
    Code:
    void displayInfo(EMP *emp, FILE *empfPtr) {
      int i = 0;
      int j = 0;
      char temp[MAXLEN];
    
      puts("Enter employee last name (display info):");
      scanf("%s", temp);
      fgets(emp[i].last_name, NAMELEN, empfPtr);
      while(emp[i].last_name != temp) {
        i++;
        if(i == SIZE) {
          puts("No corresponding employee last name.");
          puts("Next command (0 to print menu): ");
        }
        fgets(emp[i].last_name, NAMELEN, empfPtr);
      }
      if(emp[i].last_name == temp)
        strcpy(emp[i].last_name, temp);
    
      fscanf(empfPtr, "%d", emp[i].ID);
      printf("Employee ID: %d\n", emp[i].ID);
      fscanf(empfPtr, "%s", emp[i].last_name);
      printf("Employee last name: %s\n", emp[i].last_name);
      fscanf(empfPtr, "%s", emp[i].first_name);
      printf("Employee first name: %s\n", emp[i].first_name);
      fscanf(empfPtr, "%d", emp[i].dept);
      printf("Employee department: %s\n", emp[i].dept);
      fscanf(empfPtr, "%f", emp[i].salary);
      printf("Employee salary: $%.2f\n", emp[i].salary);
    
      puts("Next command (0 to print menu): ");
      return;
    }
    this definitely doesn't work!! I feel like I have no idea what I'm doing, and it's getting really frustrating... cause I'm just guessing all of that.
    Can anyone help me out here?
    Last edited by Lau; 11-22-2002 at 10:32 AM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There might be more than this, but here's one problem:

    >while(emp[i].last_name != temp) {
    You can't compare strings like this, lookup strcmp().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    yeah you're right!! I had that, but switching things around, I forgot to change that too...
    I doubt that's what causes the whole thing not to work (would be WAY too easy) lol...

    It still gives me a segmentation fault!!
    would have been too easy...

    I don't understand. I had the whole thing right when we didn't have to use a file.
    Anyway, can you at least tell me if the first function looks correct to you? There might be something wrong with this one that causes the rest not to work!!
    Last edited by Lau; 11-22-2002 at 10:40 AM.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    fscanf(empfPtr, "%d", &emp[i].ID);

    Same for other ints/floats

  5. #5
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Thanks Hammer!! didn't even those stupid mistakes...

    Unfortunately, i'm still getting the segmentation fault

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    As per Monster's post, here's another with the same problem
    >fscanf(empfPtr, "%d", emp[i].dept);
    It should be
    >fscanf(empfPtr, "%d", &emp[i].dept);

    Maybe you should post your latest effort if you've made changes and it still doesn't work, but my guess is you calls to fscanf() are causing the seg faults.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    scanf("%s", temp);
    fgets(emp[i].last_name, NAMELEN, empfPtr);
    while(emp[i].last_name != temp) {
    Even when using the strcmp you will never find a match.
    First you need to remove the newline character from last_name:
    Code:
    scanf("%s", temp);
    fgets(emp[i].last_name, NAMELEN, empfPtr);
    strtok( emp[i].last_name, "\n" ); /* remove newline char */
    while(strcmp(emp[i].last_name, temp) != 0) {

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... or better still, don't mix scanf() with fgets(). Even better, dont use scanf() at all......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    I added all the & missing!!

    Hammer: why don't I want to use scanf()? I need to upper case the input in order to compare the strings. If I don't use scanf(), I can't compare, can I ??

    here's the lattest:
    Code:
    void displayInfo(EMP *emp, FILE *empfPtr) {
      int i = 0;
      int j = 0;
      char temp[MAXLEN];
      char buf[2];
    
      puts("Enter employee last name (display info):");
      scanf("%s", temp);
      upperCase(temp);
      fgets(emp[i].last_name, NAMELEN, empfPtr);
      strtok(buf, "\n");
      for (i=0; strcmp(emp[i].last_name, temp) != 0; i++) {
        if(i == SIZE) {
          puts("No corresponding employee last name.");
          puts("Next command (0 to print menu): ");
        }
        fgets(emp[i].last_name, NAMELEN, empfPtr);
      }
      if(emp[i].last_name == temp)
        strcpy(emp[i].last_name, temp);
    
      fscanf(empfPtr, "%d", &emp[i].ID);
      printf("Employee ID: %d\n", emp[i].ID);
      fscanf(empfPtr, "%s", emp[i].last_name);
      printf("Employee last name: %s\n", emp[i].last_name);
      fscanf(empfPtr, "%s", emp[i].first_name);
      printf("Employee first name: %s\n", emp[i].first_name);
      fscanf(empfPtr, "%s", emp[i].dept);
      printf("Employee department: %s\n", emp[i].dept);
      fscanf(empfPtr, "%f", &emp[i].salary);
      printf("Employee salary: $%.2f\n", emp[i].salary);
    
      puts("Next command (0 to print menu): ");
      return;
    }
    once I understand what I'm doing wrong I should be able to fix it everywhere!!

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You need to step out of the loop when i equals SIZE:

    There are other ways to do this but you can use the break to step out the for loop:
    Code:
    for (i=0; strcmp(emp[i].last_name, temp) != 0; i++) {
        if(i == SIZE) {
          puts("No corresponding employee last name.");
          puts("Next command (0 to print menu): ");
          break;
        }
        fgets(emp[i].last_name, NAMELEN, empfPtr);
      }

  11. #11
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    I just put a return; there, cause I want to get out of the function if it doesn't come up with any match!!
    Let's see... I don't get the segmentation fault anymore, but it won't find the match.?????

  12. #12
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Sorry that was my fault. I changed my previous post:
    Code:
    strtok(buf, "\n");
    must be:
    Code:
    strtok( emp[i].last_name, "\n" );
    or (this one is more common):
    Code:
    if(emp[i].last_name[strlen(emp[i].last_name)-1] == '\n')
       emp[i].last_name[strlen(emp[i].last_name)-1] = '\0';
    Last edited by Monster; 11-22-2002 at 11:08 AM.

  13. #13
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    and if I used fscanf(empfPtr, "%s", emp[i].last_name);
    wouldn't that allow me not to use the strtok() function?

  14. #14
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Yes, the fscanf function doesn't add the newline character to the buffer, so you don't need to remove it.

    But in order to make the fscanf function safe you need to do more than fscanf(empfPtr, "%s", emp[i].last_name);
    What happens if last_name is an array of 10 characters long and you're reading a name of 20 characters long? I know, segmentation fault.

  15. #15
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    ok, now just to get to the basics: (tell me if I'm wrong there cause that's how I understand the whole thing)
    (syntax is not correct, but I just want to know if I get the process right)

    I get an input from the user: x
    I have to scanf(x)
    then fprintf(x) to a file pointer
    right?

    then when I need x from the file to print on the monitor:
    get x with fscanf(x)
    then printf(x) onto the monitor?

    Am I on the right track there?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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