09C++文件读取

阅读: 评论:0

09C++文件读取

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;
}

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

#include <iostream>
#include <fstream> //文件输入输出流
#include <string.h>
using namespace std;int main(int argc, char const *argv[])
{std::ofstream out1;out1.open("");const char *str = "/";//将str字符串中的字符逐个输出到  文件中,每个字符都会暂时存在输出流缓冲区中for (size_t i = 0; i < strlen(str); i++){out1.put(str[i]);//获取当前输出流long pos = llp();std::cout << pos << std::endl;}//关闭文件之前,刷新outfile输出流缓冲区,使所有字符由缓冲区流入文件out1.close();system("pause");return 0;
}

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;//打开 ,等待接收数据outfile.open("");const char *str = "/";//将 str 字符串中的字符逐个输出到  文件中,每个字符都会暂时存在输出流缓冲区中for (int i = 0; i < strlen(str); i++){outfile.put(str[i]);//获取当前输出流}cout << "当前位置为:" << llp() << endl;//调整新进入缓冲区字符的存储位置outfile.seekp(23); //等价于://outfile.seekp(23, ios::beg);//outfile.seekp(-6, ios::cur);//outfile.seekp(-6, ios::end);cout << "新插入位置为:" << llp() << endl;const char *newstr = "python/";outfile.write("python/", 7);//关闭文件之前,刷新 outfile 输出流缓冲区,使所有字符由缓冲区流入文件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("", 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("", ios::in);if (!infile.is_open()){cout << "读取文件失败" << endl;return -1;}//第二种读取方法char buf[1024];while (line(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("", ios::in);if (!infile.is_open()){cout << "读取文件失败" << endl;return -1;}char c;while ((c &#()) != 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("", 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("", 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("", 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("", 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("", ios::in|ios::binary);if (!infile.is_open()){cout << "读取文件失败" << endl;return -1;}//第二种读取方法char buf[1024];while (line(buf, sizeof(buf))){cout << buf << endl;}infile.close();system("pause");return 0;
}

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


本文发布于:2024-02-04 10:22:58,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170705008554722.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文件
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23