Thread: Cannot open binary file

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    Cannot open binary file

    Hi everyone,
    I don't know what the problem is here. I had this assignment to do using a text file!! Not much problem there. Now we have to use the same assignment, and change the text file to a binary file!! The file won't open!!
    Can anyone tell m if I'm doing something wrong?
    Code:
    FILE * empfPtr;
    empfPtr = fopen("employee", "rb");
    if (empfPtr == NULL) {
      printf("cannot open %s file", argv[1]);
      exit(EXIT_FAILURE);
    }
    This is just part of the code. I just changed the mode from r+ to rb!! and everytime I run it, the "cannot open..." prints.
    Anyone has an idea?
    Thanks

  2. #2
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    perhaps you need to specify where the file is and any extension eg. a:\employee.dat

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    but don't binary files work like text files?
    That is: if the file doesn't exit, it will be created automatically??
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >That is: if the file doesn't exit, it will be created automatically??
    Not in read mode, only if you open the file for write or append will a nonexistant file be created.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    wb instead of rb made the trick!!
    Thank you Prelude!!

    I ran into another problem though... I'm not really familiar with binary files, and here's an example of what I wrote (and which doesn't work!!):
    Code:
      printf ("Enter new employee ID no.:");
      scanf("%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);
      fwrite(&emp, sizeof(EMP), 1, empfPtr);
    validateID() is just making sure the ID No is correct and not used for another employee... I'm not sure at all about my use of fscanf() and fwrite() here
    Last edited by Lau; 12-04-2002 at 11:45 AM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    Code:
    code:
    --------------------------------------------------------------------------------
      printf ("Enter new employee ID no.:");
      scanf("%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);
      fwrite(&emp, sizeof(EMP), 1, empfPtr);
    --------------------------------------------------------------------------------
    ...I'm not sure at all about my use of fscanf() and fwrite() here

    It's hard to tell if you are using fwrite correctly without seeing your variable declarations. fscanf looks fine. As far as fwrite goes as long as EMP is a valid data structure, &emp is a pointer to the beginning memory location where you want to get the information you will write to the file, and empfPtr is the FILE * that holds the address of the file you want to write to everything looks ok.

    fwrite returns the number of elements/datastructures it successfully writes to the file, so if you want to see if it is working, put it in an error checking if statement.

    Example:
    Code:
    if  ((fwrite(sourcemem,size,howmany,destfile))!=howmany)
             fprintf(stderr,"Fwrite is having problems");
    Just trying to help, anybody can let me know if I'm way off track.
    don't be a two-bit user

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Lau
    I ran into another problem though... I'm not really familiar with binary files, and here's an example of what I wrote (and which doesn't work!!):
    Code:
      printf ("Enter new employee ID no.:");
      scanf("%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);
      fwrite(&emp, sizeof(EMP), 1, empfPtr);
    What doesn't work? You're doing seven different things there. Which one isn't doing what you expect it to? Furthremore, are you just opening for writing, or are you opening for updating?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Monkey_C:
    when I get into that function, it prints "enter employee ID No", and when I enter a number I doesn't do the rest => asking for the last name, first name, etc. It just creates a blanck newline.
    I actually changed this part since I posted it: (here's the whole function)
    Code:
    void newEmployee(FILE * empfPtr) {
      int i_temp = 0;
      char s_temp[MAXLEN];
      float f_temp = 0;
      EMP emp;
    
      printf("Enter an employee ID No.: ");
      scanf("%d", &i_temp);
      printf("%d", i_temp);
      validateID (i_temp, empfPtr);
      emp.ID = i_temp;
      fseek(empfPtr, (emp.ID-1)*sizeof(EMP), SEEK_SET);
      fwrite(&emp.ID, sizeof(int), 1, empfPtr);
    
      puts("Enter employee last name:");
      scanf("%s", emp.last_name);
      upperCase(emp.last_name);
      fwrite(emp.last_name, sizeof(emp.last_name), 1, empfPtr);
    
      puts("Enter employee first name:");
      scanf("%s", emp.first_name);
      upperCase(emp.first_name);
      fwrite(emp.first_name, sizeof(emp.first_name), 1, empfPtr);
    
      puts("Enter employee department:");
      scanf("%s", s_temp);
      upperCase(s_temp);
      validateDept(s_temp);
      strcpy(emp.dept, s_temp);
      fwrite(emp.dept, sizeof(emp.dept), 1, empfPtr);
    
      puts("Enter employee salary:");
      scanf("%f", &f_temp);
      validateSalary(f_temp);
      emp.salary = f_temp;
      fwrite(&emp.salary, sizeof(emp.salary), 1, empfPtr);
    
      puts("Next command (0 to print menu): ");
      return;
    }
    Quzah:
    I open the file with wb (that updates, right?)
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You have to use "a" to open in update mode. "wb" just opens the file for writing. If it exists, it is truncated. (IE: Nuked, and you start from scratch.)

    You probably want: "ab". Or, if you're going to read and write, you'll want "ab+". See fopen for more info.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Ok, I got that!! thanks!!
    "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. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM