3

How would I go about printing contents of a file that I've appended to using only low-level I/O functions?

The closest I get is printing the text that I'm using to append Example:

file1.txt = dog
file2.txt = cat

I want file2.txt, which is now "catdog" to be printed out. How would I do that?

As said before I can only get "dog" to print. I'm also successful in appended the file. I know it's probably really simple solution but I been scratching my head for hours.

My code

while (1) {
        if ((bufchar = read(fdin1, buf, sizeof(buf))) > 0) {
                bp = buf;   // Pointer to next byte to write.
                while (bufchar > 0) {
                        if ((wrchar = write(fdin2, bp, bufchar)) < 0)
                                perror("Write failed");
                        bufchar -= wrchar;   // Update.
                        bp += wrchar;
                }
        }
        else if (bufchar == 0) {  // EOF reached.
                break;
        }
        else
                perror("Read failed");
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • In its current form, it's hard to tell what the question is about. What exactly is the issue? – owacoder Oct 22 '15 at 02:39
  • i guess you better forget the term low level I/O. and post a simple high level code, and ask for converting it to low level. this way we can get an idea for what you want. – milevyo Oct 22 '15 at 02:44
  • Sorry I posted my code. Basically I want file2 appended and the whole file printed on the console – user3038871 Oct 22 '15 at 02:47
  • It is probably relevant to show the `open()` calls, since there are a lot of flags that can be used and can affect the result. `O_TRUNC` and `O_APPEND` are two of the important flags, as is `O_CREAT`. Without seeing how you are manipulating that, and how you are rewinding or reopening `file2.txt` to print to standard output, it is difficult to guess where the trouble is. Please read up on how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)). With that available, it is likely to be straight-forward to resolve your problems. – Jonathan Leffler Oct 22 '15 at 04:48

1 Answers1

0

Just some heads up, that if you are appending to file2.txt it would then be "catdog" not the other way around. If you are only able to get dog to write out, physically go into the file to ensure you are actually appending and not simply overwriting the file.

Here is some reading for the specifics of low-level file I/O. Read the top two links for opening and closing files and primitive I/O operations. Without seeing any of your code it is hard to help you though it is possible you are not properly closing the file and so your appended line is not saved...

optomus
  • 66
  • 5