C语言中,如何删除一个txt文档中的内容?
发布网友
发布时间:2022-04-22 06:53
我来回答
共5个回答
热心网友
时间:2022-06-16 23:54
c语言将一个txt文件的所有信息删除的实现方法是以写模式打开一个同名文件,用来覆盖原来的文件,这样原来文件的内容将被删除。
具体显示方法如下:
int main()
{
int i;
FILE *data;
data = fopen("peding.txt", "w");
if(!data) {
perror("对不起,文件找不到");
return 1;
}
/*循环写入100次 */
for(i = 0; i < 100; i++)
writedata(data);
fclose(data);
return 0;
}
/*写入内容不操作*/
void writedata(FILE *data)
{
//刷新缓冲区
/* do other stuff */
...
}
热心网友
时间:2022-06-16 23:54
可以用批处理实现
system("for /f "delims=" %%i in ('dir /s /b *.txt') do echo.>%%i.txt");
记得要加上Windows库的头文件
注意这是将一个文件夹里的所有txt文档清空
指定单个txt文档可以用
system("echo >文件名.txt");
热心网友
时间:2022-06-16 23:55
楼主,你好!
代码如下:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *file;
file=fopen("1.txt","w");
fclose(file);
system("del 1.txt");
return 0;
}
希望可以帮助到楼主,谢谢采纳。
热心网友
时间:2022-06-16 23:56
#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;
}
热心网友
时间:2022-06-16 23:56
FILE *fp;
fp=fopen("d:\\1234.txt","w");
fclose(fp);
这样d:\1234.txt文件中就没有内容了。