C语言 文件读写到同一个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5787867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:29:39  来源:igfitidea点击:

File read write to same file?

cfileio

提问by DaRk_f0x

I've managed to open a file and read while writing to another file with var=fopen(file,"r")/ "w"but even with r+ or w+ moded I can't open a file and alter its contents.

我设法打开一个文件并在使用var=fopen(file,"r")/写入另一个文件时读取,"w"但即使使用 r+ 或 w+ 模式,我也无法打开文件并更改其内容。

Imagine this:

想象一下:

int formatacao (char original[]){/*se a cadeia nao tiver escrita em maiusculas, esta fun?ao vai alteralas para tal*/
    int val1;
    FILE * original_open;

    original_open = fopen (original,"r+");

    if (original_open==0){
       printf ("ficheiro %c 1.",original);
    }


    while ((val1=fgetc(original_open))!=EOF){
          if (val1>='a'&&val1<='z'&&val1){
             fputc(val1-32,original_open);
          }
          else 
          fputc(val1,original_open);
    }

    fclose (original_open);
    return (0);
}

Code works, no errors, no warning, only problem is: it erases the contents on the file if I use it like this BUT this works:

代码有效,没有错误,没有警告,唯一的问题是:如果我像这样使用它,它会擦除​​文件中的内容,但这是有效的:

int main (){
    int val1,val2,nr=0;
    FILE* fp1;
    FILE* fp2;
    fp1=fopen ("DNAexample.txt","r");
    fp2=fopen ("DNAexample1.txt","w");
    if (fp1==0){
       printf ("EPIC FAIL no 1.\n");
    }
    while ((val1=fgetc(fp1))!=EOF){
          if (val1>='a'&&val1<='z'&&val1){
             fputc(val1-32,fp2);
          }
          else 
          fputc(val1,fp2);
    }


    fclose (fp1);
    fclose (fp2);
    return (0);
}

Flawlessly! How can I open a file, read char by char and decide if I want to change the char or not?

完美无瑕!如何打开文件,逐个字符读取字符并决定是否要更改字符?

回答by pmg

You need to intervene a file positioning function between output and input unless EOF was found on input.

除非在输入上找到 EOF,否则您需要在输出和输入之间插入文件定位功能。

This works for me:

这对我有用:

#include <stdio.h>

int formatacao (char *original) {
  int val1;
  FILE *original_open;
  int write_at, read_at;

  original_open = fopen(original, "r+");
  if (original_open == 0) {
    printf("ficheiro %s\n", original);
  }
  write_at = read_at = 0;
  while ((val1 = fgetc(original_open)) != EOF) {
    read_at = ftell(original_open);
    fseek(original_open, write_at, SEEK_SET);
    if (('a' <= val1) && (val1 <= 'z')) {
      fputc(val1 - 32, original_open);
    } else {
      fputc(val1, original_open);
    }
    write_at = ftell(original_open);
    fseek(original_open, read_at, SEEK_SET);
  }
  fclose(original_open);
  return (0);
}

int main(void) {
  formatacao("5787867.txt");
  return 0;
}

回答by anthares

Open the file with 'a+' option, in order to append to the file:

使用 'a+' 选项打开文件,以便附加到文件:

fopen ("DNAexample.txt","a+");

w+ will erase your file if exists or create new if the specified doesn't exist. a+ will open the existing file and you will be able to edit it.

w+ 将删除您的文件(如果存在)或创建新文件(如果指定的文件不存在)。a+ 将打开现有文件,您将能够对其进行编辑。

You can read more about file operations here: http://www.functionx.com/cppbcb/cfileprocessing.htm

您可以在此处阅读有关文件操作的更多信息:http: //www.functionx.com/cppbcb/cfileprocessing.htm

回答by AbhiRoczz...

Open file in append mode

以追加模式打开文件

original_open = fopen (original,"a+");