文件操作

来源:互联网 发布:sql server设置外键 编辑:程序博客网 时间:2024/05/29 15:01

读文件(文件内容输出到屏幕)

写文件(运算结果输出到文件)

#include "stdafx.h"

#include <iostream>

#include <fstream>

using namespace std;

 

int main(int argc, char* argv[])

{

         ifstream ifile;

         ifile.open("myfile.txt");

    char ch;

    while(!ifile.eof())

    {

       ifile.get(ch);

       cout << ch;

    }

    ifile.close();

         return 0;

}

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <cmath>

using namespace std;

 

int main(int argc, char* argv[])

{  

         ofstream ofile;

         ofile.open("myfile.txt");

         cout<<"500以内的勾股弦数有:"<<endl;

         ofile<<"500以内的勾股弦数有:";

         int i,j,k;

         float p;

         for(i=1;i<500;i++){

                   for(j=i+1;j<500;j++){

                            k=i*i+j*j;

            p=sqrt(k);

                            if ((p==int(p))&&(p<=500)){

                                     cout<<p<<'\0'<<j<<'\0'<<i<<'\t';

                                     ofile<<p<<'\0'<<j<<'\0'<<i<<'\t';

                            }       

                   }

         }

         ofile.close();

         return 0;

}

原创粉丝点击