954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Readings chars from file into vector

I'm trying to read in the chars for a file in order to compare two text files. The problem is I can't seem to load space characters into my vectors. Any suggestions for me?

vector<char> cyphVector;
    char c;
    //fill vector with cypher file chars
    while( cyphFile.good() ){
       
       cyphFile >> c;
       cyphVector.push_back(c);
    }

    //fill vector with plain file chars
    vector<char> plainVector;
    char d;
    while(plainFile.good()){
       
       plainFile >> d;
       plainVector.push_back(d);
       cout << d;
    }
DeadJustice
Light Poster
44 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Does ifstream::get read whitespace? I think it can. Use .get() instead of using the >> operator. i.e. plainVector.push_back( cyphFile.get() );

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

Alright that worked. Though there was one problem. An y with . . over it was put at the end. Could you explain that?

EDIT: I also was wondering if I can merge all white space into one ' ' char as far as the vector is concerned.

DeadJustice
Light Poster
44 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Alright that worked. Though there was one problem. An y with . . over it was put at the end. Could you explain that?

EDIT: I also was wondering if I can merge all white space into one ' ' char as far as the vector is concerned.


I don't know what you mean here:An y with . . over it was put at the end.

Is that a Unicode character? A letter with two dots over it?

Regarding the merging of spaces, don't merge them. Simply don't push a space onto the vector if the last character pushed onto the vector was also a space. Throw the second, third, fourth spaces away and grab the next character from the input stream without pushing those spaces onto the vector:

char aChar = cyphFile.get ();
if (aChar == ' ')
{
     // check if top element of vector is a space.  If
     // not, push the space onto the vector.  If it is
     // a space, do nothing.
}
else
     // push aChar onto the vector.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You