本文共 7560 字,大约阅读时间需要 25 分钟。
首先建立文本文件digit.txt,然后向文件写入个以空格分开的数字字符,最后再将10个字符从文件上读出到屏幕上
#include#include using namespace std;int main(){ //opent the file with way writing fstream testfile("file.txt",ios::out | ios::in); if(!testfile.is_open()) { cout<<"open the file abortively!"< >num; cout< >num; } cout<
实际运行结果
#include#include using namespace std;int main(){ //opent the file with way writing fstream testfile("file.txt",ios::out); if(!testfile.is_open()) { cout<<"open the file abortively!"< >num; while(!infile.eof()) { cout< <<" "; infile>>num; } cout<
#include#include using namespace std;int main(){ //opent the file with way writing fstream testfile("file.txt",ios::out|ios::in); if(!testfile.is_open()) { cout<<"open the file abortively!"< >num; if(!testfile.eof()) { cout<<"two pointers"< >num; } else { cout<<"only one pointer!"<
#includeusing namespace std;int main(){ //4-4 int n = 0; char ch; cout<<"please input some chars ,ending with enter!"<
#includeusing namespace std;int main(){ //4-4 int n = 0; char ch; cout<<"please input some chars ,ending with enter!"<
#include#include using namespace std;int main(){ int n1 = 0; char ch1; fstream outfile("filetest.txt",ios::out); outfile<<"Microsoft\nVisual\nC++"; outfile.close(); fstream infile("filetest.txt",ios::in); while(!infile.eof()) { ch1 = infile.get(); cout<
标准输入和输出
#include#include #include using namespace std;int main(){ int len , maxlen = 0; char s[8],t[8]; cout<<"please input some arrays of characters:"< maxlen) { maxlen = len; strcpy(t,s); } } cout<
#include#include #include using namespace std;int main(){ int len, maxlen = 0; char s[80],t[90]; fstream outfile; outfile.open("filetest1.txt",ios::out); if(!outfile.is_open()) { cout<<"open the file with writing is wrong!"< maxlen) { maxlen = len; strcpy(t,s); } } cout<<"the longest array of characters is:"< <
#include#include #include using namespace std;int main(){ int a = 123445; fstream outascii("file.txt",ios::out); fstream outbinary("file2.txt",ios::out | ios::binary); outascii<
功能:用于获取多个字符的输出
使用:in.read(字符型指针,字符个数); 从输入设备中读取n个字符,存入到字符型指针所指的内存空间中
注意: *如果是以二进制文件的方式进行的输入,要写入对应的数据的内存中,大小为该数据的所占用的内存空间大小
int main(){ char *ch = new char[8]; cin.read(ch,7); cout<<
int main(){ char *ch = new char[8]; fstream infile("file.txt",ios::in); if(!infile.is_open()) { cout<<"open the file abortively!"<
#include#include #include using namespace std;int main(){ //write ten number in the memory! fstream outbin("binary.txt",ios::out|ios::binary); if(!outbin.is_open()) { cout<<"open the file abortively!"< < 11;i ++) { outbin.write((char*)&i,sizeof(i)); } outbin.close(); //read the data from the memory fstream inbin("binary.txt",ios::in|ios::binary); if(!inbin.is_open()) { cout<<"open the file abortively!"< < 10;i ++) { inbin.read((char*)&i,sizeof(4)); cout< <
#include#include #include using namespace std;typedef struct Student{ char num[10]; char name[20]; int score;};int main(){ //write the data into the file Student stu[3] = { { "1210101","gray",618}, { "1608205","linux",746}, { "1564789","installman",789} }; fstream outfile("studentinfo.txt",ios::out | ios::binary); if(!outfile.is_open()) { cout<<"open the file abortively!"< < 3; i++) { outfile.write((char*)&stu[i],sizeof(stu[i])); cout<<"import the data triumphantly!"<
通过非顺序地移动指针实现对文件地随机读写
seekg和seekp
tellg和tellp
#include#include #include using namespace std;typedef struct Student{ char num[10]; char name[20]; int score;};int main(){ //write the data into the file Student stu[3] = { { "1210101","gray",618}, { "1210102","linux",746}, { "1210103","installman",789} }; fstream outfile("studentinfo.txt",ios::out | ios::binary); if(!outfile.is_open()) { cout<<"open the file abortively!"< < 3; i++) { outfile.write((char*)&stu[i],sizeof(stu[i])); cout<<"import the data triumphantly!"< >num; if(num>3|num<1) { cout<<"the student you want to check is inexistence!"<
typedef struct Student{ char num[10]; char name[20]; int score;};//the overload of <>istream& operator>>(istream& in,Student stu){ in>>stu.num>>stu.name>>stu.score; return in;}
转载地址:http://bwwvb.baihongyu.com/