En Linux, puedo ayudarlo a escribir el archivo de encabezado y la función principal para mytouch

Vamos a escribir un mytouch mixto feo de C y C ++

A veces queremos ejecutar touch para generar automáticamente un archivo c que contiene el archivo de encabezado que queremos, pero encontraremos que los archivos tocados en Linux están todos vacíos. Cuando vi que se abrió un archivo c vacío en el sistema Apple, el programa c inicial en la siguiente forma se escribiría automáticamente para usted.

#include<stdio.h>
int main(int argc,char**argv)
{
    
    
	return 0;
}

¿Podemos hacer este tipo de trucos bajo linux? La respuesta es Sí.
Solo necesitamos aprender un poco sobre la operación de archivos.
No solo puede configurar c, incluso puede configurar cualquier cpp, archivos de inicialización de java que desee

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include<sys/mman.h>
#include<pthread.h>
#include<semaphore.h>
#include <netinet/in.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include<dirent.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<errno.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#define CONFIGTXT "/home/adl/桌面/linux/小组任务/mytouch.txt"
/*
这里填你需要初始化文件的.txt文件名(其实.c也可),注意要绝对路径。
把你需要的头文件啊,main函数啊都写到.txt中,之后需要从中读取
*/

//我这不伦不类的用了string和char[]不美观!
void mytouch(char*writeFileName,string&configFileName)
{
    
    
    string line;
    struct stat statbuf;
    if(lstat(writeFileName,&statbuf)==-1){
    
    //判断我们需要创建文件是否存在,存在我们就先别覆盖,
    //我觉得覆盖太危险,就直接忽略,当然你可以去自个处理覆盖与否(这里用下c)
            ifstream is(configFileName);
            if(!is){
    
    cerr<<"出错了-----没有配置"<<endl;is.close();exit(1);}
            /*判断配置文件是否存在(这里用了点c++)*/
            ofstream os(writeFileName,ofstream::out|ofstream::trunc);
			/*从配置中一行一行读,再一行一行写入新文件中*/
            while(getline(is,line)){
    
    
                os<<line<<endl;
            }
            os.close();
        }else{
    
    
            puts("文件已经存在,先不操作"); 
            exit(1);
        }
}

int main(int argc,char**argv)
{
    
    
    string configtxt(CONFIGTXT);//默认配置
    if(argc==1){
    
    
        puts("参数少!");
        exit(1);
    }else if(argc==2){
    
    
        mytouch(argv[1],configtxt);//如果有俩参数则第二个参数就是你想创建的c文件
    }else if(argc==3){
    
    
        configtxt=argv[2];
        mytouch(argv[1],configtxt);//如果有仨参数则第三个参数是配置文件(可以是别的类型的文件)
    }
    return 0;   
}

Ah, eso es todo, ponga este directorio en / etc / environment, y luego fuente / etc / environment al
final

$ mytouch success.c

¡Mis resultados! ! !

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include<sys/mman.h>
#include<pthread.h>
#include<semaphore.h>
#include <netinet/in.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include<dirent.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<errno.h>
#include<signal.h>
#include<fcntl.h>

int main(int argc,char**argv)
{
    
    
    return 0;
}

Sí ~ ¡Es divertido hacer lo que quieras!

Supongo que te gusta

Origin blog.csdn.net/adlatereturn/article/details/105303327
Recomendado
Clasificación