1. 程式人生 > >第十四周閱讀程式1:檔案流

第十四周閱讀程式1:檔案流

問題及程式碼:

#include <iostream>
#include <fstream>
using namespace std;
int main()
 {
    fstream outfile,infile;    //定義檔案流物件
    outfile.open("data.dat",ios::out);       //以輸出方式開啟檔案 data,dat
    outfile<<"1111111111"<<endl;             //將字串存入磁碟檔案  data.dat
    outfile<<"aaaaaaaaaa"<<endl;
    outfile<<"AAAAAAAAAA"<<endl;
    outfile<<"**********"<<endl;
    outfile.close();           //關閉磁碟檔案  data.dat
    infile.open("data.dat",ios::in);         //以輸入方式開啟檔案 data,dat
    char line[80];
    int i=0;
    while(!infile.eof())       //成功開啟檔案的時候
    {
        i++;
        infile.getline(line,sizeof(line));   //得到每行的資料
        cout<<i<<": "<<line<<endl;
    }
    infile.close();            //關閉磁碟檔案  data.dat
    return 0;
}

執行結果:

執行後,在程式資料夾裡生成了一個data.dat 檔案

以記事本方式開啟檔案,看到結果正是輸入結果

執行結果,看到一共輸出了五行,而在data.dat檔案裡只有四行,那是因為在data.dat檔案的最後一行有一個回車符

知識點總結:
在運用檔案流的時候,首先標頭檔案  #include<fstream>

 然後定義一個物件

 ios::in    以輸入方式大開檔案

ios::out    以輸出方式開啟檔案,如果已有此名字的檔案,則將其原內容刪除

infile.getline(line,sizeof(line));   獲得每行的資料

相關推薦

四周閱讀程式1檔案

問題及程式碼: #include <iostream> #include <fstream> using namespace std; int main() { fstream outfile,infile; //定義檔案流物件

四周閱讀程式——1

/*Copyright (c) 2016,煙臺大學計算機學院 *All rights reserved. *檔名稱 : *作 者 : 徐聰 *完成日期 : 2016年6月5號 *版 本

四周閱讀程式1-(3)

/* * 程式的版權和版本宣告部分 * Copyright (c)2012, 煙臺大學計算機學院學生 * All rightsreserved. * 檔名稱: Vehicle.cpp * 作 者:李巧麗 * 完成日期:2013 年5月31日 * 版本號: v1.0 * 對任

四周閱讀程式1-(2)

/* * 程式的版權和版本宣告部分 * Copyright (c)2012, 煙臺大學計算機學院學生 * All rightsreserved. * 檔名稱: Vehicle.cpp * 作 者:李巧麗 * 完成日期:2013 年5月31日 * 版本號: v1.0 * 對任

四周----閱讀程式1)(2)

/* * 程式的版權和版本宣告部分 /* Copyright (c)2013, 煙臺大學計算機學院學生 * All rightsreserved. * 檔名稱: object.cpp * 作者:趙曉晨 * 完成日期: 2013年05月31日 * 版本號: v1.0 * 輸入

四周——閱讀程式1

#include<iostream> #include<vector> /*Copyright (c) 2016,煙臺大學計算機學院  *All rights reserved.  *檔名稱 :  *作    者 : 田志偉  *完成日期 : 201

四周閱讀程式2

#include <iostream> using namespace std; class Mammal { public: Mammal( ) { cout<<"Mammal constructor called."<<endl; }

四周閱讀程式

  #include <iostream> using namespace std; class BASE { private: char c; public: BASE(char n):c(n) {} virtual ~BASE() {

四周閱讀程式(2)

問題及程式碼: #include <iostream> #include <vector> using namespace std; int main() { vect

四周 閱讀程式 2

問題及程式碼: /* *Copyright (c)2014,煙臺大學計算機與控制工程學院 *All rights reserved. *檔名稱:Project.cpp *作 者:chenqin. *完成日期:2015年6月10日 *版 本 號:v1.0

四周閱讀程式3

/* * 程式的版權和版本宣告部分: * Copyright (c) 2013, 煙臺大學計算機學院 * All rights reserved. * 檔名稱:test.cpp * 作 者:任子儀 * 完成日期:2014年 5月 27日 * 版 本 號:v12.1 *

四周閱讀程式——例二

#include <iostream> #include <iomanip> using namespace std; int main() { int a; cout<<"input a:"; cin>&g

四周閱讀程式

問題 #include <iostream> using namespace std; int main() { int a=21; cout.setf(ios::showbase); cout<<"dec:"<<

四周閱讀程式——例四

4-1: #include <iostream> using namespace std; int main( ) { char *a="BASIC"; //字元指標指向'B' for(int i=4; i>=0; i--)

四周 【專案1

#include <stdio.h> #define MaxSize 100 //定義最大雜湊表長度 #define NULLKEY -1 //定義空關鍵字值 #define DELKEY -2 //定

四周【專案1

Copyright(c) 2017,煙臺大學計算機與控制工程學院 All rights reserved. 檔名稱:text.cpp 作者:黃瀟慧 完成日期:2017年12月5日 版本:vc6.0 問題描述: 認真閱讀並驗證雜湊表實施查詢

四周 【專案1-動物這樣叫】下面是給出的基類Animal宣告和main()函式。

#include <iostream> #include <string> using namespace std; class Animal { public: virtual void cry() { cout<&l

《現代作業系統(中文三版)》課後習題——章 例項研究1Linux

第十章 例項研究1:Linux 1.一個目錄包含以下的檔案: aardvark feret koala porpoise unicorn bonefish grunion llama quacker vicuna capybara hyena marmot rabbi

四周閱讀專案(2)

問題及程式碼: #include <iostream> #include <vector> using namespace std; int main() { vector<int> intList; vector<

16周閱讀程式——11

/* *Copyright (c) 2016,煙臺大學計算機學院 *All rights reserved. *檔名稱 : *作 者 : 劉雲 *完成日期 : 2016年6月15號 *版 本 號 :