Thread: Ahh filestreaming help(one question)

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    Ahh filestreaming help(one question)

    K hi there for my comp class we need to wright a program that randmly fills in a file then it check with a file that has a story in it and it keps doing this till it generates an exact copy of that file. Well i got evrything done beside i got problem every time i open for writing it truncates the file i am working on grrrr. So i tryed
    Code:
    "ofstream.outfile ("my.txt", ios::app);"
    .
    But that puts it at the end of the file so that does not work and i tryed putting the
    Code:
    "outfile.seekp(0);"
    and now it is driving me crazy. I also tryed
    Code:
    "ofstream ("my.txt", ios::out);"
    that did nothing. PLZ help me lol. And i know u can cope it over to second file but then it be SO much work lol. I was wondering if there is simpler way not because i am lazy but i made my whole program do this way and will take long time to fix. Thanks if u did not understand this at all my bad kind of in a rush. THANKS

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You can't write to the beginning of a file without overwritting what is already in it. You have to open the file, read the whole file into a buffer. Write whatever you wanted in the beginning into the file, then rewrite what was in it after that. I can't quite understand what you're assignment is, you weren't clear, but I don't see why you need to do what you're doing anyway.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    K the assiment needs to be:
    I take and paste a story into a txt (lets name is lemony)
    In a diffirent text it randomly generate characters (name this one text)
    Umm take lemony open as read text then compare them. The ones that are right characters leave them the wrong onces need to be randomly generate again. Keep doing this till the whole file is not complete, And text looks same as lemony.

    And I undestand what u mean but my teacher argued on that. He said what if u have like a huge novell with 1.5 million characters.

    And beside i dont mind to overwrite it. But does it have to truncate it. Like i mean keep all the info in there unless i overwrite it.
    Last edited by kennny2004; 03-20-2006 at 10:45 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, it does have to truncate it. It's hard to tell, but when you open a text editor on notepad and start writing at the beginning and all the text that was in there moves over, a whole lot is going on.

    I don't get what your teacher is arguing about personally. 1.5 million characters is what? 1.4mb? and when you open a file as an ofstream everything in that file gets put into memory anyway, so moving it to a string buffer really wouldn't make a difference to memory.

    Anyway, what I would do for this assignment is first read the story into a string. Then randomly generate a string of the same length. Then while the strings don't match, go through each character and keep randomly generating new characters for any unmatched characters. It's slow and unefficient, but it's pretty straight forward. If you want to optimize it a bit, you could start cutting out the beginning as it begins to match and put that into a new variable, but I wouldn't worry about that.

    What you have to watch out for is what exactly is in your file. Consider that it will contain spaces, punctuation, capitals, and probably newline characters.
    Last edited by SlyMaelstrom; 03-20-2006 at 11:09 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    Well it be all done If it did not truncate it lol. But like yah we need to do it really fast the most efficient way. But like if i copy into a buffer string will i be able to edit it? in the buffer string.

    Well i am done for the night will do it tommorow at class grrr. It was due like 2 days ago lol.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ofcourse. Strings a much more easy to edit than a file. That's why you do it. You have to understand that what ifstream does is open a file and unload the whole thing into a buffer on the open() function. From there you're just working with their buffer. Unfortunately, there isn't many functions available to you in order to edit that, this is why you put it into a string.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM