Thread: Appending txt file above last line?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    26

    Appending txt file above last line?

    I create file like this: (I need it shared...)

    Code:
    if ( (file = CreateFile(file_name.c_str(), GENERIC_WRITE	, FILE_SHARE_WRITE	,NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL)) == INVALID_HANDLE_VALUE ) 	return;
    And then, write to it:

    Code:
    WriteFile(file,buffer1,strlen(buffer1),&NOfBytes,&ovlp);
    This way, if file doesn't exist it will be created, and if it does exist it will be updated.

    My question is, how to append new text ABOVE last line in file?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you have a text file with
    123
    ABC
    ###

    And you want to add 678 above the ### line, is that what you are saying, roughly?

    The generic way to do that is to read some data from the end of the file, until you find a newline [besides the newline at the very end of the file, if there is one there]. You then need to COPY that data you read, and write the new data, followed by the copied data [if you wish, you can make this into a single write].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    26
    I am not sure I understand.
    until you find a newline
    , what do you mean by this?
    123
    ABC
    ###

    Between ABC and ###?
    You then need to COPY that data you read
    What data? The last line (###) or all other data (123,ABC)?
    If I wite this data to file I'll get copies of same data.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I was just trying to examplify what you want, so we start with:
    123
    ABC
    ###
    .
    Adding 678 would produce the file:
    123
    ABC
    678
    ###

    And, no if you read from the back, the data you read in to find the last (or second to last) newline is the ### part. So you start by setting the file position to the end of the file, then going backwards until you find the newline (that is after the C in "ABC\n"). When "walking backwards, you would obviously pass the three # characters [and likely, but not necessarily, a newline at the very end of the file]. You need to write those out again, to make sure you don't loose anything when you insert the 678 text.

    My example uses fixed length lines, but that's juse because I'm too lazy to write long lines.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To expand on mats's example. Here's how the data is actually stored in the file.

    123\n
    ABC\n
    ###

    So to find the line before the last line, you would position the file position to the end of the file, then keep reading "backwards" until you find that "\n".
    And then you need to copy that last line because if you write something, it will just overwrite it, like this:

    123\n
    ABC\n
    My new text

    So if you want to avoid that, you copy the last line, and write your new text like this:

    123\n
    ABC\n
    My new text

    Than add "\n" (newline):

    123\n
    ABC\n
    My new text\n

    Then write the data you copied, so it becomes:

    123\n
    ABC\n
    My new text\n
    ###
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    26
    I see. Now some code questions:
    As I wrote I use WriteFile function.

    How do I set new position to write from (I mean how to write backwards to wanted position and then write from this position)?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You mean read
    It can be done via SetFilePointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    26
    And after I get the position, how do I write to file from this position?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by markiz View Post
    I see. Now some code questions:
    As I wrote I use WriteFile function.

    How do I set new position to write from (I mean how to write backwards to wanted position and then write from this position)?
    You set the file position with SetFilePointerEx().


    Well, WriteFile doesn't write backwards - but if you set the fileposition to any particular position, you can write to that position.

    To "read backwards", you can do two things: Either read one character at a time, or blocks using ReadFile to read the data. In either case, you first set the file position to X less than the last position in the file (use a negative value and dwMoveMethod as FILE_END). Then read the amount of data. If necessary [as in, you didn't find the newline of the penultimate line], you move another X byte(s) back from the current position [which means -2X from current position (-2X using dwMoveMethod as FILE_CURRENT), because ReadFile just moved X byte(s) forwards].


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    26
    Will it allow to do it by multi process?

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    26
    I still can't make it work:
    After I've created new file via CreateFile,

    I set pointer:

    SetFilePointer(file, 0, 0, FILE_END);

    Now how to continue from here?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Move backwards from the end by one byte:
    SetFilePointer(file, -1, 0, FILE_END);
    Read one byte.
    Check if byte == '\n'.
    Repeat.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    26
    Done that, is it possible to insert new line between 2 last lines, without copying the last line??

    Because the operation must be very fast and I can't recopy last time each time.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how do you plan to inseart a byte between two bytes written on the disk without moving the second byte? you just overwrite it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by markiz View Post
    Done that, is it possible to insert new line between 2 last lines, without copying the last line??

    Because the operation must be very fast and I can't recopy last time each time.
    Not possible due to how files works.
    You may need to invest in a database if you need the speed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM