zoukankan      html  css  js  c++  java
  • c++PrimerChap8IO库

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    
    
    int main(){
    	ofstream outFile("d:\test.txt",ofstream::out|ofstream::app);
    	if(!outFile){cout<<"Open file failed !"<<endl;	}else{cout<<"Open file suc!"<<endl;
    	}
    	//写入固定的str 
    	outFile<<"First!"<<endl;
    	//键入文件 
    	char ch;
    	while(cin.get(ch)){
    		if(ch  == '
    ')break;
    		outFile.put(ch);
    	}
    	outFile.close();
    	
    	
    	
    	ifstream readFile("d:\test.txt",ios::in);
    	if(!readFile){cout<<"read error!";return 0;
    	} else  cout<<"read suc"<<endl;
    //读取文本中所有的内容 
    	char c;
    	readFile.get(c);
    	cout<<"get-------------->"<<c<<endl; 
    	char str[100];
    	while(readFile.getline(str,100))	{
    	cout<<str<<endl;
    	}
    	readFile.close();
    	
    	
    	//binary二进制形式传送和存储。用read函数和write函数 
    	// read(char *buf,int len);  write(const char *buf,int len);
    		
    	fstream binaryFile("d:\test.txt",ios::out|ios::binary|ios::in);
    	if(!binaryFile){cout<<"read error!";return 0;
    	} else  cout<<"read suc"<<endl;
    	char arr[13] = "hello world!";
    	binaryFile.write(arr,13);
    	binaryFile.seekg(ios::beg);// 定位至文件首部
    	char read_array[13] ;
    	binaryFile.read(read_array,13);
    	cout<<"----------"<<read_array;
    	binaryFile.seekg(0,ios::beg);
    
    
    
    	
    //tellg()//返回输入文件读指针的当前位置;  
    //seekg(文件中的位置)//将输入文件中的读指针移动到指定位置
    //seekg(位移量,参照位置)//以参照位置为基准移动若干字节
    //beg//从文件开头计算要移动的字节数
    //cur//从文件指针的当前位置计算要移动的字节数
    //end//从文件的末尾计算要移动的字节数
    //一个回车键占2个字符 
    //	
    	
    	
    	
    	 
    
    	
        fstream binaryReadFile("d:\test.txt",ios::out|ios::binary|ios::in);
    	binaryReadFile.write(arr,12);
    	auto length = binaryReadFile.tellg();cout<<"length ----->"<<length<<endl;
    	char *buff = new char[length];
    	binaryReadFile.seekg(ios::beg);
    	static char read_array1[10]; // 在此我将打算读出些数据
     
        binaryReadFile.read(read_array1,3); // 读出前三个字符——"Hel"
     
        cout <<"read_array is ------>" <<read_array1 << endl; 
    	binaryReadFile.read(buff,length);
    	cout<<"buff is ------>"<<buff<<ends;
    	binaryReadFile.close();
    	
    	return 0;
    } 
    

      写的很乱,基本就是put,get,getline,write,read等的用法,及其中的一些相关参数。仅此记录吧。

    很多时候我都在期待3年后的自己是一个什么样的,5年后自己又是一个什么样的。因为未知,所以生命才更加精彩。
  • 相关阅读:
    【pycharm 密钥】pycharm 2017 密钥
    【jenkins 忘记密码】忘记Jenkins管理员密码的解决办法
    【git 报错】Could not read from remote repository.Please make sure you have the correct access rights.
    【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import
    【python接口自动化测试教程】00---00章节就代表开篇吧
    【python-strip】Python strip()方法
    认识map-reduce
    subprocess实用手册
    k8s学习路线
    nginx小知识
  • 原文地址:https://www.cnblogs.com/ashen/p/4402907.html
Copyright © 2011-2022 走看看