1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to copy data from ifstream to a vector<char> ?

Discussion in 'MFC' started by RYoung992, Jun 26, 2010.

  1. RYoung992

    RYoung992 New Member

    Joined:
    Jun 26, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    How can I copy the data from an ifstream to a vector<char> ?

    The input file holds an array, so it contains something to the effect of:
    MapArray[10]{3,2,1,5,6,3,2,2,3,5,}

    The function I've written can read this and copy "10" to an array size variable, then the contents of the array between {} to a c-string. However with a c-string, the size must be constant, so I cannot use the variable from the input file to define it. This is why I am trying to use a vector.

    I've tried this:
    Code:
    int mSize = // data read from file, in example above, 10
    
    mapFile.ignore(25, '{');
    int vSize = mSize*2;			// vector size is twice the map size
    vector<char> mDataV(vSize);		// map Data Vector
    char mDataC[21];			// map Data C-string
    mapFile.getline(mDataC, vSize+1, '}');	// copy ifstream to mDataC
    mDataV.push_back(mDataC);		// copy mDataC to mDataV
    But this returns error C2664, saying it cannot convert mDataC from char[21] to const char &. I understand what this means but not how to resolve it. I tried replacing mDataC with *mDataC, and this compiled, but no data was copied to mDataV (it was empty, and no map was drawn on the screen).

    Sadface,
    R.Young
     

Share This Page