System call (API) to achieve "process to create, read and write files" application

API implementation includes a "process to create, read and write files" application

In the Windows environment, the use of high-level language programming environment, call the relevant system call (API), including the realization of a "process to create, read and write files" application. There is a text file CommandList.txt, the first line of descriptive text: The last date this document is open and running 20,200,327. The second line is a line for each executable program name (including the path). An application can be written to open the file, and wherein each program execution sequence, and update the first line in the file.

代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<time.h> 
int main(int argc,char** argv)
{
	time_t t;//将t声明为时间变量
  	struct tm *p;//struct tm是一个结构体,声明一个结构体指针
  	time(&t);
  	p=localtime(&t);//获得当地的时间    
	FILE *fp;
	char load_file[256];
	char line[10][100];
	int k = 0;
	scanf("%s",load_file);
	//打开文件 ,读 
	fp=fopen(load_file,"r");
	if(fp==NULL)
	{
		printf("can not load file!");
		return 1;
	}
	bool flag;
	fgets(line[0],100,fp);
	while(!feof(fp))
	{	
		//按行读取,并将换行符去掉 
		fgets(line[k++],100,fp);
		printf("%s",line[k-1]);
		int temp = strlen(line[k-1]);
		if(line[k-1][temp-1]==10) line[k-1][temp-1]=0;
		//创建进程,打开exe文件 
		STARTUPINFO si = {sizeof(si)};
		PROCESS_INFORMATION pi;
		flag=CreateProcess(NULL,line[k-1],NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi);
		if(!flag){
			DWORD dwErrCode = GetLastError();
			printf("ErrCode : %d\n",dwErrCode);
		}
	}
	fclose(fp);
	//打开文件,写 
	fp=fopen(load_file,"w");
	if(fp==NULL)
	{
		printf("can not load file!");
		return 1; 
	}
	fputs("本文件最后一次打开和运行日期是",fp);
	fprintf(fp,"%d%02d%02d %02d:%02d:%02d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
	fputc('\n',fp);
	for(int i=0;i<k-1;i++){
		fputs(line[i],fp);
		fputc('\n',fp);
	}
	fputs(line[k-1],fp);
	fclose(fp);	
	return 0;
}

Released seven original articles · won praise 7 · views 205

Guess you like

Origin blog.csdn.net/weixin_44145894/article/details/105146355