Thread: saving a CString to binary file with trailing spaces

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    saving a CString to binary file with trailing spaces

    Heres what i want to happen except i want it to happen with a CString instead of a char []:

    Code:
    char name[10] = "Bob      ";
    
    ofstream fout(filename, ios::binary);
    
    fout.write(name, sizeof(name));
    
    fout.close();
    the output has 'Bob .' (with the trailing spaces).
    when i try this with CString it give me 'Bob.......' (with null's instead of spaces)
    I know that CString doesn't have the trailing spaces and is only 'Bob'.

    How do i either tell CString to be 10 spaces no matter what, or copy CString to Char[10] and have the spaces, or some other way of fixing it ;-)
    Last edited by nineofhearts; 01-02-2006 at 04:05 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to add the spaces to the CString manually. Subtract the result of GetLength() from 10 and that will tell you how many spaces to add to the end of the CString variable.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    i have tried

    Code:
    CString name = "Bob";
    name += "          ";
    But on the save funtion name becomes "Bob" again. and the spaces are gone.
    Last edited by nineofhearts; 01-02-2006 at 09:46 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    try this. main thing is to tack "\n" at the end of the string before writing it to that getlin() will read the whole string including spaces.
    Code:
    int main()
    {
    	char filename[] = "test.txt";
    	char name[] = "Bob      \n";
    	ofstream fout(filename);
    	fout.write(name, sizeof(name));
    	fout.close();
    	CString str;
    	ifstream fin(filename);
    	fin.getline(name,sizeof(name));
    	fin.close();
    	str = name;
    	cout << "'" << (LPCTSTR)str << "'\n";
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Can you show me the code you use to save and load the CString? No generic string class would dare discard spaces without being asked to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by CornedBee
    Can you show me the code you use to save and load the CString? No generic string class would dare discard spaces without being asked to.

    Didn't I just do that????

    you can also use CArchive to serialize CString. Something like this. I didn't compile it so there may be one or two syntax errors.
    Code:
    CString str = "Hello World      ";
    CFile f("filenme", <flags here>);
    CArchive ar(&f, CArchive::store);
    ar.WriteString(str);
    ar.Close();
    f.Close();
    //
    // read it back
    f("filename" <open flags here>);
    CArchive ar(&f, CArchive::load);
    ar.ReadString(str);
    ar.Close();
    f.Close();
    No generic string class would dare discard spaces without being asked to.
    C++ iostreams are designed to do just that!
    Last edited by Ancient Dragon; 01-02-2006 at 10:13 PM.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ar << MyCString;

    Works fine.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by Ancient Dragon
    C++ iostreams are designed to do just that!
    istream operator>> is designed to do that. Hence the use of get or getline.
    Woop?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> C++ iostreams are designed to do just that!
    Not on output.

    The spaces are probably being written out properly if the CString really does contain
    Code:
    "Bob       "
    Make sure you are checking with an appropriate text editor and not by reading the string back in.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And definitely not on binary writes. Besides, I was asking the OP, not you. It might be that his code has a bug. Actually I'm convinced his code has a bug.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by CornedBee
    Besides, I was asking the OP, not you. It might be that his code has a bug. .
    Myapologies for misunderstanding you.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Code:
    class RecAdj 
    {
    public:
    	RecAdj();
    
    	char m_invoice_c11[11];
    
    	void ReadRec(int number, CListBox* pListBox);
    	void SaveRecItem(CString filename);
    
    };
    
    
    void CRegisterDoc::OnReceive() 
    {
    	// TODO: Add your command handler code here
    	VendorDlg adlg;
    	if(adlg.DoModal() == IDOK)
    	{
    		RecItemdlg recdlg;
    		do
    		{
    			//adlg.m_invoicenumber is a CString its value here would ex. "Bob" with no spaces.
    			strcpy(recdlg.recitem.m_invoice_c11 , adlg.m_invoicenumber);
    		}while(recdlg.DoModal()==IDOK);
    	}//endif
    }
    
    
    
    void RecAdj::SaveRecItem(CString filename)
     {
     	std::ofstream fout(filename, std::ios::binary | std::ios::app);
     	if(fout.fail())
     		AfxMessageBox("Temp rec file failed to open", MB_OK);
    	else
    	{
     
      		fout.write(m_invoice_c11, sizeof(m_invoice_c11)); //as is "Bob........" is the output
    	}
    	fout.close();
     }
    the above is the acutall code minus the call funtion to SaveRecItem. Its called from another function but doesn't change or affect the data in anyway. I have stripped this down to be only one variable, in the actual code there are lots more.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I thought the problem was that you couldn't get it to work using CString. Show us the code where you try to do it with CString and it doesn't work (when you used += ".......").

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM