文字列のみを含むテキストファイルを読み込もうとしています。コンパイルして開いていますが、読んでいると、ファイル内の文字とはまったく関係のないジャンクが表示されます。
誰かが何が悪いのかわかりますか?
#include <iostream>
#include <fstream>
using namespace std;
fstream myfile;
char* input;
void main(void)
{
myfile.open("H://fstream_test.txt", fstream::in);
if(myfile.fail())
{
cout<<"error"; exit(0);
}
cout<<"file is open";
int beginning = myfile.tellg();
myfile.seekg(0, fstream::end);
int end = myfile.tellg();
int size = end-beginning;
cout<<size; //returns 15
input = new char[size];
myfile.read(input,size);
cout<<input;
//returns junk
//for(int i=0;i<size;i++)
//cout<<input[i];
//returns the same;
}
編集を終了:
input = new char[size];
myfile.seekg(0, fstream::beg);
while(!myfile.eof())
{
myfile.read(input,size);
}
cout<<input;
system("pause");