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

This is a discussion on Is there a convenient way to write ints & floats to a file in binary mode? within the C++ Programming forums, part of the General Programming Boards category; I'm converting them to strings using sprintf then using fwrite but I'm getting strange errors so I figure there must ...

  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,185
    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,185
    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,185
    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,185
    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.
    Attached Images Attached Images  

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    902
    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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 04:02 AM
  2. Write to binary file from 2D array
    By wbaker01 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2006, 05: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, 09:33 AM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

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