写入后尝试从文件打印会产生奇怪的输出

时间:2017-12-06 01:50:35

标签: c++ filereader

我正在尝试写入两个文件并从中读取。我能够成功地将信息输入到两个文件(words.txt& wordsTwo.txt)。当我尝试从这两个文件中读取时,它会出现符号而不是我为它设置的文本。有人可以帮我解决这个问题吗?

#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;
}

0 个答案:

没有答案