如何阅读并覆盖文本文件用C? [英] How to read and overwrite text file in C?

查看:210
本文介绍了如何阅读并覆盖文本文件用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件的text.txt读取(为了简单起见)

 这是一行
这是第二行
这是三线

为简单起见再次,我只是想设置的第一个字符中的每一行以'X',所以我期望的结果将是

  xhis是一行
xhis是线两条
xhis是三线

所以我开的text.txt文件,并试图覆盖每一个符合期望的输出相同的文本文件。在while循环,我在每一行的x设置的第一个字符。我还设置变量线等于一,因为如果其上的第一行,我要倒带至文件的开头,以便在开始时,而不是在该文件的末尾覆盖。然后线路将增加,因此将跳过退为下一次迭代,并应继续覆盖第二和第三线。它完美的第一道防线。

任何人有任何解决方案?顺便说一句,我研究这个广泛无论在计算器​​和其他网站,并没有运气。这里是我的code和我的输出也低于:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#定义MAX 500诠释主(){
    字符*缓冲=的malloc(sizeof的(char)的* MAX);
    FILE *计划生育=的fopen(的text.txt,R +);
    INT线= 1;
    而(与fgets(缓冲液,500,FP)!= NULL){
            缓冲器[0] ='×';
            如果(线== 1){
                    倒带(FP);
                    fprintf中(FP,%S,缓冲区);
            }
            其他{
                    fprintf中(FP,%S,缓冲区);
            }
            行++;
    }
    免费(缓冲);
    FCLOSE(FP);
}

输出:

  xhis是一行
这是第二行
xhis是线两条
Ë
X


解决方案

 长POS = FTELL(FP); //保存当前位置
而(与fgets(缓冲液,500,FP)!= NULL){
    缓冲器[0] ='×';
    fseek的(FP,POS,SEEK_SET); //移动到行首
    fprintf中(FP,%S,缓冲区);
    fflush(FP);
    POS = FTELL(FP); //保存当前位置
}

I have a text file text.txt that reads (for simplicity purposes)

this is line one
this is line two
this is line three

Again for simplicity's sake, I am just trying to set the first character in each line to 'x', so my desired result would be

xhis is line one
xhis is line two
xhis is line three

So I am opening the text.txt file and trying to overwrite each line with the desired output to the same text file. In the while loop, I set the first character in each line to 'x'. I also set the variable "line" equal to one, because if its on the first line, I want to rewind to the beginning of the file in order to overwrite at the start instead of at the end of the file. Line is then incremented so it will skip the rewind for the next iteration, and should continue to overwrite the 2nd and 3rd lines. It works perfectly for the first line.

Anybody have any solutions? BTW, I have researched this extensively both on stackoverflow and other sites, and no luck. Here's my code and my output is also below:

#include <stdio.h>
#include <stdlib.h>
#define MAX 500

int main() {
    char *buffer = malloc(sizeof(char) * MAX);
    FILE *fp = fopen("text.txt", "r+");
    int line = 1;
    while (fgets(buffer, 500, fp) != NULL) {
            buffer[0] = 'x';
            if (line == 1) {
                    rewind(fp);
                    fprintf(fp, "%s", buffer);
            }
            else {
                    fprintf(fp, "%s", buffer);
            }
            line++;
    }
    free(buffer);
    fclose(fp);
}

Output:

xhis is line one
this is line two
xhis is line two
e
x

解决方案

long pos = ftell(fp);//Save the current position
while (fgets(buffer, 500, fp) != NULL) {
    buffer[0] = 'x';
    fseek(fp, pos, SEEK_SET);//move to beginning of line
    fprintf(fp, "%s", buffer);
    fflush(fp);
    pos = ftell(fp);//Save the current position
}

这篇关于如何阅读并覆盖文本文件用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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