⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tool.cpp

📁 简单的基于文件的数据库小程序
💻 CPP
字号:
#include "Tool.h"#include <sys/stat.h>#include <fcntl.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <iostream>#include <stdio.h> static char path[MAX_LEN]="/usr/database";static char dir[MAX_LEN]="/home/baicai/database/"; /** * 作为读写的缓冲区 * */static char buffer[MAX_LEN]; /** * 作为当前读到的一行的缓冲 * *///static char linebuffer[MAX_LEN];/** *初始化文件目录,创建/home/baicai/database 的目录 * 如果该目录已经存在,则什么都不作 * */void inits(){    char* linebuffer=new char[MAX_LEN];    getcwd(linebuffer,MAX_LEN);    strcpy(dir, linebuffer);    strcat(dir,"/data/");    mkdir(dir,S_IRUSR | S_IWUSR | S_IRGRP |S_IXUSR | S_IXGRP | S_IWGRP | S_IRGRP );    }/***得到当前目录*/char* getDir(){return dir;}/** * 向一个文件中添加新行 * 参数file是文件的名字,line是添加的内容。 * 文件在这个函数中打开,并在这个函数中关闭. * */ void addLineToFile(const char * file,const char * line){     strcpy(path,dir);     strcat(path,file);     checkFileExist(file);     int  fd=open(path,O_WRONLY | O_APPEND , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);          write(fd,line,strlen(line));     write(fd,"\n",1);     close(fd); }  /**  * 从文件中删除一行  * 参数file是文件的名字,line是删除的内容。  * 文件在这个函数中打开,并在这个函数中关闭.  * */void deleteOneLine(const char * file,const char * content){     char* linebuffer=new char[MAX_LEN];     strcpy(path,dir);     strcat(path,file);     checkFileExist(file);          int fd1=open(path,O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);     int result;     strcpy(path,dir);     strcat(path,"temp");     int fd2=open(path,O_CREAT | O_WRONLY , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);          while((linebuffer=getNextLine(fd1))!=NULL){	//        result=strcmp(linebuffer,content);	        if(strcmp(linebuffer,content)!=0){            write(fd2,linebuffer,strlen(linebuffer));	    //std::cout<<linebuffer<<endl;            write(fd2,"\n",1);                      }        }    close(fd1);    close(fd2);        strcpy(path,dir);     strcat(path,file);     unlink(path);//删除原文件     checkFileExist(file);             strcpy(path,dir);     strcat(path,file);          fd2=open(path,O_CREAT| O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);          fd1=openFile("temp");     while((linebuffer=getNextLine(fd1))!=NULL){	 if(linebuffer!="")        write(fd2,linebuffer,strlen(linebuffer));        //printf("get a new line:%s\n",linebuffer);        write(fd2,"\n",1);            }    close(fd1);    close(fd2);    strcpy(path,dir);    strcat(path,"temp");    unlink(path);}/** * 查看文件是否存在,存在返回true,不存在返回false; * */ bool hasFile(const char * name){    strcpy(path,dir);    strcat(path,name);    return access(path,F_OK)==0; } /** * 删除该文件 * */  void deleteFile(const char *name){    strcpy(path,dir);    strcat(path,name);    //checkFileExist(path);    unlink(path); }/** * 查看文件是否存在,不存在则创建一个文件. *参数 file是文件的名字 * */ void checkFileExist(const char * file){    strcpy(path,dir);    strcat(path,file);    int fd=open(path,O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);        close(fd); }  /**  *  查看文件夹是否存在,不存在的话创建一个  * 参数file是文件夹的名字  * */void checkDirectoryExist(const char * file){        strcpy(path,dir);    strcat(path,file);    mkdir(path,S_IRUSR | S_IWUSR | S_IRGRP |S_IXUSR | S_IXGRP | S_IWGRP | S_IRGRP );}/** * 打开一个文件,以作为读取行的开始 * 返回文件描述符 * */ int openFile(const char * file){    strcpy(path,dir);    strcat(path,file);    int fd=open(path,O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);     buffer[0]='\0';    return fd; } /**  * 关闭文件,以作为读取完的结束  * */ void closeFile(int fd){    close(fd); } /**  * 在getNextLine() 中使用  * */char getNextChar(int fd){    static int position=0;//表示当前的内容在buffer中的位置.    if(position>= (int)strlen(buffer)){        int result=read(fd,buffer,MAX_LEN);            if(result<=0){                return EOF;                                }                   buffer[result]='\0';          position=0;          return buffer[position++];            }    return buffer[position++];  } /** * 因为这个程序大多数都是处理一行一行的,所以用此函数将一个字符串中的下一行取出. * fd为文件的描述符 *读到的一行放入linebuffer中. * 返回负数表示已经到了文件末尾 * */char* getNextLine(int fd){       char* linebuffer=new char[MAX_LEN];    char c=getNextChar(fd);    int position=0;    while(c!='\n'){        if(c==EOF) return NULL;        linebuffer[position++]=c;        c=getNextChar(fd);          }       linebuffer[position++]='\0';   return linebuffer;}  StringToken::StringToken(const char *str,char point){    content=string(str);    position=0;    this->point=point;        };  bool StringToken::hasMore(){    //cout<<position<<" content.length()"<<content.length()<<endl;    return position < content.length();    };  char * StringToken::next(){    if(!hasMore()) return 0;    int begin=position;    while(position< content.length() && content[position]!=point){        buffer[position-begin]=content[position];        position++;    }    buffer[position-begin]='\0';    position++;    return buffer;    };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -