Few Questions

This is a discussion on Few Questions within the C Programming forums, part of the General Programming Boards category; How can I write to the beginning of the file? I have it working now but it overwrites in it's ...

  1. #1
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Few Questions

    How can I write to the beginning of the file? I have it working now but it overwrites in it's path which is annoying :P

    Also is there anyway I can have it read a file and take out a certain line of code?

    LAST BUT NOT LEAST how can I use variables in system()? Right now I have it retardidly writing the command to a file, reading the file to a var, then run the var, when I want it to be able to system("yada %s") type thing.

    *Edit* One last thing, How can I make it read a sentence w/ spaces? I currently have it reading a char that works and stuff, but it stops at spaces, thanks again.

    I'm still pretty new to C although I have all the basics down, any help would be appreciated ^_^ Thanks!
    Last edited by Ne0; 08-18-2004 at 10:49 PM.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    a space can be read the exact same way as a character.
    show us some code.

    im not sure if this is what you meant,
    to start a new file or to overwrite and old file you need to open it like this
    Code:
    fp = fopen("C:\path\","w");
    to append to the end of the file you change the 'w' to 'a'
    Code:
    fp = fopen("C:\path\","a");

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > How can I write to the beginning of the file?
    You can't insert stuff into the middle of a file - you have to rewrite the whole file.

    > Also is there anyway I can have it read a file and take out a certain line of code?
    Use fgets() to read each line, and a counter to stop at the one you want.

    If the file is fairly static (doesn't change), fairly large (several MB), and you need to do this a lot, then you can build an index. But that does require a lot of extra work at the start.

    > how can I use variables in system()?
    See other recent posts, it's being discussed right now in another thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    Quote Originally Posted by sand_man
    a space can be read the exact same way as a character.
    show us some code.

    im not sure if this is what you meant,
    to start a new file or to overwrite and old file you need to open it like this
    Code:
    fp = fopen("C:\path\","w");
    to append to the end of the file you change the 'w' to 'a'
    Code:
    fp = fopen("C:\path\","a");
    I meant how do you ADD lines of code to the beginning of a file that already contains information.

    > How can I write to the beginning of the file?
    You can't insert stuff into the middle of a file - you have to rewrite the whole file.
    Damn.

    > Also is there anyway I can have it read a file and take out a certain line of code?
    Use fgets() to read each line, and a counter to stop at the one you want.
    I'm still kinda new to C so I don't get the counter part o.o Throw a hint? XD
    > how can I use variables in system()?
    See other recent posts, it's being discussed right now in another thread.
    Thanks gonna look now
    Last edited by Ne0; 08-19-2004 at 01:12 AM.

  5. #5
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Quote Originally Posted by Ne0
    I meant how do you ADD lines of code to the beginning of a file that already contains information...
    a. open file.
    b. read old data to memory.
    c. seek begin of file.
    d. write new data begin of file
    e. append data from memory to this file.
    f. close file.
    Last edited by tjohnsson; 08-19-2004 at 01:39 AM. Reason: didn't answer to question... :rolleyes:

  6. #6
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    Thanks, and couldnt find that recent thread on system()

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

  8. #8
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    Thanks! Can anyone throw me a hint on how I could delete somthing in a file? I asked above and someone mentioned a counter o.o

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can't open a file and edit it. You'll need to open it, read in the contents, make all the changes to it while it's in memory, write your new contents to the file. You can add stuff to the very end of the file, but that's it.

  10. #10
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    So you can't delete a certian line in a file?

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by tjohnsson
    a. open file.
    b. read old data to memory.
    c. seek begin of file.
    d. write new data begin of file
    e. append data from memory to this file.
    f. close file.
    similar to what tjohnsson said
    a. open file.
    b. read old data to memory.
    c. edit data
    d. write new data
    f. close file.

  12. #12
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    How do you edit it though? O_o

  13. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    For what I think is the fourth time you're being told this, you can not just "edit a file". You have to open it, read it into memory in your program, make your changes to the copy in memory, read what's in memory in to the file, and close the file. If you don't understand this by now you need to go find a tutorial on File I/O.

  14. #14
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    Lol, well you said edit the data so I asummed you meant file. And i will go look that up because that's what i've been asking how to do this whole time :P Making changes to the copy in memory.

  15. #15
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Arrays, pointers, strings. You should learn that before trying to do any file i/o.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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