文章出處

  問題描述:

  要求把第二個文件拼接到第一個文件的末尾。 如把file2 拼接到 file末尾。 (直接復制一下不就行了嘛! 但是老師非讓編程, 真是蛋疼!!,而且是閑的蛋疼!!!)。例如:

file1: I am not responsible of this code. They made me write it, against my will.   file2: When I wrote this, only God and I understood what I was doing. Now, God only kowns.   拼接后: file1: I am not responsible of this code. They made me write it, against my will. When I wrote this, only God and I understood what I was doing. Now, God only kowns.  

知識點: 文件流類。

 

#include<iostream>
#include<fstream>
using namespace std;

const int maxn = 1000+10;

int main()
{
    char str[maxn]; 
    ifstream cin("file2.txt");
    ofstream cout("file1.txt", ios::app);
    while(cin.getline(str, maxn))
    {
        cout<<str<<endl;
    }
    return 0;
}

一周內請勿抄襲! 以防作業雷同!

后面的題目也是我的作業題, 也都是關于I/0流的, 只不過比較水! I/0流是理解C++面向對象的典型例子之一, 所以在此也附上。

《2》使用I/O流, 以文本方式建立一個文件。并寫入一串字符。

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ofstream cout("file3.txt");
    cout<<"該文件已成功建立!";
    return 0; 
}
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

struct stu{
	string num;
	string name;
	double score;
}Stu; int n;

void judge(double a)
{
	cout<<"成績等級:";
	if(a>=90) cout<<"優\n";
	else if(a>=80) cout<<"良\n";
	else if(a>=70) cout<<"中\n";
	else if(a>=60) cout<<"及格\n";
	else cout<<"不及格\n";
}
int main()
{
	{//老是錯,最后終于對啦, 原來是作用域的鍋!!! 
	ofstream fc("stud.dat");
	if(fc.fail())
	{
		cerr<<"error opening file\n";
		return 0;
	}
	cout<<"請輸入學生的總人數:\n";
	cin>>n;
	int T=n;
	while(T--)
	{
		cout<<"請輸入學生的學號 姓名 成績\n";
		cin>>Stu.num>>Stu.name>>Stu.score;
		fc<<Stu.num<<" "<<Stu.name<<" "<<Stu.score<<" ";
	}
	}
	ifstream cc("stud.dat");
	while(n--)
	{
		cc>>Stu.num>>Stu.name>>Stu.score;
		cout<<"學號:"<<Stu.num<<endl;
		cout<<"姓名:"<<Stu.name<<endl;
		cout<<"成績:"<<Stu.score<<endl;
		judge(Stu.score);
	}
	return 0;
}

 


《3》使用串流I/O的方式, 對字符串逐個讀取。 如字符串“12345667899,,,,,”。

#include<iostream>
using namespace std;

int main()
{
    char c;
    while(c=cin.get())
    {
        cout<<c<<endl;
    }
    return 0;
}

《4》 輸入一系列的數據(學號, 姓名, 成績)存放在文件 stud.dat中, 輸出這些學生的數據和相應的成績等級(優, 良,,,,)。

這道題有細節性問題(氣死我啦!!!)。

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 using namespace std;
 5 
 6 struct stu{
 7     string num;
 8     string name;
 9     double score;
10 }Stu; int n;
11 
12 void judge(double a)
13 {
14     cout<<"成績等級:";
15     if(a>=90) cout<<"優\n";
16     else if(a>=80) cout<<"良\n";
17     else if(a>=70) cout<<"中\n";
18     else if(a>=60) cout<<"及格\n";
19     else cout<<"不及格\n";
20 }
21 int main()
22 {
23     {//老是錯,最后終于對啦, 原來是作用域的鍋!!! 
24     ofstream fc("stud.dat");
25     if(fc.fail())
26     {
27         cerr<<"error opening file\n";
28         return 0;
29     }
30     cout<<"請輸入學生的總人數:\n";
31     cin>>n;
32     int T=n;
33     while(T--)
34     {
35         cout<<"請輸入學生的學號 姓名 成績\n";
36         cin>>Stu.num>>Stu.name>>Stu.score;
37         fc<<Stu.num<<" "<<Stu.name<<" "<<Stu.score<<" ";
38     }
39     }
40     ifstream cc("stud.dat");
41     while(n--)
42     {
43         cc>>Stu.num>>Stu.name>>Stu.score;
44         cout<<"學號:"<<Stu.num<<endl;
45         cout<<"姓名:"<<Stu.name<<endl;
46         cout<<"成績:"<<Stu.score<<endl;
47         judge(Stu.score);
48     }
49     return 0;
50 }
View Code


《5》 在二進制文件中寫入三個記錄(啥是記錄?,百度解釋: 記錄:jì lù] 

 
記錄,漢語詞語,把所見所聞通過一定的手段保留下來,并作為信息傳遞開去。

