-
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
-
perhaps you need to specify where the file is and any extension eg. a:\employee.dat
-
but don't binary files work like text files?
That is: if the file doesn't exit, it will be created automatically??
-
>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
-
:rolleyes: :rolleyes: :rolleyes:
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
-
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.
-
Quote:
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.
-
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?)
-
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.
-
Ok, I got that!! thanks!!