19.1C++ 文件流操作

回车 (CR) 字符 ( 0x0D, \r) 将光标移动到行首而不前进到下一行。此字符在 Commodore 和早期 Macintosh 操作系统(OS-9 和更早版本)中用作换行符。
换行(LF) 字符 ( 0x0A, \n) 将光标向下移动到下一行,而不返回到行首。此字符在基于 UNIX 的系统(Linux、Mac OSX 等)中用作换行符
行尾 (EOL) 序列 ( 0x0D 0x0A, \r\n) 实际上是两个 ASCII 字符,即 CR 和 LF 字符的组合。它将光标向下移动到下一行和该行的开头。该字符在大多数其他非 Unix 操作系统(包括 Microsoft Windows、Symbian OS 等)中用作换行符。

windows:EOF首先在最后一行结束后(此时未换行)输入ENTER键,新起一行,再输入ctrl+z,再输入时ENTER键即可。
Linux:EOF
直接按CTRL+D快捷键

fstream、ifstream、ofstream创建新文件https://blog.csdn.net/hcf999/article/details/77864456

写文件

从控制台输入字符回显,并写入文件

#include<iostream>
#include<fstream>//引入文件流库
using namespace std;
int main()
{
    int a[3] = {1,2,3};
    ofstream OutputFile ;//1.创建输出流的文件流对象
    OutputFile.open("1.txt",ios::out);//2.打开文件
    if(!OutputFile)
    {
        cerr<<"openfile"<<endl;
        return -1;
    }
    int ch;
    cout<<"-->"<<endl;
    while ((ch = cin.get()) != EOF)
    {
        cout.put(ch);
        OutputFile << ch <<" ";//3.用输出运算符对文件进行写出
    }
    OutputFile.close();//4.关闭文件
    return 0;
}

读文件

Difference between while(!file.eof()) and while(file >> variable)

从文件读取字符并输出到控制台

#include<iostream>
#include<fstream>//引入文件流库
using namespace std;
int main()
{
    ifstream File;//1.创建输出流的文件流对象
    File.open("1.txt", ios::in);//2.打开文件
    if (!File)
    {
        cerr << "openfile faile" << endl;
        return -1;
    }

    int data;
    cout << "-->" << endl;
    
    //
    while (File >> data)   //file >> variable输入后检查file' 状态。每当您输入错误或遇到 EOF 时,条件都是false. 
        cout.put(data);
    
    /*
    while (!File.eof()){
        File >> data;
        cout.put(data);
    }
// File.eof()在读完最后一个数据时,eofbit仍然是false
// 只有当流再往下读取时,发现文件已经到结尾了,才会将标志eofbit修改为true.必须记住eof状态是在先读完文件之后才产生的.
//所以上面是错误写法,导致cout.put(data);多数输出了一次

    while (true)
    {
        File >> data;
        if (File.eof())break;
        cout.put(data);
    }

*/
/*
      bool
      eof() const
      { return (this->rdstate() & eofbit) != 0; }
    */

    File.close();//4.关闭文件
    return 0;
}

以二进制方式存取文件

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

int main()
{
    ofstream WriteFile ;
    WriteFile.open("1.txt",ios::in|ios::binary);
    if(!WriteFile)
    {
        cerr<<"openfile failure"<<endl;
        return -1;
    }
    // int ch;
    // cout<<"-->"<<endl;
    // while ((ch = cin.get()) != EOF)//从控制台一个个读
    // {
    //     cout.put(ch);
    //     WriteFile << ch <<" ";
    // }
    char a[3] = {0x61,0x62,0x63};
    WriteFile.write((char*)a,sizeof(a));//一次全部写进去
    WriteFile.close();

    ifstream ReadFile ;
    ReadFile.open("1.txt",ios::in|ios::binary);
    if(!ReadFile)
    {
        cerr<<"openfile failure"<<endl;
        return -1;
    }

    // while (!ReadFile.eof())//一个个往控制台写
    // {
    //     cout << hex << ReadFile.get();
    // }

    ReadFile.read((char*)a,sizeof(a));//一次全部读出来
    //cout<<a[0]<<a[1]<<a[2];
    cout<<a;

    ReadFile.close();//4.关闭文件
    return 0;
}

暂存

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

