ElRStar
ElRStar

Reputation: 333

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. For instance, file.txt is

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).

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!

Upvotes: 0

Views: 5184

Answers (3)

chux
chux

Reputation: 153348

Simply read each char, one at a time and print the suffix when the end-of-line detected. 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");

Upvotes: 2

Weather Vane
Weather Vane

Reputation: 34584

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.

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

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

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()]
  • define a string with the constant input value that you want to add after each line. [char * constvalue = "-1";]
  • Read a line from the input file. [fgets()##]
  • use fprintf() to write the data read from the input file and the constant value, together. Some pseudocode may look like

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

  • loop untill there is value in the input file [while (fgets(infile....) != NULL)]
  • close both the files. [fclose()]

## -> fgets() reads and stores the trailing newline \n to the supplied buffer. You may want to remove that.

Upvotes: 1

Related Questions