Can anyone tell me what's wrong with my program below?

I'm creating a program that writes every character(byte) from a buffer to a FILE until the number of bytes written is equal to the size of the buffer.

char *next;
int character;
int byteswritten = 0;

next = buf; //buf is the buffer that holds the string to be written on a FILE
while (byteswritten < payload) { //payload is the size of the buffer
           character = fputc(*next, fp); //fp is the FILE where the contents of buf will be written to
           byteswritten++;
           next++;
}

The problem of my program above is when *next encounters a NULL in the middle of the buffer, everything else written on the FILE is NULL but in reality non-NULL characters still follow the NULL character. How could I fix this? Big big thanks to anyone who could answer my question.

why are you doing that one character at a time? You can write the entire buffer all in one function call using fwrite(). You will want to open the file in binary instead of text mode. bytewritten = fwrite(buf,1,payload,fp);

Edited 5 Years Ago by Ancient Dragon: n/a

why are you doing that one character at a time? You can write the entire buffer all in one function call using fwrite(). You will want to open the file in binary instead of text mode. bytewritten = fwrite(buf,1,payload,fp);

It's because I'm not really writing everything contained in the buffer. For example, if the buffer contains 100 characters like: buf[100] = "100 characters here", I want to start writing starting from the 10th character up to the 100th, so I'll have to go to the 10th character first before I start writing. Something like that. So I thought I should use fputc rather than fwrite. Or do you know of other ways to write a certain range of bytes instead of 1 character at a time? Thank you.

start = 10;    // start writing at byte #11
bytewritten = fwrite(&buf[start],1,payload-start,fp);

&buf[start] is the address of the 11th byte

start = 10;    // start writing at byte #11
bytewritten = fwrite(&buf[start],1,payload-start,fp);

&buf[start] is the address of the 11th byte

What if I don't know exactly where in buf I will start writing? For example if for the first time buf[] = "This is my test string.", and for the second time buf[] = "This again is my test string." What if I want to start writing to FILE after I encounter the word "my" in the buffer? As can be seen in the 2 values of buf[], the loacation of "my" is different from the other so I can't just assign a number where I should start. My program looks like this:

char *next;
       for(next = buf; *next != (char)NULL; next++) {
          if(strncmp(next, "my", 2) == 0) {
             next++;
             next++;
             
             fwrite(&buf[int (next)], 1, payload-start, fp);
          }
       }
start = 10;    // start writing at byte #11
bytewritten = fwrite(&buf[start],1,payload-start,fp);

&buf[start] is the address of the 11th byte

Thanks for this reply. I was able to fix my problem.

This article has been dead for over six months. Start a new discussion instead.