Hey, guys. I am writing single program, which should replace 10th,11th and 12th character in file with "A" . Problem is, that my program will just write thos A's at the end of the file. Could you,please help me? Thanks in advance.

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

int main(int argc, char **argv)
{
    int des,l,k;
    char buf;

    des = open("tmp",O_CREAT|O_APPEND|O_RDWR,S_IRUSR|S_IWUSR);
    if (l = lseek(des,10L,SEEK_SET)== -1)
        {
            perror("lseek()");
            return (des);
        }                   //get to the position 10
    else
    {   
        for (k=0;k<3;k++)
            {
                write(des,"A",1);
            }
    }                       //write AAA
    read(des,&buf,1);
    printf("%c",buf);       //checking
    return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.