c语言实现删除一个txt文件中含有xxx内容的一行,并把删除之后的文件写成另一份txt文件

从一个博客的回复上看到的,学习之后加了注释。源代码来自csdn博客。

通过这段代码学习到fgets实现从文件中一行一行读,fputs实现把每次读到的内容原样写到一个文件中,strstr查找包含某个xxx内容的字符串,feof判断文件末尾,#if #else #endif预编译命令,通过写bat文件向编译好的文件传入main函数的参数(这个同样可以通过在Project中设置实现向main函数传参数),实际上使用bat文件和在cmd使用dos命令作用是相同的,这里就相当于在cmd下cd进入到exe文件夹下,然后敲入xxx.exe arg[1] arg[2],其中后面为传入main函数的两个参数常用的c的文件操纵函数还有

//printf fprintf
//scanf fscanf

//getc fgetc
//putc fputc

//gets fgets
//puts fputs

//fread fwrite

//fseek fgetpos feof

 

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


#define DST_FILE   "dst.txt"
#define SRC_FILE   "src.txt"
//assume that every line is not larger than 256 bytes
#define MAX_LEN    256

static char achBuf[MAX_LEN] = {0};

static int RemoveSameStringLine(int argc, char **argv)
{
	FILE *fpSrc = NULL;
	FILE *fpDst = NULL;
	char achStr[50] = {0};
	int  nCount = 0;
	int  nArgc = 0;
	char achSrcF[50] = {0};
#if 1
	//argc the number of parameter, here must more than 2
	if (argc < 2)
	{
		printf("parameter is less than < 2\n");
		return -1;
	}

	strcpy(achSrcF, argv[1]);
	strcpy(achStr, argv[2]);
	printf("argc: %d\n", argc);
	for (nArgc = 0; nArgc < argc; nArgc++)
	{
		printf("argv[%d]: %s\n", nArgc, argv[nArgc]);
	}

	for (nArgc = 3; nArgc < argc; nArgc++)
	{
		strcat(achStr, " ");
		strcat(achStr, argv[nArgc]);
	}

#else
	strcpy(achSrcF, SRC_FILE)
		strcpy(achStr, SEEK_STR);
	printf("argv:[0]-%s,[1]-%s,[2]-%s\n", argv[0], SRC_FILE, SEEK_STR);
#endif

	printf("File:%s find str:\"%s\", and remove this str line\n", achSrcF, achStr);

	fpSrc = fopen(achSrcF, "rt");
	if (NULL== fpSrc)
	{
		printf("Open source file: %s failed\n", SRC_FILE);
		return -1;
	}

	fpDst = fopen(DST_FILE, "wt");
	if (NULL== fpDst)
	{
		printf("Create source file: %s failed\n", DST_FILE);
		fclose(fpSrc);
		return -1;
	}

	nCount = 0;

	while (!feof(fpSrc))
	{
		memset(achBuf, 0, sizeof(achBuf));
		//Get a string from a stream. the first parameter is the string, the second is the length of the string
		//the third parameter is the File stream
		//fgets reads characters from the current stream position 
		//to and including the first newline character, to the end of the stream, 
		//or until the number of characters read is equal to n – 1, whichever comes first.
		//The newline character, if read, is included in the string. 
		//so if we want to read a file line by line, we can use this function, and in almost every time we make the 
		//size of the buffer larger than the bytes of the most long line, and actually every time we can read a Enter
		//identifier from every line
		//通过对比ASCII码表,进行试验后发现,换行就是真的换了一行,
		//而回车只是一个字符而已,所以每个文件中的回车符可以通过char == "\n"来得到,换行符也是一个char型的ascii码
		fgets(achBuf, sizeof(achBuf), fpSrc);
		//Each of these functions returns a pointer to the first occurrence of strCharSet in string, 
		//or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.
		if (NULL == strstr(achBuf, achStr))
		{
			//Write a string to a stream. the first parameter is the string, and the second is the stream
			fputs(achBuf, fpDst);
		}
		else
		{
			nCount++;
		}
	}
	fclose(fpSrc);
	fclose(fpDst);
	printf("Total Count Same string:%d\n", nCount);

	return 0;
}

int main(int argc, char *argv[])
{
	int nRet;

	nRet = RemoveSameStringLine(argc, argv);
	printf("RemoveSameStringLine-ret:%d\n", nRet);

	Sleep(6000);

	return 0;
}


之后在编译好的exe文件同文件加下拷贝入源文件src.txt,之后写bat文件 xxx.exe src.txt delstringxxx即可

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值