Thread: how get string vector from class in file?

  1. #1
    Registered User
    Join Date
    Jun 2005

    how get string vector from class in file?

    how can I store a class of string vectors as a bin or text file? I tried a .write/.read method which compiles fine, but I get a run time access violation error. Please see code below. I am using VC++ 6.0 and 7.0. Many thanks!


    vector<string>Feeder(1500); int Min1 = 0; string sMess1;
    void CfeedertestDlg::OnBnClickedButton1()
    mimix Mupp(Feeder);//declare empty class with empty string vector
    	Feeder[Min1] = sMess1;//int Min1 and string sMess1 defined by other buttons, set in vector.
     	Mupp.SetD1(Feeder); //set vector with string sMess1 at position Min1 in class Mupp.
    	ofstream fout1("MuppMem", ios::binary);//prepare file
    	fout1.write((char*) &Mupp, sizeof Mupp);//write class to file
    	Reader = Mupp.GetD1();//run time error here if loaded from bin/text class store file!
    	string sMess2 = Reader[Min1];//get string and display in message box:
    	char A[255];
    	ofstream fout("bupp.txt");
    	fout << sMess2;
    	ifstream fin("bupp.txt");
    	fin >> A;
    	CString cMess = A;
    Reader.erase(Reader.begin(), Reader.end());
    Feeder.erase(Feeder.begin(), Feeder.end());
    {itsD1 = initD1;
    void CfeedertestDlg::OnBnClickedButton6()
    	string sMess3;
    	mimix Mepp(Feeder);
    ifstream fin2("MeppMem",ios::binary);*) &Mepp, sizeof Mepp);
    	Reader = Mepp.GetD1();//problem here, but only if retrieved from file, see above!
    	char B[255];
    	ofstream fout4("buppo.txt");
    	fout4 << sMess3;
    	ifstream fin4("buppo.txt");
    	fin4 >> B;
    	CString cMess2 = B;
    //from class file:
    #pragma once
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    class mimix
    	void SetD1(vector<string>D1) {itsD1 = D1;}
    	vector<string> GetD1() const {return itsD1;}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    For writing, you need to iterate over the container and write each individual string in the vector to the file seperately, probably seperating them by newlines (text mode might be better for the I/O in this case). For reading you need to repeatedly get a line of text and push it into the vector. You cannot use the read/write file stream methods to do I/O with a vector... it doesn't work. As a shortcut, you might be able to use the copy function defined in the algorithm header to do both the reading and writing.

    #include <vector>
    #include <fstream>
    #include <string>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    vector <string> strVectOut, strVectIn;
    ofstream out;
    ifstream in;
    // Write strVectOut vector to the "out" file"file.txt");  // Open in text mode
    copy(strVectOut.begin(), strVectOut.end(), ostream_iterator<string>(out,"\n"));
    // Read from "in" file into strVectIn"file.txt");   // Open in text mode
    copy(istream_iterator<string>(in), istream_iterator<string>(), back_inserter(strVectIn));
    If the strings contain whitespace (space, tabs, newlines) you might need to do the reading/writing differently.

    I am unsure what you are trying to accomplish by creating the mimix wrapper. It doesn't really seem to do much for you.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Join Date
    Feb 2002
    You can save strings (including those with whitespace) to a binary file by first saving the length of the string and then writing the string. To load, read the length of the string, and then read that many bytes. Similarly, when storing a vector, you can save the length of the vector, and then save each entry. To load, read the length of the vector, and then read that many entries.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Jan 2005
    When using pianorain's method, make sure you are sending the data in the string to the write function, not the string itself. The data() function would be appropriate for this use, or the c_str() function (but you must remember that the c_str() function will add a null to the end so if you want that null written to your file you will have to write an extra byte). When reading in, you should read into a character buffer, make sure there is a terminating null and then assign that character buffer to your string instance. Simply writing and reading the string instances would not work as you have seen because they often hold dynamic memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM