09C++文件读取

文件读取

1.输出单个字符

#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    string str = "hello world";
    for (int i = str.length() - 1; i >= 0; i--)
    {
        cout.put(str[i]);
    }
    cout.put('\n');
    system("pause");
    return 0;
}

2.输出字符串

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    const char *str = "helloWorld";
    cout.write(str, 4);
    const string str2 = "helloWorld";
    string newStr = str2.substr(0, 4);
    cout << newStr << endl;
    system("pause");
    return 0;
}

3.cout.tellp()和cout.seekp()

1.cout.tellp()

首先,tellp() 成员方法用于获取当前输出流缓冲区中最后一个字符所在的位置,其语法格式如下

#include <iostream>
#include <fstream> //文件输入输出流
#include <string.h>
using namespace std;

int main(int argc, char const *argv[])
{
    std::ofstream out1;
    out1.open("tmps2/test2.txt");
    const char *str = "http://c.biancheng.net/cplus/";
    //将str字符串中的字符逐个输出到 test.txt 文件中,每个字符都会暂时存在输出流缓冲区中
    for (size_t i = 0; i < strlen(str); i++)
    {
        out1.put(str[i]);
        //获取当前输出流
        long pos = out1.tellp();
        std::cout << pos << std::endl;
    }
    //关闭文件之前,刷新outfile输出流缓冲区,使所有字符由缓冲区流入test.txt文件
    out1.close();
    system("pause");
    return 0;
}
2.cout.seekp()

seekp() 方法用于指定下一个进入输出缓冲区的字符所在的位置。 用于修改缓存区中的值

//指定下一个字符存储的位置
ostream& seekp (streampos pos);
//通过偏移量间接指定下一个字符的存储位置   
ostream& seekp (streamoff off, ios_base::seekdir way);
模式标志描 述
ios::beg从文件头开始计算偏移量
ios::end从文件末尾开始计算偏移量
ios::cur从当前位置开始计算偏移量
#include <iostream> //cin 和 cout
#include <fstream>  //文件输入输出流
#include <string.h>
using namespace std;


int main()
{
    //定义一个文件输出流对象
    ofstream outfile;
    //打开 test.txt,等待接收数据
    outfile.open("tmps2/test3.txt");
    const char *str = "http://c.biancheng.net/cplus/";
    //将 str 字符串中的字符逐个输出到 test.txt 文件中,每个字符都会暂时存在输出流缓冲区中
    for (int i = 0; i < strlen(str); i++)
    {
        outfile.put(str[i]);
        //获取当前输出流
    }
    cout << "当前位置为:" << outfile.tellp() << endl;
    //调整新进入缓冲区字符的存储位置
    outfile.seekp(23); //等价于:
                       //outfile.seekp(23, ios::beg);
                       //outfile.seekp(-6, ios::cur);
                       //outfile.seekp(-6, ios::end);

    cout << "新插入位置为:" << outfile.tellp() << endl;
    const char *newstr = "python/";
    outfile.write("python/", 7);
    //关闭文件之前,刷新 outfile 输出流缓冲区,使所有字符由缓冲区流入test.txt文件
    outfile.flush();
    outfile.close();
    system("pause");
    return 0;
}

4.按字符读取文件

遇到空格会换行

#include <iostream>
#include <fstream> //文件输入输出流
using namespace std;

int main(int argc, char const *argv[])
{
    ifstream infile;
    infile.open("tmps2/test2.txt", ios::in);
    if (!infile.is_open())
    {
        cout << "文件读取失败" << endl;
    }
    char buf[1024] = {0};
    while (infile >>buf) //遇到空格会换行
    {
        cout << buf << endl;//输出读取的文本文件数据
    }
    infile.close();
    system("pause");
    return 0;
}

5.按行读取文件

数组方法,逐行读取,可读取空格

#include <iostream>
#include <fstream> //文件输入输出流
using namespace std;

int main(int argc, char const *argv[] )
{
    ifstream infile;
    infile.open("tmps2/test2.txt", ios::in);
    if (!infile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    //第二种读取方法
    char buf[1024];
    while (infile.getline(buf, sizeof(buf)))
    {
        cout << buf << endl;
    }
    infile.close();
    system("pause");
    return 0;
}

6.按逐字符读取

#include <iostream>
#include <fstream> //文件输入输出流
using namespace std;

int main(int argc, char const *argv[])
{
    ifstream infile;
    infile.open("tmps2/test2.txt", ios::in);
    if (!infile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    char c;
    while ((c =infile.get()) != EOF)
    {
        cout << c;
    }
    cout << endl;
    infile.close();
    system("pause");
    return 0;
}

7.读取至Vector容器中

#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
    ifstream infile;
    infile.open("tmps2/test2.txt", ios::in);
    if (!infile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    //第五种读取方法
    string s;
    vector<string> v1;
    while (getline(infile, s))
    {
        v1.push_back(s);
    }
    for (size_t i = 0; i < v1.size(); i++)
    {
        cout << v1.at(i);
        cout << endl;
    }
    infile.close();
    system("pause");
    return 0;
}

8.写数据到文件

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

int main(int argc, char const *argv[])
{
    ofstream outfile;
    outfile.open("tmps2/test4.txt", ios::out | ios::app);
    if (!outfile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    outfile << "hello world" << endl;
    outfile << "世界 我来了";
    outfile.close();
    system("pause");
    return 0;
}
ios::in只读
ios::out只写
ios::app从文件末尾开始写,防止丢失文件中原来就有的内容
ios::binary二进制模式
ios::nocreate打开一个文件时,如果文件不存在,不创建文件
ios::noreplace打开一个文件时,如果文件不存在,创建该文件
ios::trunc打开一个文件,然后清空内容
ios::ate打开一个文件时,将位置移动到文件尾

ios::noreplace 默认存在

9.二进制读取

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

int main(int argc, char const *argv[])
{
    ofstream outfile;
    outfile.open("tmps2/test.txt", ios::out | ios::binary);
    if (!outfile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    outfile << "hello world" << endl;
    outfile << "世界 我来了";
    outfile.close();
    system("pause");
    return 0;
}
1.写入文件
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main(int argc, char const *argv[])
{
    ofstream outfile;
    outfile.open("tmps2/test.txt", ios::out | ios::binary);
    if (!outfile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    string str("Hello, world 张三李四");
    outfile.write(str.c_str(), sizeof(str));
    outfile.close();
    system("pause");
    return 0;
}
2.读取数据
#include <iostream>
#include <fstream> //文件输入输出流
using namespace std;

int main(int argc, char const *argv[] )
{
    ifstream infile;
    infile.open("tmps2/test.txt", ios::in|ios::binary);
    if (!infile.is_open())
    {
        cout << "读取文件失败" << endl;
        return -1;
    }
    //第二种读取方法
    char buf[1024];
    while (infile.getline(buf, sizeof(buf)))
    {
        cout << buf << endl;
    }
    infile.close();
    system("pause");
    return 0;
}

stream infile;
infile.open(“tmps2/test.txt”, ios::in|ios::binary);
if (!infile.is_open())
{
cout << “读取文件失败” << endl;
return -1;
}
//第二种读取方法
char buf[1024];
while (infile.getline(buf, sizeof(buf)))
{
cout << buf << endl;
}
infile.close();
system(“pause”);
return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值