淘宝网站那个做的,打广告的平台,渭南市建设工程有限责任公司,做网站心得体会1.读取文件的步骤#xff1a;
读文件步骤如下: 1.包含头文件 #include fstream 2.创建流对象 ifstream ifs; 3.打开文件并判断文件是否打开成功 ifs.open(“文件路径”,打开方式); 4. 读数据 四种方式读取 5.关闭文件 ifs.close(); 读取方法一#xff1a;
#include…1.读取文件的步骤
读文件步骤如下: 1.包含头文件 #include fstream 2.创建流对象 ifstream ifs; 3.打开文件并判断文件是否打开成功 ifs.open(“文件路径”,打开方式); 4. 读数据 四种方式读取 5.关闭文件 ifs.close(); 读取方法一
#includeiostreamusing namespace std;#includefstream//包含头文件//文本文件 读文件void test01() {//1.包含头文件//2.创建流对象ifstream ifs;//3.打开文件 并且判断是否打开成功ifs.open(test.txt,ios::in);if (!ifs.is_open()) {cout 文件打开失败 endl;return;}//4.读数据//第一种char buf[1024] { 0 };while (ifsbuf) {cout buf endl;}//5.关闭文件ifs.close();}int main() {test01();system(pause);return 0;
} 读取方法二
#includeiostreamusing namespace std;#includefstream//包含头文件//文本文件 读文件void test01() {//1.包含头文件//2.创建流对象ifstream ifs;//3.打开文件 并且判断是否打开成功ifs.open(test.txt,ios::in);if (!ifs.is_open()) {cout 文件打开失败 endl;return;}//4.读数据//第二种char buf[1024] { 0 };while (ifs.getline(buf, sizeof(buf))) {cout buf endl;}//5.关闭文件ifs.close();}int main() {test01();system(pause);return 0;
}
第三种读取方法
#includeiostreamusing namespace std;#includefstream#includestring//包含头文件//文本文件 读文件void test01() {//1.包含头文件//2.创建流对象ifstream ifs;//3.打开文件 并且判断是否打开成功ifs.open(test.txt,ios::in);if (!ifs.is_open()) {cout 文件打开失败 endl;return;}//4.读数据//第三种string buf;while (getline(ifs, buf) ){cout buf endl;}//5.关闭文件ifs.close();}int main() {test01();system(pause);return 0;
}
第四种读取文件方法
#includeiostreamusing namespace std;#includefstream#includestring//包含头文件//文本文件 读文件void test01() {//1.包含头文件//2.创建流对象ifstream ifs;//3.打开文件 并且判断是否打开成功ifs.open(test.txt,ios::in);if (!ifs.is_open()) {cout 文件打开失败 endl;return;}//4.读数据char c;//如果没有读到文件末尾就一直读。//EOF end of filewhile ((c ifs.get())!EOF) {cout c;}//5.关闭文件ifs.close();}int main() {test01();system(pause);return 0;
}