fstream.h question

This is a discussion on fstream.h question within the C++ Programming forums, part of the General Programming Boards category; Lol ok ok i haven't been around for a while...hooked on diablo II....ok im starting to go back to C++...........the ...

  1. #1
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269

    fstream.h question

    Lol ok ok i haven't been around for a while...hooked on diablo II....ok im starting to go back to C++...........the question is:

    When you output a file, how do you keep it from the user being able to open up with notepad and change numbers/strings?

    Oh and what is it called?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    I believe you could use a system() command to
    change the file attribs to read only

    I wouldn't call it secure by any means though

  3. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    do you know what system(); function it is? or is there another way.....

    i think i know some way....

    Code:
    .......
    int main()
    {
      ofstream output("filename.txt", some::thing);
    }
    i've read something like that before but i haven't gotten an explanation yet........pleas help!
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >do you know what system(); function it is?
    This is system dependent, the easiest way is to either encrypt the data before writing it, or write it in binary mode so that if the user opens the file, it will be impossible to change without causing the program to fail when reading it.

    >ofstream output("filename.txt", some::thing);
    ofstream output ( "filename.txt", ios_base::binary );

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest

    kinda related .....

    What is the use of deriving a class you make from fstream ?

    I ask because i see that once and a while.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What is the use of deriving a class you make from fstream ?
    One word, customization. If a base class doesn't have the functionality that you want, you can derive your own child class and mold it however you want.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    I dont know what your asking.....but this is what i can say:

    i use fstream for files and for looooong games so you can save it and load later.....and i want to know this so the player cant change, like in an rpg, change yoru lvl, strength, defense, so and so........
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    And Prelude is saying write it in binary so that they can't change it. Unless they can read binary of course.

  9. #9
    Unregistered
    Guest
    why doesn't the binary file only show 1's and 0's,

    sometimes I can read whole strings and such ?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why doesn't the binary file only show 1's and 0's
    Because 1's and 0's are simply symbols for the values 1 and 0 when printed to your screen. Real binary code has no defined characters to print so the character set interprets it as junk characters that mean nothing.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    well......how do you write binary?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  12. #12
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Here, This should get you started -

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    
    struct applicant {
    	char name[61];  // applicant's name
    	char phone[21]; // phone number
    	long date;      // date of application in yyyymmdd format
    };
    
    
    int main(void){
    
    	fstream fs("2111112.aaa", ios::in | ios::out | ios::binary);
    
    	applicant x;
                x.date=9;
                strcpy(x.name,  "ginoitalo");
                strcpy(x.phone, "13281");
    
    	cout<<endl<<"--------------"<<endl<<"Name="<<x.name<<endl;
    	cout<<"Date="<<x.date<<endl;
    	cout<<"Phone="<<x.phone<<endl<<"--------------"<<endl<<endl;
    
    	if(!fs.fail()){
    			fs.seekp(0, ios::beg);
    			fs.write( (char*)&x, sizeof(applicant) );
    			fs.close();
    	}
        else{
    		exit(99);
    	}    
    
    
    	// File is Totally Closed as of now.
    
    	// Re-open to prove it doen't read grabage.
    	fstream fs2("2111112.aaa", ios::in | ios::out | ios::binary);
    	
    	if(!fs2.fail()){
    
    		fs2.seekg(0, ios::end);
    		
    		cout<<"End of file        = "<<fs2.tellg()        <<endl;
    		cout<<"Size of applicant  = "<<sizeof(applicant) <<endl;
    
            int recs = ( 1*fs2.tellg() / sizeof(applicant) );
            cout<<endl<<"Records in file="<<recs<<endl;
    
            applicant in;
            fs2.seekg(0, ios::beg);
            fs2.read((char*)&in, sizeof(applicant) );
    		fs2.close();
    
    		cout<<endl<<"--------------"<<endl<<"Name="<<in.name<<endl;
    		cout<<"Date="<<in.date<<endl<<"Phone="<<in.phone<<endl;
    		cout<<"--------------"<<endl<<endl;
    	}
    	else{
    		exit(98);
    	}
    
    
    return 0;
    }

  13. #13
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    In regards to opening a binary file, if the file is outputted with characters, the text editor will still be able to read it. If you write an interger to a binary file however, each byte of the two byte interger will be interpreted it=nto the matching character. I advise using simple XOR encryption however though. It is a little more fool proof, because there are programs out there that will let you see binary files for what they may or may not actually be. I know, because i have tried what you are talking about to try to fool-proof my game save files.

    When you come up with a good way, derive a class from fstream and encapsulate the functions there and put that into a library. This way, you can use it in all of your cames.

    ~Inquirer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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