int main()
{
    ofstream WriteFile ;
    WriteFile.open("1.txt",ios::in|ios::binary);
    if(!WriteFile)
    {
        cerr<<"openfile failure"<<endl;
        return -1;
    }
    // int ch;
    // cout<<"-->"<<endl;
    // while ((ch = cin.get()) != EOF)//从控制台一个个读
    // {
    //     cout.put(ch);
    //     WriteFile << ch <<" ";
    // }
    char a[3] = {0x61,0x62,0x63};
    WriteFile.write((char*)a,sizeof(a));//一次全部写进去
    WriteFile.close();

    ifstream ReadFile ;
    ReadFile.open("1.txt",ios::in|ios::binary);
    if(!ReadFile)
    {
        cerr<<"openfile failure"<<endl;
        return -1;
    }

    // string str;
    // while (ReadFile.good())
    // {
    //     getline(ReadFile, str);  //该方法来自<string>
    //     cout << str << endl;
    // }
    
    
    while (!ReadFile.eof())//一个个往控制台写
    {
        ReadFile.getline(a,3);
        cout<<a;
    }

    ReadFile.close();//4.关闭文件
    return 0;
}

write和read方法主要是操作块数据

#include <iostream>
#include <fstream>
using namespace std;
class Rect
{
private:
    int length;
    int width;

public:
    void setLength(int l);
    void setWidth(int w);
    int getLength(void);
    int getWidth(void);
};
void Rect::setLength(int l)
{
    this->length = l;
}
void Rect::setWidth(int w)
{
    this->width = w;
}
int Rect::getLength(void)
{
    return length;
}
int Rect::getWidth(void)
{
    return width;
}

int main()
{
    Rect r;
    r.setLength(200);
    r.setWidth(100);

    ofstream outputfile("data.ini",ios_base::out);
    outputfile.write((char*)&r,sizeof(r));
    r.setLength(500);
    r.setWidth(200);
    outputfile.write((char*)&r,sizeof(r));
    outputfile.close();

    ifstream inputfile("data.ini",ios_base::in);
    inputfile.read((char*)&r,sizeof(r));
    cout <<  "Length: " << r.getLength() << endl;
    cout <<  "Width: " << r.getWidth() << endl;
    inputfile.read((char*)&r,sizeof(r));
    cout <<  "Length: " << r.getLength() << endl;
    cout <<  "Width: " << r.getWidth() << endl;
    inputfile.close();

    return 0;
}
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    fstream RWFile("data.ini",ios_base::in | ios_base::out | ios_base::binary);

    RWFile.seekg(0,ios_base::end);  //1.移动读指针到文件末尾
    streampos fileLength = RWFile.tellg();  //2.获取文件长度
    char* dataPtr = new char[fileLength];
    RWFile.seekg(0,ios_base::beg);  //3.移动读指针到文件开头
    RWFile.read(dataPtr,fileLength);   //4.读取长度fileLength的文件,写入dataPtr所指内存

    for(int i=0;i<fileLength;i++)
        cout << "-" <<dataPtr[i];
    cout << endl;
    cout << fileLength << endl;

    RWFile.seekp(-1,ios_base::end);  //1.移动读指针到文件末尾-1
    RWFile.write(dataPtr,fileLength);   //2.把dataPtr所指内存,长度fileLength,写入文件
    delete [] dataPtr;

    RWFile.seekg(0,ios_base::end);
    fileLength = RWFile.tellg();
    dataPtr = new char[fileLength];
    RWFile.seekg(0,ios_base::beg);
    RWFile.read(dataPtr,fileLength);

    for(int i=0;i<fileLength;i++)
        cout <<  "-" <<dataPtr[i];
    cout << endl;
    cout << fileLength << endl;;

    RWFile.close();
    delete [] dataPtr;
    return 0;
}
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    fstream RWFile("data.ini",ios_base::in | ios_base::out | ios_base::binary);

    RWFile.seekg(0,ios_base::end);  //1.移动读指针到文件末尾
    streampos fileLength = RWFile.tellg();  //2.获取文件长度
    char* dataPtr = new char[fileLength];
    RWFile.seekg(0,ios_base::beg);  //3.移动读指针到文件开头
    RWFile.read(dataPtr,fileLength);   //4.读取长度fileLength的文件,写入data所指内存

    for(int i=0;i<fileLength;i++)
        cout << "-" <<dataPtr[i];
    cout << endl;
    cout << fileLength << endl;

    RWFile.seekp(-1,ios_base::end);  //1.移动读指针到文件末尾-1
    RWFile.write(dataPtr,fileLength);   //2.把data所指内存,长度fileLength,写入文件
    delete [] dataPtr;

    char data;
    RWFile.seekg(0,ios_base::beg);
    while (!RWFile.eof())// 判断是不是读到了文件的末尾
     {
         RWFile.get(data);  //每次从文件流中读取一个字节存放到data中
         cout<<data;
     }

    cout << endl;

    RWFile.close();
    //delete [] data;
    return 0;
}
/*
 * 共能:新建文件1.txt,从控制台输入数据,复制1.txt到2.txt
*/
#include<iostream>
#include<fstream>//引入文件流库
using namespace std;
int main()
{
    fstream File1("1.txt",ios_base::out);
    int ch;
    cout<<"input:";
    while ((ch = cin.get()) != EOF)
        File1 << ch;
    File1.close();

    ifstream File2("1.txt",ios_base::in);
    if (!File2)
    {
        cerr << "openfile faile" << endl;
        return -1;
    }

    ofstream File3("2.txt",ios_base::out);

    //方法一:存到string
    /*
    string str;
    File2 >> str;
    File3 << str;
    */

    //方法二:新建动态数组
    File2.seekg(0,ios_base::end);   //读指针移到文件末尾
    streampos File2Length = File2.tellg();  //获取文件长度
    char* data = new char[File2Length]; //新建数组
    File2.seekg(0,ios_base::beg);   //读指针移到文件开头
    File2.read(data,File2Length);   //把文件内容读到data中
    File3.write(data,File2Length);  //把data所指的内存的数据写入到File3中

    File3.close();
    File2.close();
    delete[] data;

    return 0;
}

 以二进制方式存储STRING

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

