cat - C dup2 overwrite file bug when line > 1 -



cat - C dup2 overwrite file bug when line > 1 -

i have next simple programme catenates infile outfile

char *execargs[] = { "cat", null}; int outfile = open("outfile", o_wronly | o_creat, 0644); int infile = open("infile", o_rdonly, 0); dup2(outfile, stdout_fileno); dup2(infile, stdin_fileno); close(outfile); close(infile); execvp(execargs[0], execargs);

now, suppose content of infile is

this infile

and outfile is

this outfile

after running program, content of outfile has "e" @ end such

this infilee

also, if outfile instead

this outfile outfile

it becomes

this infilee outfile

what wrong?

what you're experiencing expected behavior. cat writes number of bytes reads, since original outfile longer, remaining bytes contain contained before.

if you're asking why different behavior using shell perform:

cat < infile > outfile

the reason shell opens outfile (or target of >) o_trunc, truncates file 0 length part of opening it. can same behavior adding | o_trunc open call's flags argument.

c cat dup

Comments

Popular posts from this blog

javascript - I need to update the text of a paragraph by inline edit -

javascript - THREE.js reposition vertices for RingGeometry -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -