char ch;
fpos_t ptr;
fgetpos(fp,&ptr);
ch=fgetc(fp);
while(ch!='\n')
{
fsetpos(fp,&ptr);
fputc(0,fp);
fgetpos(fp,&ptr);
ch=fgetc(fp);
}
fputc(0,fp);
5 个解决方案
#1
还是简单一点吧,全部读入内存,删那一行,根据内存内容重建文件。
#2
while(成功读入一行)
{
if(这行需要保留)
{
追加到临时文件
}
}
把临时文件重命名
{
if(这行需要保留)
{
追加到临时文件
}
}
把临时文件重命名
#3
还是简单一点吧,全部读入内存,删那一行,根据内存内容重建文件。
=============================================================
对,就这样,up之
=============================================================
对,就这样,up之
#4
【Ref】
#include <stdio.h>
#include <string.h>
void InsertLine(char* FileName,int L,char str[256])
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[50][256]={0};
//char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while( fgets(Buf,256,fp) )
{
Lid++;
if(Lid==L)
strcpy(tmp[Lid++],str);
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
for(Lid=1;Lid<=MaxLine;Lid++)
fputs(tmp[Lid],fp);
fclose(fp);
}
void cat(char *filepath)
{
int nl = 0;
FILE *stream;
char s[256];
char *p = s;
stream = fopen(filepath, "r+");
while((p = fgets(s, 256, stream)) != NULL)
{
nl++;
printf("Line %d: %s", nl, s);
}
fclose(stream);
}
void deleteLine(char* FileName,int lineno)
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[20][256]={0};
char *p = Buf;
// char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while((p=fgets(Buf,256,fp))!=NULL)
{
Lid++;
if(Lid==lineno)
{
if((p=fgets(Buf,256,fp))!=NULL) // 原文多了个分号
{
strcpy(tmp[Lid],Buf);
}
}
else
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
fclose(fp);
remove(FileName); // 删除原文件
if( (fp=fopen(FileName,"w"))==NULL)
{
printf("Can't open file!\n");
return;
}
for(Lid=1;Lid<=MaxLine;Lid++) // 多写一行
fputs(tmp[Lid],fp);
fclose(fp);
}
int main()
{
cat("c:\\a.txt");
printf("\n......................................................\n");
InsertLine("c:\\a.txt",3,"7777\n");
cat("c:\\a.txt");
printf("\n......................................................\n");
deleteLine("c:\\a.txt",3);
printf("\n......................................................\n");
cat("c:\\a.txt");
return 0;
}
#include <stdio.h>
#include <string.h>
void InsertLine(char* FileName,int L,char str[256])
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[50][256]={0};
//char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while( fgets(Buf,256,fp) )
{
Lid++;
if(Lid==L)
strcpy(tmp[Lid++],str);
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
for(Lid=1;Lid<=MaxLine;Lid++)
fputs(tmp[Lid],fp);
fclose(fp);
}
void cat(char *filepath)
{
int nl = 0;
FILE *stream;
char s[256];
char *p = s;
stream = fopen(filepath, "r+");
while((p = fgets(s, 256, stream)) != NULL)
{
nl++;
printf("Line %d: %s", nl, s);
}
fclose(stream);
}
void deleteLine(char* FileName,int lineno)
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[20][256]={0};
char *p = Buf;
// char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while((p=fgets(Buf,256,fp))!=NULL)
{
Lid++;
if(Lid==lineno)
{
if((p=fgets(Buf,256,fp))!=NULL) // 原文多了个分号
{
strcpy(tmp[Lid],Buf);
}
}
else
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
fclose(fp);
remove(FileName); // 删除原文件
if( (fp=fopen(FileName,"w"))==NULL)
{
printf("Can't open file!\n");
return;
}
for(Lid=1;Lid<=MaxLine;Lid++) // 多写一行
fputs(tmp[Lid],fp);
fclose(fp);
}
int main()
{
cat("c:\\a.txt");
printf("\n......................................................\n");
InsertLine("c:\\a.txt",3,"7777\n");
cat("c:\\a.txt");
printf("\n......................................................\n");
deleteLine("c:\\a.txt",3);
printf("\n......................................................\n");
cat("c:\\a.txt");
return 0;
}
#5
还是简单一点吧,全部读入内存,删那一行,根据内存内容重建文件。
=============================================================
对,就这样,up之
能不能更具体一点,搞不太明白。
=============================================================
对,就这样,up之
能不能更具体一点,搞不太明白。
#1
还是简单一点吧,全部读入内存,删那一行,根据内存内容重建文件。
#2
while(成功读入一行)
{
if(这行需要保留)
{
追加到临时文件
}
}
把临时文件重命名
{
if(这行需要保留)
{
追加到临时文件
}
}
把临时文件重命名
#3
还是简单一点吧,全部读入内存,删那一行,根据内存内容重建文件。
=============================================================
对,就这样,up之
=============================================================
对,就这样,up之
#4
【Ref】
#include <stdio.h>
#include <string.h>
void InsertLine(char* FileName,int L,char str[256])
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[50][256]={0};
//char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while( fgets(Buf,256,fp) )
{
Lid++;
if(Lid==L)
strcpy(tmp[Lid++],str);
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
for(Lid=1;Lid<=MaxLine;Lid++)
fputs(tmp[Lid],fp);
fclose(fp);
}
void cat(char *filepath)
{
int nl = 0;
FILE *stream;
char s[256];
char *p = s;
stream = fopen(filepath, "r+");
while((p = fgets(s, 256, stream)) != NULL)
{
nl++;
printf("Line %d: %s", nl, s);
}
fclose(stream);
}
void deleteLine(char* FileName,int lineno)
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[20][256]={0};
char *p = Buf;
// char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while((p=fgets(Buf,256,fp))!=NULL)
{
Lid++;
if(Lid==lineno)
{
if((p=fgets(Buf,256,fp))!=NULL) // 原文多了个分号
{
strcpy(tmp[Lid],Buf);
}
}
else
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
fclose(fp);
remove(FileName); // 删除原文件
if( (fp=fopen(FileName,"w"))==NULL)
{
printf("Can't open file!\n");
return;
}
for(Lid=1;Lid<=MaxLine;Lid++) // 多写一行
fputs(tmp[Lid],fp);
fclose(fp);
}
int main()
{
cat("c:\\a.txt");
printf("\n......................................................\n");
InsertLine("c:\\a.txt",3,"7777\n");
cat("c:\\a.txt");
printf("\n......................................................\n");
deleteLine("c:\\a.txt",3);
printf("\n......................................................\n");
cat("c:\\a.txt");
return 0;
}
#include <stdio.h>
#include <string.h>
void InsertLine(char* FileName,int L,char str[256])
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[50][256]={0};
//char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while( fgets(Buf,256,fp) )
{
Lid++;
if(Lid==L)
strcpy(tmp[Lid++],str);
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
for(Lid=1;Lid<=MaxLine;Lid++)
fputs(tmp[Lid],fp);
fclose(fp);
}
void cat(char *filepath)
{
int nl = 0;
FILE *stream;
char s[256];
char *p = s;
stream = fopen(filepath, "r+");
while((p = fgets(s, 256, stream)) != NULL)
{
nl++;
printf("Line %d: %s", nl, s);
}
fclose(stream);
}
void deleteLine(char* FileName,int lineno)
{
int Lid=0;
int MaxLine=0;
FILE* fp=NULL;
char Buf[256]="";
char tmp[20][256]={0};
char *p = Buf;
// char str[256]="Good China\n";
if( (fp=fopen(FileName,"r+"))==NULL)
{
printf("Can't open file!\n");
return;
}
while((p=fgets(Buf,256,fp))!=NULL)
{
Lid++;
if(Lid==lineno)
{
if((p=fgets(Buf,256,fp))!=NULL) // 原文多了个分号
{
strcpy(tmp[Lid],Buf);
}
}
else
strcpy(tmp[Lid],Buf);
}
MaxLine=Lid;
rewind(fp);
fclose(fp);
remove(FileName); // 删除原文件
if( (fp=fopen(FileName,"w"))==NULL)
{
printf("Can't open file!\n");
return;
}
for(Lid=1;Lid<=MaxLine;Lid++) // 多写一行
fputs(tmp[Lid],fp);
fclose(fp);
}
int main()
{
cat("c:\\a.txt");
printf("\n......................................................\n");
InsertLine("c:\\a.txt",3,"7777\n");
cat("c:\\a.txt");
printf("\n......................................................\n");
deleteLine("c:\\a.txt",3);
printf("\n......................................................\n");
cat("c:\\a.txt");
return 0;
}
#5
还是简单一点吧,全部读入内存,删那一行,根据内存内容重建文件。
=============================================================
对,就这样,up之
能不能更具体一点,搞不太明白。
=============================================================
对,就这样,up之
能不能更具体一点,搞不太明白。