阅读背景:

逐行读取文件时读取和写入文件

来源:互联网 

Program:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
    FILE *f;
    char* line;
    size_t ln=100;
    char* s;
    line=new char[100];
    s=new char[100];
    cout<<"input key"<<endl;
    cin>>s;
    f=fopen("parvin.txt","r");
    if(f==NULL)
    {
        cout<<" no file TO read so creating for writing "<<endl;
        //return 0;
        f=fopen("parvin.txt","w");
        fputs(s,f);
        fputc('\n',f);
    }
    else
    {
        while(! feof(f))
        {
            fgets(line,100,f);
            cout<<line<<endl;

            //if(!strncmp(line,s,strlen(line)-1))
            if(strcmp(line,s)== 0 )
            {
                cout<<"duplicate found"<<endl;
                fclose(f);
                return 0;
            }
        }
        fclose(f);
        f=fopen("parvin.txt","a+");
        fputs(s,f);
        fputc('\n',f);
    }
    fclose(f);
}
#include<iostream>
#include<fstream>



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: