mfile.c

来自「深圳英培特EduKit-III实验箱实验程序。一共有10多个」· C语言 代码 · 共 72 行

C
72
字号
/*********************************************************************
* File:		mfile.c
* Author:	Embest	J.Zhao	2005.2.21
* Desc:		file function
* History:	
*********************************************************************/
#include <stdio.h>

struct sFile
{
	char szLetter[20];
};

int f_nMemb = 5;

int main(int argc, char *argv[])
{
	FILE *pFile;						/* file pointer */
	size_t size;
	int nTmp;
	char cTmp;
	char *pMode = "a+";					/* open or creat file */
	struct sFile sWbuf[f_nMemb];		/* file buf */
	struct sFile sRbuf[f_nMemb];		/* file buf */
	
	/* if input file name*/
	if(argc < 2)
	{
		printf("please scanf the file name\n");
		exit(0);
	}

	/* open file, or creat one if not exist*/
	if((pFile = fopen(argv[1], pMode)) == NULL)
	{
		printf("open error\n");
		exit(0);
	}
	
	/* write file */
	nTmp = 0;
	for(cTmp = 'a'; cTmp < 'f'; cTmp++)
	{	
		sprintf(sWbuf[nTmp].szLetter, "Letter : %c", cTmp);
		nTmp++;
	}
	size = fwrite(sWbuf, sizeof(struct sFile), f_nMemb, pFile);
	printf("write done.\n");
	
	/* reopen file, read data */
	if((pFile = freopen(argv[1], pMode, pFile)) == NULL)
	{
		printf("reopen error\n");
		exit(0);
	}
	size = fread(sRbuf, sizeof(struct sFile), f_nMemb, pFile);
	printf("read:\n");
	for(nTmp=0; nTmp < f_nMemb; nTmp++)
		printf("Buf[%d] = %s\n", nTmp, sRbuf[nTmp].szLetter);
	
	/* close file */
	fclose(pFile);
	
	/* delete file */
	if((nTmp = remove(argv[1])) == -1)
	{
		printf("remove error\n");
		exit(0);
	}
	return 0;
}

⌨️ 快捷键说明

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