然而弱智的我并不能清楚的理解, 想哭!), 然后顯示其內容。 然后刪除第二個記錄。 現在我以我的理解方式做題。我的理解: 就是刪除第二個字符串。

下面的代碼是推廣版的, 不僅適用于多個記錄, 而且能刪減任意一個記錄。

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 using namespace std;
 5 
 6 string str, str2;
 7 int n = 3;           //可以更改, 以便滿足多個記錄的情況
 8 
 9 int main()
10 {
11     {
12     ofstream fc("data.dat");
13     if(fc.fail())
14     {
15         cerr<<"error opening file\n";
16         return 0;
17     }
18     
19     cout<<"請依次輸入3個記錄\n";
20     for(int i=1; i<=n; i++)
21     {
22        cin>>str;
23        fc<<str<<" ";
24     }
25     } 
26     cout<<"輸入你要刪除的記錄:\n";
27     cin>>str2;
28     {
29     ifstream cc("data.dat");
30     cout<<"~~未刪減版\n\n";
31     for(int i=1; i<=n; i++)
32     {
33         cc>>str;
34         cout<<str<<endl;
35     }
36     }
37     {
38     ifstream cc("data.dat");
39     cout<<"~~刪減版,廣電總局的鍋!\n\n";
40     for(int i=1; i<=n; i++)
41     {
42         cc>>str;
43         if(str!=str2)cout<<str<<endl;
44     }
45     }
46     return 0;
47 }
View Code

 《6》刪除 文件里注釋

哎!最近在看“紫書”, 發現作者的代碼庫中的代碼是有不少是用中文寫的, 你知道的, 當我用cpp的格式打開的時候, 中文全部變成了亂碼, 一大波的亂碼。 對于十分看重代碼“長相”的我當然不能忍受。 誰敢褻瀆我神圣的代碼, 誰? 我就問一句, 他媽的還有誰?。所以,我就一不做, 二不休, 刪除了所有的注釋。但是一行一行的刪實在是太SB啦, 于是乎, 我就寫了下面的小程序。

刪除 文件里注釋的代碼。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<fstream>
 4 using namespace std;
 5 
 6 const int MAXN = 1000+10;
 7 char s[MAXN];
 8 
 9 int main()
10 {
11     int i, len, flag=0;//控制文件結束的標志 
12     ifstream cin("file.cpp");
13     ofstream cout("file2.cpp");
14     while(1)
15     {
16         cin.getline(s, MAXN);
17         len = strlen(s);
18         if(len==0) 
19         {
20             flag++;
21             if(flag==2) break;//這里表示,當連續出現兩個空行時,文件結束。 
22         }
23         else flag = 0;
24         for(i=0; i<len; i++)
25         {
26             if(s[i]==s[i+1]&&s[i]=='/')
27             break;
28         }
29         for(int j=0; j<i; j++)
30         cout<<s[j];
31         if(i!=0)cout<<endl;
32     }
33     return 0;
34 }
View Code


《7》提取文件里的注釋。

等我刪完注釋, 我那個成就感啊, 簡直就是爽的,,,,,。 然而, 問題很快就來啦。你們知道:“草灘小恪”--就是在下。是一個名副其實的菜鳥。 “紫書”上的代碼大都具有很巧妙地技巧。 如果沒有注釋,渣渣的,弱弱的, “草灘小恪”怎么可能看懂嘛!。所以“草灘小恪”一直想破譯亂碼。然而, 微軟早已看穿了一切。原來, “草灘小恪”無意中發現,用記事本直接打開cpp源文件, 里面的注釋竟然不是亂碼(記事本功能強大, 請有興趣者自行google)。 于是懶惰的“草灘小恪”希望把txt文件里面的注釋都提取出來。

于是下面的程序橫空出世!

 1 //提取注釋
 2 #include<iostream>
 3 #include<cstring>
 4 #include<fstream>
 5 using namespace std;
 6 
 7 const int MAXN = 1000+10;
 8 char s[MAXN];
 9 
10 int main()
11 {
12     int i, len, flag=0;//控制文件結束的標志 
13     ifstream cin("file1.txt");
14     ofstream cout("file.cpp");
15     while(1)
16     {
17         cin.getline(s, MAXN);
18         len = strlen(s);
19         if(len==0) 
20         {
21             flag++;
22             if(flag==2) break;//這里表示,當連續出現兩個空行時,文件結束。 
23         }
24         else flag = 0;
25         for(i=0; i<len; i++)
26         {
27             if(s[i]==s[i+1]&&s[i]=='/')
28             break;
29         }
30         for(int j=i; j<len; j++)
31         cout<<s[j];
32         if(i!=len)cout<<endl;
33     }
34     return 0;
35 } 
View Code

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()