Writting in a file (simple question...)

This is a discussion on Writting in a file (simple question...) within the C Programming forums, part of the General Programming Boards category; How can you write X character in a X position in a file without erasing the rest? Like if you ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Writting in a file (simple question...)

    How can you write X character in a X position in a file without erasing the rest?
    Like if you have a file
    "Name:Tom and Jerry\n"
    and you want
    "Name: George and Jerry\n"
    how it should be done effectively?

    I believe fputc(), fwrite() overwrite existing data in the file so they are not useful. Like they would do:
    "Name: GeorgedJerry\n"

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    You can't. You need to move the data (push it, so to speak), to make room for more data.
    Code:
    "Name:Tom and Jerry\n"
    "Name:Tom    and Jerry\n"
    "Name:George and Jerry\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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Is there a function to do that for you? Like an fwrite() that pushes data...

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    No, you'll have to do it manually.
    In this example, read "and Jerry\n" (everything after the "Name:Tom " part).
    Calculate the difference of length "Name:George " - "Name:Tom " (which is 3 in this case).
    Seek to "Name:Tom " + offset you calculated. Write the data you read.
    The result should be something like:
    "Name:Tom andand Jerry\n"
    Now you'll have enough space to write overwrite "Name:Tom and" with "Name:George "
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or, alternatively, write a new file with the new information (all of it, even the stuff that didn't change), and then later, copy it to the old filename.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Thanx. I have done it as tabstop said, I just expected for their to be already a function. Usually I first create a function to do my job and search later if it was already there :P

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Ah well, no. Unfortunately, there is no such function at OS level, either, so it's not like it's a failing in the C library (it was meant to be low level).
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Simple file input question?
    By LeighRogers in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2007, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21