-
-
Save 02015678/65870b7e212d40ac6d69 to your computer and use it in GitHub Desktop.
C++中读写二进制与ASCII混合文本流测试
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
int main(int argc, char* argv[]) | |
{ | |
ofstream fout; | |
int n=31; | |
double v=9.9998e-79; | |
string str="Hello,World!"; | |
cout<<"Before"<<endl; | |
cout<<"n="<<n<<","<<"v="<<v<<endl; | |
cout<<str<<endl; | |
fout.open("test.out",ios::binary); | |
if(!fout.good()) | |
{ cout<<"CANNOT OPEN FILE FOR WRITE!"<<endl; return 1;} | |
fout.write( (char*)&n ,sizeof(n)); | |
fout.write( (char*)&v ,sizeof(v)); | |
fout.seekp( 1024, ios::beg); | |
fout<<"Hello,World!"<<endl; | |
fout.close(); | |
n=38;v=3.14159265354; | |
str="FAIL"; | |
ifstream fin("test.out",ios::binary); | |
if(!fin.good()) | |
{ cout<<"CANNOT OPEN FILE FOR READ!"<<endl; return 1;} | |
fin.read( (char*)&n ,sizeof(n)); | |
fin.read( (char*)&v ,sizeof(v)); | |
cout<<"After"<<endl; | |
cout<<"n="<<n<<","<<"v="<<v<<endl; | |
fin.seekg( 1024, ios::beg); | |
getline(fin,str); | |
cout<<str<<endl; | |
fin.close(); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment