0

I am trying to write to two files and read from them. I was able to successfully input information to the two files ( words.txt & wordsTwo.txt). When I try to read from the two files, it will come out with symbols and not the text I set for it. Can some one help me solve this problem?

#include<iostream>
#include<fstream>

using namespace std;

int main() {

 // creating variables to input to files
 ofstream inputFirst;
 ofstream inputSecond;

 // creating variables to read from files
 ifstream readignFirst;
 ifstream readingSecond;

 inputFirst.open("words.txt");
 inputSecond.open("wordsTwo.txt");

 readignFirst.open("words.txt");
 readingSecond.open("wordsTwo.txt");

if(inputFirst.is_open() && inputSecond.is_open()){
     inputFirst<<"red";
     inputSecond<<"apple";
}

 //close input stream
inputFirst.close();
inputSecond.close();

 //storing characters
 char characterArrayOne[100];
 char characterArrayTwo[100];

//checking to make sure both are files are ready to be read
 if (readignFirst.is_open() && readingSecond.is_open()  ) {

 while (!readignFirst.eof() && !readingSecond.eof() ) {
    readignFirst >> characterArrayOne;
    readingSecond >> characterArrayTwo;
    cout<<" "<<characterArrayOne <<" "<<characterArrayTwo ;

 }
}else{

    cout<<"please have a input";

}
    cout<<" "<<characterArrayOne <<" "<<characterArrayTwo << "test" ;



//close input stream
inputFirst.close();
inputSecond.close();

return 0;
}
Tin Le
  • 1
  • 1
  • Dont use while (!.eof) – Jake Freeman Dec 06 '17 at 01:51
  • 1
    There are multiple bugs with the shown code. It is unspecified whether or not the entire concept of opening the same file for both reading and writing, ***first***, then writing to the same file using one stream, and reading it from the other, will work as intended. Furthermore, you are not guaranteed that the stuff written to the writing stream will be flushed in the first place. Basically, this entire approach is fundamentally broken, besides [while (!...eof()) also being broken](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Sam Varshavchik Dec 06 '17 at 01:53

0 Answers0