I'm converting them to strings using sprintf then using fwrite but I'm getting strange errors so I figure there must be something simpler.
I'm converting them to strings using sprintf then using fwrite but I'm getting strange errors so I figure there must be something simpler.
Do you want them to appear in binary form? If so, why are you converting them to strings using sprintf?
Well, would there be any other way to write them then using fwrite?
If you wanted them in the file in binary form, you would use fwrite only, without sprintf.
Doing fwrite(&i, sizeof(int), 1, fp) and fwrite(&fl, sizeof(float), 1, fp), I get even stranger errors.
You're going to have to be way way way way more specific (as in, code and actual errors).
The above code is what I believe is relevant. Using this, whenever an employee object is "retrieved" (i.e. printed on the screen) regardless of the input, the ouput is thisCode:class data { public: char last[31], first[36]; float salary; int yrhired; void display(void); void modify(void); data(void); }employee[20]; void save(void){ int savecount = 0; FILE *fp; char endline[2]; endline[0] = '\n'; endline[1] = '\r'; fp = fopen("company.db", "wb"); while (savecount < 20){ fwrite("Last: ", 1, 6, fp); fwrite(employee[savecount].last, 1, 30, fp); fwrite(endline, 1, 2, fp); fwrite("First and Middle: ", 1, 18, fp); fwrite(employee[savecount].first, 1, 35, fp); fwrite(endline, 1, 2, fp); fwrite("Salary: ", 1, 8, fp); fwrite(&(employee[savecount].salary), sizeof(float), 1, fp); fwrite(endline, 1, 2, fp); fwrite("Year hired: ", 1, 12, fp); fwrite(&(employee[savecount].yrhired), sizeof(int), 1, fp); fwrite(endline, 1, 2, fp); savecount++; } void retrieve(void){ char character; FILE *fp; fp = fopen("company.db", "rb"); while ((character = fgetc(fp)) != EOF){ printf("%c", character); } fclose(fp); prompt(); }
When I use sprintf, if I edit say the third employee, in the salary and year hired place for employees 4 through 20 there are numbers following the data that shouldn't be there (e.g. instead of -1.00000 it's -1.0000 9).Code:Last: First and Middle: Salary: ?(upside-down question mark) Year hired:
Why do you expect fgetc to be able to read binary data? (Hint: It won't.) You use fread to read binary data back in. Or, if fgetc is what you want, then you don't want to use fwrite towrite binary data in the first place.
The main problem isn't the reading (which I have now fixed), it's the fact that the writing is all screwy. Take a look at the attachment.
I have no idea where you got the data for last and first, so who knows what's there. If you can read numeric data in notepad, you did it wrong.
Do you want numerical data in the text file, or "0110101.."? Because binary data will look like garbage.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Why does this produce garbage at the end of the salary?Code:void save(void){ int savecount = 0; FILE *fp; char newline[1]; newline[0] = '\n'; fp = fopen("company.db", "wb"); while (savecount < 20){ strcpy (ints," "); strcpy (floats, " "); fwrite("Last: ", 1, 6, fp); fputs(employee[savecount].last, fp); fwrite(newline, 1, 1, fp); fwrite("First and Middle: ", 1, 18, fp); fputs(employee[savecount].first, fp); fwrite(newline, 1, 1, fp); fwrite("Salary: ", 1, 8, fp); snprintf(floats, 10, "%.2f", employee[savecount].salary); fputs(floats, fp); fwrite(newline, 1, 1, fp); fwrite("Year hired: ", 1, 12, fp); sprintf(ints, "%d", employee[savecount].yrhired); fputs(ints, fp); fwrite(newline, 1, 1, fp); savecount++; } fclose (fp); prompt(); }
Where do the ints and floats variables come from?
Considering that this is C++, use std::fstream instead of FILE, and overload operator<< for data. Wait, why do you call the class data? You should call it Employee instead. Oh, and why do you have a global array? That array should be created elsewhere, e.g., in the main function.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I could make my code better but I'm okay with how it is. I just want to know why the code I posted produces garbage at the end of salary and I'm good to go.
I suspect that the bug has to do with the floats variable, but since it was not declared in the code that you posted, I cannot say what that bug might be.Originally Posted by Mikal
You should be able to change this:
to:Code:snprintf(floats, 10, "%.2f", employee[savecount].salary); fputs(floats, fp);
And to change this:Code:fprintf(fp, "%.2f", employee[savecount].salary);
to:Code:sprintf(ints, "%d", employee[savecount].yrhired); fputs(ints, fp);
thus removing the need for the floats and ints variables respectively.Code:fprintf(fp, "%d", employee[savecount].yrhired);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)