-
new question
I am trying to read in from an input file and everytime the word "is" appears in the file, replace it with "are".
will this work?
Code:
string word;
for (int i=0, i<kiSize, i++)
{
word=ifIn.getline(array[], 15);
if (word=="is")
{
word=="are";
}
}
thanks
-
You're reading it in, but you're not writing it back to the file. What you're gonna have to do is either write it to another file, or keep all of the words in a buffer and overwrite the original file.
You should also use ifIn>>word; so it breaks at the spaces.
-
word=ifIn.getline(array[], 15);
You should get some errors from that line. getline() returns a reference to the istream object, and it's parameters are char* and long.
istream& getline(char* pArray, streamsize n)
And, what the heck is:
array[]
?? Some undeclared, anonymous, dimensionless place holder? This is the way getline works:
char text[200];
ifIn.getline(text, 200);
cout<<text<<endl;
There is a version for string types too:
string line_of_data;
getline(ifIn, line_of_data);
Even if your "word=ifIn.getline()" statement did work, word is going to equal a whole line of data, so it's not going to equal "is".
-
i figured out the getline thanks for the help.
but now i have to read in a word and insert the word "uh" after the 4th letter in that word.
for example:
read in "programmer" and change it to "prog-uh-rammer"
how would i do that?
-
to leave programmer intact use a different buffer to hold proguhrammer. use a loop to read in first four char of word to change. Then add uh to new word. Then read last part of old word into new word. Works best if you use two different index variables, one for the old word and one for the new word, increasing the index variables appropriately.