c语言高手求助。怎么改txt文件中的特定内容。

图中的格式是用c语言写进去的(用户名,密码,姓名,性别,手机和邮箱),比如说我想改其中的第一行的邮箱,我该怎么写。
这个是修改手机号码的代码,有一个bug就是会误删其他的信息,有什么办法可以改进吗

/*刚写的,好累啊,已测试,希望能帮到你*/
#include <stdio.h>
#include <string.h>

void modify(FILE *x, FILE *y);

int main(void)
{
    FILE *file1 = NULL;
    FILE *file2 = NULL;
    char filename1[] = "测试.txt";  //这里换成你的txt文件;
    char filename2[] = "副本.txt";  //这里不要动;
    
    if((file1 = fopen(filename1,"r")) == NULL)  //只读方式打开;
    {
        printf("文件打开失败!\n");
        return 1; 
    }
    
    if((file2 = fopen(filename2,"w")) == NULL)  //只写方式打开;
    {
        printf("文件打开失败!\n");
        return 1; 
    }
    
    modify(file1,file2);        
    fclose(file1);
    fclose(file2);
    file1 = file2 = NULL;
    remove(filename1);
    rename(filename2,filename1);    
    return 0;
}

void modify(FILE *x, FILE *y)  //参数:两个已打开文件的指针,第一个必须能读,第二个必须能写;
{
    char temp[1024] = {0};
    char del[1024] = {0};
    char rep[1024] = {0};
    char *p1 = NULL;
    char *p2 = NULL;
    
    printf("请输入要替换的旧内容:");
    gets(del); 
    printf("请输入要替换的新内容:");
    gets(rep);
     
    while(fgets(temp,1024,x))
    {    
        p2 = temp;    
        
        while((p1 = strstr(p2,del)) != NULL)
        {                    
            for(int i = 0; &p2[i] != p1; ++i)
                fputc(p2[i],y);
                        
            for(int i = 0; i < strlen(rep); ++i)
                fputc(rep[i],y);
                
            p2 = p1 + strlen(del);                                                        
        }
        
        for(int i = 0; p2[i] != '\0'; ++i)
            fputc(p2[i],y);
                                    
    }
}

追问

幸苦你了,我是新手好多函数看不懂啊。

要不你帮我看看我的代码有什么办法可以改进

这个是修改手机号码的代码,有一个bug就是会误删其他用户的信息,有什么办法可以改进吗,谢谢你了

追答

修改需要全部代码,而且不要图片

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-03
读源文件 、打开另一文件,边读边写到要替换的行,就不写原文件的内容、写入修改后的内容 。。。。。。。完成后改文件名。。。。。。。。。。。。。
第2个回答  2016-09-03
全部读进来,放数组,然后内存中修改,删除文件,新建同名文件写回去。
相似回答