Thread: Is there a convenient way to write ints & floats to a file in binary mode?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Is there a convenient way to write ints & floats to a file in binary mode?

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you want them to appear in binary form? If so, why are you converting them to strings using sprintf?

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Well, would there be any other way to write them then using fwrite?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you wanted them in the file in binary form, you would use fwrite only, without sprintf.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Doing fwrite(&i, sizeof(int), 1, fp) and fwrite(&fl, sizeof(float), 1, fp), I get even stranger errors.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to have to be way way way way more specific (as in, code and actual errors).

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Code:
    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();
    
    }
    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 this
    Code:
    Last:
    First and Middle:
    Salary:      ?(upside-down question mark)
    Year hired:
    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).

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    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.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    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();
    }
    Why does this produce garbage at the end of the salary?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mikal
    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.

    You should be able to change this:
    Code:
    snprintf(floats, 10, "&#37;.2f", employee[savecount].salary);
    fputs(floats, fp);
    to:
    Code:
    fprintf(fp, "%.2f", employee[savecount].salary);
    And to change this:
    Code:
    sprintf(ints, "%d", employee[savecount].yrhired);
    fputs(ints, fp);
    to:
    Code:
    fprintf(fp, "%d", employee[savecount].yrhired);
    thus removing the need for the floats and ints variables respectively.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Write to binary file from 2D array
    By wbaker01 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2006, 06:16 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM