-2

So I am trying to add text to the end of a line. Right now it adds it fine to the beginning but I can't figure out how to get it added to the end.

So the code here takes content from one file, and adds it to a temporary file, and than writes it back to the original file with the added text. Right now however its adding text to the beginning of the line, I need it at the end of each line.

Also I was wondering is there a way to just append text to the end of each line, without copying all contents to a temporary file, and just display the output to stdout?

int add_text_end(FILE *fileContents) 
{

    FILE *tmp = tmpfile();
    char *p;
    FILE *fp;
    char line[LINESIZE]; 

    if ((fileContents = fopen("fileContents.txt", "r")) == 0) 
    {
        perror("fopen");
        return 1; 
    }
    /* Puts contents of file into temp file */
    fp = fopen("fileContents.txt", "r");
    while((p = fgets(line, LINESIZE, fp)) != NULL)
    {
        fputs(line, tmp);
    }
    fclose(fp); 

    rewind(tmp);
    /* Reopen file to write to it */
    fopen("fileContents.txt", "w");
    while ((p = fgets(line, LINESIZE, tmp)) != NULL)
    {
        line[strlen(line)-1] = '\0';  /* Clears away  new line*/
        sprintf(line, "%s %s", line, "test");
        fputs(line, fp);
    }
    fclose(fp); 
    fclose(tmp);
    return 0;

}
nb023
  • 35
  • 1
  • 1
  • 6
  • 1
    You can't use sprintf that way. The buffer it writes to can't be the same as the one it reads from. Allocate a new buffer big enough to hold the sequence you want and free it when you're done. From the manpage: "If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined." – Matthew Jun 26 '15 at 23:30
  • @Matthew Okay so I created a new char buffer[LINESIZE]; and than changed my sprintf to sprintf(buffer, "%s %s", line, "test"); now it just prints the output from the original file and doesn't add "test" anywhere. It used to add "test" to the beginning of each line. – nb023 Jun 26 '15 at 23:49
  • did you change the `fputs` line to `fputs(buffer, fp)`? Make sure you also memset `buffer` to zero each time before you use it, as well. – Matthew Jun 26 '15 at 23:51
  • Actually, in this case it's better to use fprintf instead - see my answer. – Matthew Jun 26 '15 at 23:54
  • You need to read all of the contents of the file into the memory if temporary file does not use. – BLUEPIXY Jun 27 '15 at 00:56
  • @BLUEPIXY Sorry I don't understand what you mean – nb023 Jun 27 '15 at 01:41
  • Open file with "a+" mode instead of "r" – udit043 Jun 27 '15 at 04:49

1 Answers1

0

There's a better way to do this, without using sprintf in the wrong way that causes undefined behavior (you can't have the destination buffer be the same buffer that sprintf reads from as an argument) - change the while block to the following:

while ((p = fgets(line, LINESIZE, tmp)) != NULL)
{
        line[strlen(line)-1] = '\0';  /* Clears away  new line*/
        fprintf(fp, "%s %s\n", line, "test");
}

If the file has \r\n endings, then use this instead:

while ((p = fgets(line, LINESIZE, tmp)) != NULL)
{
        char *r;
        if ((r = strchr(line, '\r')) != NULL)
            *r = '\0';  /* Clears carriage return */
        fprintf(fp, "%s %s\r\n", line, "test");
}

Make sure to #include <string.h> if you get a warning about strchr() not being declared.

Matthew
  • 563
  • 4
  • 13
  • I just changed it to this and now it copies every line twice, and between every copied line it prints test.. Before it was printing "test" at the beginning of each line, I just wanted to print "test" at the end of each line. – nb023 Jun 27 '15 at 00:00
  • What file are you feeding into it? Does the file have '\r\n' endings? – Matthew Jun 27 '15 at 00:08
  • I'm not sure if it has \r\n endings. I am using cygwin on a windows machine. Heres my code `fprintf(fp, "%s %s\n", line, "test");` `fputs(line, fp);` – nb023 Jun 27 '15 at 00:29
  • 1
    @nb023 Remove the fputs line. – Matthew Jun 27 '15 at 00:33
  • Okay I did. It gets rid of the duplicate entries but it is still printing "test" at the beginning of each line, instead of at the end of each line. – nb023 Jun 27 '15 at 01:44
  • @nb023 - See the final edit. That should fix it. Use the second block since you're on Windows and it uses '\r\n' endings for new lines. – Matthew Jun 27 '15 at 14:24