c++小程序问题,帮看下

来源:百度知道 编辑:UC知道 时间:2024/06/28 14:57:29
#include <iostream>
#include <fstream>
using namespace std;
int main()
{fstream file1;
file1.open("XJZB1.txt",ios::in);//读
fstream file2;
file2.open("XJZB2.txt",ios::out|ios::trunc);//写
char ch;
while (!file1.eof())
{file1.read(&ch,1);
cout<<ch;
file2.write(&ch,1);
}
file1.seekg(6,ios::beg);
file1.read(&ch,1);
cout<<ch;//这里!!为什么没有输出??
file2.close();
file1.close();
cout<<endl;

return 0;
}

MSDN原文:

istream::read
istream& read( char* pch, int nCount );

istream& read( unsigned char* puch, int nCount );

istream& read( signed char* psch, int nCount );

Parameters

pch, puch, psch

A pointer to a character array.

nCount

The maximum number of characters to read.

Remarks

Extracts bytes from the stream until the limit nCount is reached or until the end of file is reached. The read function is useful for binary stream input.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
fstream file1;
file1.open("XJZB1.txt",ios::in);//读
fstream file2;
file2.open("XJZB2.txt",ios::out|ios::trunc);//写
char ch;
while (!file1.eof())
{
file1.read(&ch,1);
cout<<ch;
file2.write(&ch,1);
}
file1.seekg(6,ios::beg);