int main(int args, char* argv[])
{
    string str = "agfshgdjhkjlkqrywtueiruotvbzxnbcmngthrdgtrhyjng\n";
    ofstream fout("1.txt",ios_base::binary | ios_base::out | ios::trunc);   //使用构造函数打开文件
    assert(fout);   //  静态断言,文件是否正常打开
    int len = str.length();
    fout.write(reinterpret_cast<char*>(&len),sizeof(int));//先存取字符串长度
    fout.write(&str[0],len);//再把字符传据存入文件
    fout.close();

    string str1;
    int len1;
    ifstream fin("1.txt",ios_base::binary | ios_base::in);
    assert(fin);
    fin.read(reinterpret_cast<char*>(&len1),sizeof(int));//先读取字符串长度,放入str1
    str1.resize(len);//改变str1大小
    fin.read(&str1[0],len);//再从文件读入到字符串str1中


    cout << str1;
    fin.close();
    return 0;
}

字符串流操作

#include <iostream>
#include <sstream>  //stringstream
#include <cassert>
using namespace std;

string i2str(int i)
{
    ostringstream oss;  //实例化输出字符串流对象
    oss << i;   //把i插入到输出流中
    return oss.str();   //返回流中的数据
}

int str2i(const string& str)
{
    int temp;
    istringstream iss(str); //用str作为参数,调用构造函数,生成输入流对象
    iss >> temp;    //从输入流中提取数据到temp中
    return temp;
}


int main(int args, char* argv[])
{
    int i = 10086;
    string str = i2str(i);
    cout << str << endl;

    i = str2i(str);
    cout << i << endl;

    return 0;
}

C++sstream三种输入输出流_虽千万人 吾往矣的博客-CSDN博客

#include <iostream>
#include <sstream>  //stringstream
#include <cassert>
using namespace std;


string ipconv(const string& str)    //sstream通常称为字符串转换流
{
    stringstream ss(str);  //构造有参数,绑定字符串提取流对象
    int a,b,c,d;
    char ch;

    cout << ss.fail() << ss.eof() << ss.bad() << ss.good() << endl;
    ss >> a >> ch >> b >> ch >> c >> ch >> d;
    cout << ss.fail() << ss.eof() << ss.bad() << ss.good() << endl;

    ss.clear(); //每一次ss中的数据读完之后,都会出出发eof标志位,所以再次使用前,需要清零
    cout << ss.fail() << ss.eof() << ss.bad() << ss.good() << endl;

    char temp= '.';
    ss << a << temp << b << temp << c << temp << d;

    return ss.str();
}


int main(int args, char* argv[])
{
    cout << ipconv("192,168,1,1") << endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值