ca74a_c++__文件流对象的使用-用来读写文件ifstream

/*ca74a_c++__文件流对象的使用-用来读写文件
将文件流对象绑定到文件上
检查文件是否打开成功
将文件流与新文件重新绑定
清楚文件流的状态
infile.close();//关闭流
infile.clear();//恢复流的状态,不然infile依然停留在infile.eof的位置。不能使用。
eof: end of file

ifstream infile("one.txt");//,定义infile文件流对象,直接绑定one.txt
//也可以如下写法,但要用c风格file.c_str(),这个c++保留了c风格的方法
ifstream infile2(file.c_str());//c++风格又变回c风格字符串,绑定并打开one.txt

ifstream infile3;//先定义流对象,没有绑定文件

infile3.open("one.txt");//用open方式绑定one.txt
//if (infile3)//判断打开文件是否成功
if (!infile3)//判断打开文件是否错误
{
cerr << "打开文件失败: " <<file<< endl;
return -1;
}

vector<string> files;
string ss1;
file.push_back("one.txt"); //files少写了一个s,就会C2664

error C2664: “void std::basic_string<char,std::char_traits<char>,std::allocator<char>>::push_back(const _Elem)”: 无法将参数 1 从“const char [8]”转换为“const _Elem”
1> with
welcome to discuss
txwtech@163.com
*/

  1 /*ca74a_c++__文件流对象的使用-用来读写文件
  2 将文件流对象绑定到文件上
  3 检查文件是否打开成功
  4 将文件流与新文件重新绑定
  5 清楚文件流的状态
  6 infile.close();//关闭流
  7     infile.clear();//恢复流的状态,不然infile依然停留在infile.eof的位置。不能使用。
  8 eof: end of file
  9 
 10 ifstream infile("one.txt");//,定义infile文件流对象,直接绑定one.txt
 11     //也可以如下写法,但要用c风格file.c_str(),这个c++保留了c风格的方法
 12     ifstream infile2(file.c_str());//c++风格又变回c风格字符串,绑定并打开one.txt
 13 
 14     ifstream infile3;//先定义流对象,没有绑定文件
 15     
 16     infile3.open("one.txt");//用open方式绑定one.txt
 17     //if (infile3)//判断打开文件是否成功
 18     if (!infile3)//判断打开文件是否错误
 19     {
 20         cerr << "打开文件失败: " <<file<< endl;
 21         return -1;
 22     }
 23 
 24 vector<string> files;
 25     string ss1;
 26     file.push_back("one.txt"); //files少写了一个s,就会C2664
 27 
 28     error C2664: “void std::basic_string<char,std::char_traits<char>,std::allocator<char>>::push_back(const _Elem)”: 无法将参数 1 从“const char [8]”转换为“const _Elem”
 29 1>        with
 30     welcome to discuss
 31 txwtech@163.com
 32 */
 33 
 34 #include <iostream>
 35 #include <fstream>//文件流
 36 #include <string>
 37 #include <vector>
 38 using namespace std;
 39 void process(string s)
 40 {
 41     cout << s ;
 42     
 43 }
 44 
 45 int main()
 46 {
 47     ofstream outfile("test.txt");//创建一个文件,c风格的字符串,变成c++的string
 48     outfile << "hello file!";//写入内容
 49     outfile.close();//关闭文件
 50 
 51     string file("one.txt");//表示file="one.txt";
 52     ifstream ();
 53 
 54     ifstream infile("one.txt");//,定义infile文件流对象,直接绑定one.txt
 55     //也可以如下写法,但要用c风格file.c_str(),这个c++保留了c风格的方法
 56     ifstream infile2(file.c_str());//c++风格又变回c风格字符串,绑定并打开one.txt
 57     infile2.close();//关闭流对象
 58 
 59     //或者
 60     ifstream infile3;//先定义流对象,没有绑定文件
 61     
 62     infile3.open("one.txt");//用open方式绑定one.txt
 63     //if (infile3)//判断打开文件是否成功
 64     if (!infile3)//判断打开文件是否错误
 65     {
 66         cerr << "打开文件失败: " <<file<< endl;
 67         return -1;
 68     }
 69     
 70     //或者  infile3.open(file.c_str());方法
 71     infile3.close();
 72     string s;
 73 
 74     while (infile >> s) //把内容读取到字符串中
 75     {
 76         cout << s;
 77         if (!infile.eof())//如果没有都到结束符,就添加逗号.eof:end of file
 78             cout << ",";
 79     }
 80     
 81     cout << endl;
 82     infile.close();//关闭流
 83     infile.clear();//恢复流的状态,不然infile依然停留在infile.eof的位置。不能使用。
 84     //cout << "读取到的内容:" << s << endl;
 85 
 86     //再次使用infile
 87     string file2 = "two.txt";
 88     string s2;
 89     infile.open(file2.c_str());
 90     if (!infile)//判断打开文件是否错误
 91     {
 92         cerr << "打开文件失败: " << file2 << endl;
 93         return -1;
 94     }
 95     while (infile >> s2)
 96     {
 97         cout << s2;
 98         if (!infile.eof())
 99             cout << ",";
100     }
101     cout << endl;
102     infile.close();
103     infile.clear();
104 
105     //例子2:
106     cout << endl;
107     cout << "vector方式打开文件" << endl;
108     vector<string> files;
109     string ss1;
110     files.push_back("one.txt");
111     files.push_back("two.txt");
112     files.push_back("tt1.txt");
113     files.push_back("three.txt");
114     files.push_back("test.txt");
115 
116     vector<string>::const_iterator it = files.begin();
117     while (it != files.end())
118     {
119         ifstream input2(it->c_str()); //打开vector的文件
120         if (!input2)
121         {
122             cerr << "打开失败!" << *it<<endl;
123             //break;//要么停止打开
124             input2.clear();//要么先清除流,继续打开
125             ++it;
126             continue;
127 
128         }
129         while (input2 >> ss1)
130         {     process(ss1);
131            if (!input2.eof())
132             cout << ",";
133         }
134         cout << endl;
135         input2.close();
136         input2.clear();
137         ++it;
138         
139             
140 
141 
142     }
143 
144     return 0;
145     
146 }
欢迎讨论,相互学习。 txwtech@163.com
原文地址:https://www.cnblogs.com/txwtech/p/12294371.html