Thread: Binary File Errors

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    16

    Question Binary File Errors

    For a big project for school I have to make an airline reservation simulator but I have run into a problem. I want to save the the flight code and its location in a binary file so that I can obtain a code according to the location but this happens:

    The current output
    Binary File Errors-wxpkxpo-png

    expected output

    Binary File Errors-tjwtnde-png


    Here is the source class
    Code:
     class file
    {
        private:
    
    
        char code[8];
        char from[20];
        public:
            void input();
            void show();
            char *getn()
            {
                return code;
            }
    };file fileobj;
    
    
    void file::input()
    {
        clrscr();
        cout<<"Enter Code+ no: ";
        gets(code);
        int A=strlen(code);
        if(A==6)
            strcat(code,"  ");
        else
            strcat(code," ");
        cout<<"Enter Location: ";
        gets(from);
        int a=20-strlen(from);
        for(int i=0;i<a;i++)
              strcat(from," ");
    }
    void file::show()
    {
        cout<<"Code: ";puts(code);
        cout<<"\t\tLocation: ";puts(from);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Please don't make duplicate posts. Give people some time to find and answer your question.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    how to delete the current post then?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you need to decide on whether to use C or C++ first.
    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.

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    i will be programming in C++ but old standards which work with borland 5.02

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why old standards? Is it a requirement for your school?
    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.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    FWIW, your code variable is 8 characters and you are treating it as a C-style string when printing using puts which means that it can only reasonably store 7 characters (with the remaining one being a null terminating char). When you use the strcat function to add 2 characters to something that is already 6 characters, you completely fill the 8 available characters such that it does not represent a proper C-style string. You obliterate the location variable as well making it useless as a C-style string.

    You should use C++ std::string objects to store this information, or at the very least stop trying to concatenate too much data into strings that are too small. If the purpose of the concatenation is to make things align when printing, then you can use printf with width/alignment format specifiers to do the printing.

    BTW, this:
    Code:
    char *getn()
    {
        return code;
    }
    is dangerous because you expose a raw pointer into your object's data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Aug 2014
    Posts
    16

    Red face

    Increased the size by 1 and the program works thankyou so much

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code have a ton of problems. So again, I ask: why old standards? Is it a requirement for your school?
    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.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    3,863
    Quote Originally Posted by bilgramiraza View Post
    i will be programming in C++ but old standards which work with borland 5.02
    Do you know of a place to get a free and legal copy of borland 5.02?

    I have an embedded board that uses a highly modified version of borland 5.02 as the Compiler.
    I thought maybe a copy borland 5.02 would help learn what works and does not work with it.

    Hint: on your issue I suggest using strncpy instead of strcpy; or using std::string.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Aug 2014
    Posts
    16
    YUP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary File Errors
    By bilgramiraza in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2014, 10:17 AM
  2. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  3. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  4. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  5. oprantds errors of type int and const char [31] to binary???
    By Gaidin0147 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2003, 07:53 PM

Tags for this Thread