-1

Text File:

line_1: abcdefg

override the first k chars to ttttt so that the new text file is:

line_1: tttttfg

I used fprintf and when opening the .txt file I used w+ flag, however it will erase the whole .txt file and not just override the first bytes_number characters.

I tried many things that didn't seem to wok. Any hints would be thankful! Thanks in advance!

void my_write (char* path, int bytes_number, char* flag, char* data, int sockfd)
{
    FILE* fp;
    char* test;
    int n, i;
    char buffer[BUFFER_SIZE];
    if (bytes_number > 1000 || bytes_number < 0)
    {
        write (sockfd, "Failure", strlen("Failure"));
        return;
    }
    test = data;
    if (strlen(test) < bytes_number)
    {
        write (sockfd, "Failure", strlen("Failure"));
        return;
    }
    if (!strcmp(flag, "override"))
    {
        fp = fopen(path, "w+"); /* Open file with flag 'w' so that we overrun the data */
        if (fp == NULL) { /* File doesn't exist, invalid path*/
            write (sockfd, "Failure", strlen("Failure"));
            return;
        }
        **i = fprintf (fp, "%.*s\n", bytes_number, data);**
        if (i < 0)
            write (sockfd, "Failure", strlen("Failure"));
        else
            write (sockfd, "Write Success", strlen("Write Success"));
    }
    else if (!strcmp(flag, "append"))
    {
        fp = fopen(path, "ab"); /* Open file with flag 'ab' so that we don't override data while writing */
        if (fp == NULL) /* File doesn't exist, invalid path*/
            write (sockfd, "Failure", strlen("Failure"));
            i = fprintf (fp, "%.*s\n", bytes_number, data);
        if (i < 0)
            write (sockfd, "Failure", strlen("Failure"));
        else
            write (sockfd, "Write Success", strlen("Write Success"));
    }
    else {
        write (sockfd, "Failure", strlen("Failure"));
    }
    fclose(fp);
}
fluter
  • 13,238
  • 8
  • 62
  • 100
Tony Tannous
  • 463
  • 1
  • 10
  • 23
  • Possible duplicate of [How to read and overwrite text file in C?](http://stackoverflow.com/questions/31188053/how-to-read-and-overwrite-text-file-in-c) – Simon May 07 '16 at 00:37
  • @Simon in my case the number of characters is unknown, the other lad only wants to change the first char of each line. – Tony Tannous May 07 '16 at 00:41

1 Answers1

2

fopen(path,"w+") opens a file for write and update, deleting the file first if it already exists. To avoid deleting the file first, one can use fopen(path,"r+") to open it for reading and update.

If the aim is just to replace the specified characters, then the \n at the end of the fprintf format string should be omitted, so that the line reads i = fprintf (fp, "%.*s", bytes_number, data).

Simon
  • 10,679
  • 1
  • 30
  • 44
  • I tried "r+" flag, it does overrite the first K characters, however it then deletes 2 characters and the rest are in a seperated line. Any idea how to fix that ? – Tony Tannous May 07 '16 at 01:08
  • @TonyTannous: I think you'll find that the problem is the newline character in the `fprintf` format, and I expect you are using a Windows computer where the newline character is automatically expanded by C into two characters - carriage return followed by a newline - thus two extra characters are deleted and the following text continues on the next line of the text file. – Simon May 07 '16 at 01:13
  • Most probably, I'm on windows 7, however the file is compiled and run on a VS using VMware, and the output.txt is on my laptop (which is run on windows7). I am using google to find a way to omit the \n. Thank you! – Tony Tannous May 07 '16 at 01:18
  • @TonyTannous: As far as I can see, you should just be able to take the "\n" out of the string. Unless I'm missing something, I don't see why you would need it. – Simon May 07 '16 at 01:26