有没有一种方法一旦被读取,删除从文本文件的第一个字符? [英] Is there a way to delete the first character from a text file once it is read?

查看:187
本文介绍了有没有一种方法一旦被读取,删除从文本文件的第一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法删除的第一个字符在文本文件中,一旦你看了吗?
我希望程序从文本文件中读取字符,一旦使用就应该删除从文本文件读取当前字符。

Is there any way to delete the first character in a text file once you read it? I want the program to read the character in from the text file and once it is used it should delete that current character that was read from the text file.

推荐答案

您不能删除文件的字符,你可以对其进行修改和逻辑上说,数据已被删除。

you can't delete characters from file,you can modify them and logically says that data was deleted.

我们无法删除文件内容。而不是删除,您可以在您想要的文件删除字符的位置添加*(或者说是不存在的文件的任何东西)。

we can't remove file content. instead of deleting you can add * (or any thing that is not there in your file) at the position where you want to delete char in file.

更多,在删除后也有很多*在你的文件。只是复制,除了*是你的整个文件在文件中的新文件

after many more deletions there are lot * 's in your file. just copy your entire file except *'s in your file to new file

删除旧文件
重命名新文件到旧文件

remove old file rename new file to old file

您刚才复制的部分之后,你删除了你想删除的部分数据。

you just copy the data after the part you delete over the part you want deleted..

请参阅此code:

#include <stdio.h>
#include <stdlib.h>

int main(){

FILE *fp = fopen("file.txt","r+"); /* Open for reading  and writing */
FILE *fp1=fopen("new.txt","a+");
FILE *fp2=fopen("new2.txt","a+");

char ch;
int i=0,n;

printf("Enter how many characters do you want to delete from file\n ");
scanf("%d",&n);
while(((ch = fgetc(fp)) != EOF )&&( i<n))
{
fseek(fp,-1,SEEK_CUR);
fputc('*',fp);
i++;
}

printf("file after delete\n");
rewind(fp);
while((ch = fgetc(fp)) != EOF )
{
if(ch!='*')
printf("%c",ch);
}

printf("copy to another file after delete\n");
rewind(fp);
while((ch = fgetc(fp)) != EOF )
{
if(ch!='*')
fputc(ch,fp1);
}


printf("file after delete\n");
rewind(fp);
while((ch = fgetc(fp)) != EOF )
{
if(ch!='*')
printf("%c",ch);
}


printf("delete first n characters from new.txt delete\n");

printf("Enter how many characters do you want to delete from file\n ");
scanf("%d",&n);
i=0;
rewind(fp1);
while((ch = fgetc(fp1)) != EOF )
{
if(i>n)
fputc("%c",ch);
i++;
}



printf("\n\n");
fclose(fp);
fclose(fp1);
fclose(fp2);
//use rename() and remove() functions to delete old file and then rename new file as old one.
return 0;
}

这篇关于有没有一种方法一旦被读取,删除从文本文件的第一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