zoukankan      html  css  js  c++  java
  • c++实现两个文件的拼接。

      问题描述:

      要求把第二个文件拼接到第一个文件的末尾。 如把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<<"优
    ";
    	else if(a>=80) cout<<"良
    ";
    	else if(a>=70) cout<<"中
    ";
    	else if(a>=60) cout<<"及格
    ";
    	else cout<<"不及格
    ";
    }
    int main()
    {
    	{//老是错,最后终于对啦, 原来是作用域的锅!!! 
    	ofstream fc("stud.dat");
    	if(fc.fail())
    	{
    		cerr<<"error opening file
    ";
    		return 0;
    	}
    	cout<<"请输入学生的总人数:
    ";
    	cin>>n;
    	int T=n;
    	while(T--)
    	{
    		cout<<"请输入学生的学号 姓名 成绩
    ";
    		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<<"";
    16     else if(a>=80) cout<<"";
    17     else if(a>=70) cout<<"";
    18     else if(a>=60) cout<<"及格
    ";
    19     else cout<<"不及格
    ";
    20 }
    21 int main()
    22 {
    23     {//老是错,最后终于对啦, 原来是作用域的锅!!! 
    24     ofstream fc("stud.dat");
    25     if(fc.fail())
    26     {
    27         cerr<<"error opening file
    ";
    28         return 0;
    29     }
    30     cout<<"请输入学生的总人数:
    ";
    31     cin>>n;
    32     int T=n;
    33     while(T--)
    34     {
    35         cout<<"请输入学生的学号 姓名 成绩
    ";
    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
    ";
    16         return 0;
    17     }
    18     
    19     cout<<"请依次输入3个记录
    ";
    20     for(int i=1; i<=n; i++)
    21     {
    22        cin>>str;
    23        fc<<str<<" ";
    24     }
    25     } 
    26     cout<<"输入你要删除的记录:
    ";
    27     cin>>str2;
    28     {
    29     ifstream cc("data.dat");
    30     cout<<"~~未删减版
    
    ";
    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<<"~~删减版,广电总局的锅!
    
    ";
    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
  • 相关阅读:
    【Visual C++】游戏开发笔记之六——游戏画面绘图(三)透明特效的制作方法
    【不定期更新】游戏开发中的一些良好习惯与技术技巧
    【Visual C++】游戏开发笔记之七——基础动画显示(一)定时器的使用
    【超级经典】程序员装B指南(转)
    Gentoo安装小记
    图形学中的贴图采样、走样与反走样等
    面试题之银行业务调度系统
    四川雅安芦山加油挺住
    ZOJ 3223 Journey to the Center of the Earth
    android中ListView拖动时背景黑色的问题
  • 原文地址:https://www.cnblogs.com/acm1314/p/4557759.html
Copyright © 2011-2022 走看看