如何在C中读取和覆盖文本文件?

时间:2015-07-02 14:58:51

标签: c file pointers fgets overwrite

我有一个文本文件text.txt,可以读取(为简单起见)

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

为了简单起见,我只想将每行中的第一个字符设置为' x',所以我想要的结果是

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

所以我打开text.txt文件并尝试用所需的输出覆盖每一行到同一文本文件。在while循环中,我将每行中的第一个字符设置为' x'。我还设置变量" line"等于一,因为如果它在第一行,我想倒回到文件的开头,以便在开始时而不是在文件的末尾覆盖。然后增加行,以便在下一次迭代时跳过倒带,并且应该继续覆盖第2行和第3行。它适用于第一行。

有人有任何解决方案吗?顺便说一句,我已经在stackoverflow和其他网站上进行了广泛的研究,但没有运气。这是我的代码,我的输出也在下面:

#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);
}

输出:

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

5 个答案:

答案 0 :(得分:6)

// Automatically load required Grunt tasks
require('jit-grunt')(grunt, {
  useminPrepare: 'grunt-usemin',
  ngtemplates: 'grunt-angular-templates',
  cdnify: 'grunt-google-cdn'
});

答案 1 :(得分:2)

我总是建议使用另一个文件做这个kindda解决方案。

  1. 阅读
  2. 将x放入一行中的新文件中,然后复制该行的其余部分。
  3. 这样做直到你得到EOF
  4. 删除旧文件
  5. 重命名此新文件

答案 2 :(得分:0)

试试这个

{{1}}

我在Windows上解决了这个问题

答案 3 :(得分:0)

// file_overwrite.cpp : main project file.
// File opens and write y value to a file
// again reads same file and re-writes y value to a file

#include "stdafx.h"

using namespace System;

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

int main(int argc, char *argv[])
{
    int x = 19530;

    FILE *fp1 = fopen("D:\\Data\\BUFF.txt","w+");

    if(fp1 == NULL)
        printf("File not opening \n");
        int y=x;
        fprintf(fp1, "%d \n", y);
        fclose(fp1);
        printf("\n file -> open -> write y value and close");


        freopen("D:\\Data\\BUFF.txt", "w", fp1);
        rewind(fp1);
        y=100;
        fprintf(fp1, "%d \n", y);
        printf("\n file -> Reopen -> rewind write y values and close");
        fclose(fp1);

       getch();
       return 0;
}

答案 4 :(得分:0)

// overwrite_file.cpp 
// File opens and write y value to a file
// again reads same file and re-writes y value to a file

#include "stdafx.h"

using namespace System;

#include<stdio.h>
#include<stdio.h>
#include<string.h>                //Include appropriate headers
#include <conio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
    int x = 19530;                            // Give any value in the limit

    FILE *fp1 = fopen("D:\\Data\\BUFF.txt","w+");   // open file to write

    if(fp1 == NULL)                                 // if the file pointer encounters a null, it may not open neither overwrite 
        printf("File not opening \n");
        int y=x;                                     
        fprintf(fp1, "%d \n", y);                   //print y
        fclose(fp1);
        printf("\n file -> open -> write y value and close");  // close the file after writing the value of y


        freopen("D:\\Data\\BUFF.txt", "w", fp1);            //reopen and rewind file 
        rewind(fp1);
        y=100;                                              // this value of y given within the limits gets printed on the .exe console
        fprintf(fp1, "%d \n", y);
        printf("\n file -> Reopen -> rewind write y values and close");    // rewind write values and close 
        fclose(fp1);

       getch();
       return 0;
}
相关问题