简体   繁体   English

将相同的文本添加到C中文件的每一行的末尾

[英]Add same text to end of each line in file from C

I am trying to add a -1 to the end of each line of a file. 我正在尝试在文件的每一行末尾添加-1。 For instance, file.txt is 例如,file.txt是

1 4 5
2 5 9
3 5 6

but would become 但会变成

1 4 5 -1
2 5 9 -1
3 5 6 -1

I am figuring out how to add text in general to a file from C, but I cannot figure out how to add the same text to each line in the file, and assure that the new line character is placed after the new last character in the lines (in this case -1). 我正在弄清楚如何从C向文件中添加文本,但是我无法弄清楚如何向文件中的每一行添加相同的文本,并确保将换行符放置在C中新的最后一个字符之后。行(在这种情况下为-1)。

Here is what I have tried: 这是我尝试过的:

FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
    printf("Error opening file!\n");
    exit(1);
}

/* print some text */
const char *text = " -1";
fprintf(f, "%s\n", text);

Any advice greatly appreciated! 任何建议,不胜感激!

Simply read each char , one at a time and print the suffix when the end-of-line detected. 只需一次读取每个char ,并在检测到行尾时打印后缀。 Then print the character read. 然后打印读取的字符。

void Append(FILE *inf, FILE *outf, const char *suffix) {
  int ch;
  for (;;) {
    int ch = fgetc(inf);
    if (ch == '\n' || ch == EOF) {
      fputs(suffix, outf);
      if (ch == EOF) {
        break; 
      }
    }
    fputc(ch, outf);
  }
}

// Error checking omitted
char tmp[L_tmpnam];
tmpnam(tmp);
FILE *inf = fopen("file.txt", "r");
FILE *outf = fopen(tmp, "w");
Append(inf, outf, " -1");
fclose(inf);
fclose(outf);
remove("file.txt");
rename(tmp, "file.txt");

If you agree to use two seperate files for input and output, your job will be very easy. 如果您同意使用两个单独的文件进行输入和输出,您的工作将非常容易。 The algorithm to achieve what you want can be designed like below 可以如下设计实现所需目标的算法

  • Open the input file, open the output file. 打开输入文件,打开输出文件。 [ fopen() ] [ fopen() ]
  • define a string with the constant input value that you want to add after each line. 用您要在每行之后添加的常量输入值定义一个字符串。 [ char * constvalue = "-1"; [ char * constvalue = "-1"; ] ]
  • Read a line from the input file. 从输入文件中读取一行。 [ fgets() ## ] [ fgets() ## ]
  • use fprintf() to write the data read from the input file and the constant value, together. 使用fprintf()一起写入从输入文件读取的数据和常量值。 Some pseudocode may look like 一些伪代码可能看起来像

    fprintf(outfile, "%s %s", readdata, constvalue);

  • loop untill there is value in the input file [ while (fgets(infile....) != NULL) ] 循环直到输入文件中有值[ while (fgets(infile....) != NULL) ]
  • close both the files. 关闭两个文件。 [ fclose() ] [ fclose() ]

## -> fgets() reads and stores the trailing newline \\n to the supplied buffer. ##-> fgets()读取尾随换行符\\n并将其存储到提供的缓冲区中。 You may want to remove that. 您可能要删除它。

I can add -1 to each line using a text editor, by replacing "\\r\\n" with " -1\\r\\n" or similar depending on the file's eol format. 我可以使用文本编辑器在每行中添加-1 ,方法是将"\\r\\n"替换为" -1\\r\\n"或类似文件,具体取决于文件的eol格式。

Or programmatically, create a new file like this: 或以编程方式创建一个新文件,如下所示:

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

int main()
{
    FILE *fr, *fw;
    char buffer[10000];
    fr = fopen("file.txt","rt");
    if (fr == NULL) {
        printf("Error opening input file\n");
        return 1;
    }
    fw = fopen("file1.txt","wt");
    if (fw==NULL) {
        printf("Error opening output file\n");
        fclose (fr);
        return 1;
    }
    while (fgets(buffer, 10000, fr) != NULL) {
        buffer [ strcspn(buffer, "\r\n") ] = 0;   // remove trailing newline etc
        fprintf(fw, "%s -1\n", buffer);
    }
    fclose(fw);
    fclose(fr);
    return 0;
}

Output file: 输出文件:

1 4 5 -1
2 5 9 -1
3 5 6 -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM