第13周-閱讀專案4-寫出下面程式的輸出結果

kewlgrl發表於2015-06-10
問題及程式碼:

#include <iostream>   
#include <fstream>   
using namespace std;  
int main()  
 {       
    fstream outfile,infile;       
    outfile.open("data.dat",ios::out);       
    outfile<<"1111111111"<<endl;       
    outfile<<"aaaaaaaaaa"<<endl;       
    outfile<<"AAAAAAAAAA"<<endl;  
    outfile<<"**********"<<endl;  
    outfile.close();       
    infile.open("data.dat",ios::in);       
    char line[80];       
    int i=0;       
    while(!infile.eof())       
    {           
        i++;           
        infile.getline(line,sizeof(line));           
        cout<<i<<": "<<line<<endl;       
    }       
    infile.close();   
    return 0;  
}  


執行結果:



問題及程式碼:

#include <iostream>  
#include <fstream>  
using namespace std;  
int main()  
{  
    ifstream readFile;  
    ofstream writeFile;  
    char ch;  
    readFile.open("a.txt", ios::in);  
    writeFile.open("b.txt", ios::out);  
    while (readFile.get(ch))  
        writeFile.put(ch);  
    readFile.close();  
    writeFile.close();  
    cout << "Finish!" << endl;  
    return 0;  
}


執行結果:



問題及程式碼:

#include <iostream>  
#include <fstream>  
#include<cstring>  
using namespace std;  
int main()  
{  
    ifstream readFile;  
    ofstream writeFile;  
    char ch[100];  
    readFile.open("a.txt", ios::in);  
    writeFile.open("b.txt", ios::out);  
    while (!readFile.eof())  
    {  
        readFile.getline(ch,100,'\n');  
        writeFile.write(ch,strlen(ch));  
        writeFile.write("\n",1);  
    }  
    readFile.close();  
    writeFile.close();  
    cout << "Finish!" << endl;  
    return 0;  
}  


執行結果:




知識點總結:
檔案的讀取與寫入。

學習心得:

就是再次加強鞏固阿,反正是新技能get了~~

相關文章