在C语言中我们如何来读写文件?只需调用freopen函数,stdin表示读入,而stdout标输出。具体操作如下:

#include<stdio.h>
int main(){
    freopen("Input.txt","r",stdin);  
     freopen("Output.txt","w",stdout); 
    return 0;
}

 

输出样例:

在desktop文件夹中会有一个Output.txt

 

C++中操作文件,它包含一个类,ofstream,(感觉跟Python的写法很像。。。)在头文件<fstream>中,cout就是这个类衍生出的一个变量,因此cout的用法都可以用于这个类,只是它是直接用于输出到文件中

  1. #include <iostream>  
    #include <fstream>  
    using namespace std;  
      
    int main()  
    {  
        ofstream outfile,fout;  
        outfile.open("haha.txt");  
        char s[50];  
        cin>>s;  
        //cout << fixed;  
        //cout.precision(2);  
        //cout.setf(ios_base::showpoint);  
        cout<<"写入文件的内容: "<<s<<endl;  
        outfile<<"写入文件的内容: "<<s<<endl;  //将输出写入到文件中
        outfile.close();  
        return 0;  
    } 
  2. 后续继续练习C++ 对文件的相关操作(待续。。。)
posted on 2016-10-19 21:27  Google-boy  阅读(581)  评论(0)    收藏  举报