2015-07-02 29 views
6

我有一个文本文件的text.txt读取(为简单起见)如何读取和覆盖C文本文件?

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'。我还将变量“行”设置为1,因为如果它位于第一行,我想倒回到文件的开头,以便在文件的开头而不是文件末尾进行覆盖。线然后增加,因此它将跳过下一次迭代的倒带,并应继续覆盖第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 
+1

您在阅读文件时无法覆盖该文件。那么,你可以,但你会得到乱码的数据。你的操作系统是什么?如果是Linux/Unix,只需在打开文件后删除文件 - “unlink(”text.txt“);' - 然后打开一个具有相同名称的**新文件,并将修改的行写入新文件。你将拥有**两个** FILE *'变量。 –

+0

它必须位于行的开头。 – BLUEPIXY

+0

就地重写文件是非常棘手的。一个鲜为人知的重要事实是,当你读到你想开始写作的确切位置时,你必须在开始写作之前调用'fseek(fp,0,SEEK_CUR)'。然后再写完之后再开始阅读。 –

回答

6
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 
} 
+1

@NattachaiSuteerapongpan请参阅** 7.21.5.3 [n1570.pdf]的fopen函数p7 **(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – BLUEPIXY

+0

I知道你的评论是在2年前回复的,但我刚刚阅读了你的答案,而我对“fflush()”并不十分清楚。你的代码工作非常好,但我试过删除“fflush(fp)”并运行代码。该程序陷入无限循环。由于我打破了这个程序,它产生了非常奇怪的结果。我真的不知道为什么结果是这样的。你能帮我解释一下为什么要删除fflush产生这样的结果。 这里是文本文件的链接(执行程序之前和之后)。 https://drive.google.com/drive/folders/0B3cRfiRmMzw6Q01fZ09uX0VOU2c?usp=sharing –

+1

@NattachaiSuteerapongpan有必要调用前面评论中显示的链接中所写的'fflush'。如果它不存在,它并不总是正常工作。简单地说,它是缓冲的,所以它需要反映在实际的文件中。 – BLUEPIXY

2

我总是建议使用其他文件做到这一点kindda解决方案。

  1. 阅读线
  2. 将X个在生产线中的新文件,并复制该行的其余部分。
  3. 做到这一点,直到你得到EOF
  4. 删除旧文件
  5. 重新命名这个新的文件
0

尝试了这一点

#include<stdio.h> 
#include<stdio.h> 
#include<string.h> 
int main() 
{ 
    char buffer[500],read[50][50]; 
    FILE *fp=fopen("text.txt","r+"); 
    int line =1; 
    while(fgets(buffer,500,fp)!=NULL){ 
     buffer[0]='x'; 
     printf("\n%d ",line); 
     puts(buffer); 
     strcat(read[line-1],(const char*)buffer); 
     line++; 
    } 
    fclose(fp); 
    FILE *fp1=fopen("text.txt","w"); 
    rewind(fp1); 
    fprintf(fp1,"%s",read); 
    return 0; 
    } 

我工作了这一点,在Windows

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; 
} 
+0

虽然看起来很明显,但是在回答问题时,请考虑使用代码注释或文档参考,以便人们获得更多背景信息。 – Lepidopteron

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