File Garbage

This is a discussion on File Garbage within the C++ Programming forums, part of the General Programming Boards category; I have a function that writes a "block" of code to a file. The problem is, it writes like it ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    File Garbage

    I have a function that writes a "block" of code to a file.

    The problem is, it writes like it is supposed too, but at the end there are characters I didnt tell it to write(jarbled up garbage).

    What causes this to happen and how can I stop it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Usually by opening the file in binary mode
    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.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    post the code

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    void Obj::WriteRef(char *RefBuffer, long size, char *RouteName)
    {
      char FilePath[255];
      sprintf(FilePath, "C:\\Program Files\\Microsoft Games\\Train Simulator\\Routes\\%s\\%s.ref", RouteName, RouteName);
    
      ofstream REF(FilePath, ios::app);
    
      if(!REF.is_open()) {
        cout << endl << "Error Writing Ref";
        cin.get();
        exit(1);
      }
    
      REF << endl << endl;
      REF.write(RefBuffer, size);
    
      REF.close();
    }
    Does C++ automatically open in binary? If so, can I treat it as a text file?

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Just to show you whats happening

    Code:
    static (
      class ("Blah")
      shape(Foo)
      Align(none)
    )
    
    static (
      class(Bar)
    )  pA D@ tA @
    Thats what it puts out... wich is fine except for
    pA D@ tA @

    I cant for the life of me figure out where that last bit is coming from... so odd!
    Last edited by Vicious; 07-19-2004 at 10:51 PM.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I dont mean to keep bumping this thread or seem impatient, but its driving me bonkers. This is the only little bug I need to work out before the program is finished. I have searched google HIWORDs and LOWORDs and cant find any thing.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your size is too large and you are writing random characters.

    If RefBuffer is a null terminated character string just use:
    Code:
    REF << RefBuffer;
    or
    Code:
    REF.write(RefBuffer, strlen(RefBuffer));
    Otherwise, fix your code so that size is correct. If you still have problems, post the code where size is calculated.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Erm, why are you treating what appears to be a text file as a binary file?
    You also need to show how you set up your RefBuffer, and how you calculated the length.
    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.

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I used strlen and it worked. Thank you.

    I got the size by opening the file, using seekg to pint at the end, then assigning the value of tellg to a const long called size. The function then returned "size" and I made
    char RefBuffer[size];

    Thanks Again

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Erm, why are you treating what appears to be a text file as a binary file?
    I am? I didnt realize it. How can I treat it as text then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  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

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