topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 5:18 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: C++ - Saving / Loading data  (Read 7172 times)

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
C++ - Saving / Loading data
« on: February 25, 2010, 12:30 AM »
Hi, first of all - I want someone to do exactly as I want and not to make any external ideas up or ask why i want it like so etc...

So, sorry bout that - to the topic:

I want a simple C++ example (with includings and stuff - a fully working program) which asks user a number and saves it TO THE EXE, TO A INI-FILE or TO A TXT FILE - or all 3.
But inorder to test this data-reading, I want it so - that when the program starts, it prints a list where user can read the saved numbers and after press of an enter, it will ask new number, saves that and again on startup it creates the list an so on...

I don`t need encryption, or security - I`ll do it later if I want, just give me example of that.

I would mostly want example of saving to exe and txt-file, ini is not so important.
Remember to test it, lol - so i dont have to reply with bugreport.


Thanks!

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #1 on: February 25, 2010, 02:19 AM »
Demand demand demand! :)

What kind of "ask user number" functionality do you want? Since you only say "C++" I'd guess a console mode app, but it doesn't hurt specifying more explicitly.

A little note: saving to .exe won't work - there's been various tricks proposed during the years, but most of them have relied on undocumented stuff and don't work today. If you really want to save to exe, you have to take the route I've done with fSekrit: basically, copy current .exe to a temporary location, append data there, launch temporary .exe with special flag to make it overwrite original exe, launch now-overwritten-with-new-data original exe with flag to make it delete the temporary copy.
- carpe noctem

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #2 on: February 25, 2010, 02:22 AM »
I think just a simple "cin>>" for number-input is fine - as Im using that "Command-Propt-Like-Thingy".  :-[
Btw, I tried to create that "Virtual-Screen" (where you can create ie. pictures by adding colored pixels in it) by using some tutorial but it had something to do with Pascal and It doesn`T workd on Win7, 64bit - So is there any other way, pascal-tut was all about some weird assemblycode :|?

And let`s change the exe to the .dll then, that would be nice - no matter how sucky it is.  :tellme:
« Last Edit: February 25, 2010, 02:26 AM by RedPillow »

Crush

  • Member
  • Joined in 2006
  • **
  • Posts: 402
  • Hello dude!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #3 on: February 25, 2010, 02:26 AM »
The code isn´t a big thing to do, but I don´t understand how to save it to an .exe or .dll file? I see no sense in doing this or at least a way to do this without destroying the exe-file. Is this prog for educational use?
« Last Edit: February 25, 2010, 02:28 AM by Crush »

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #4 on: February 25, 2010, 02:33 AM »
It`s just because I presonally think it`s the most easiest thing to do o_O.

Let`s say that I wan`t to make a standalone prog with the ask number and list em on startup -functions, and I cannot use registry
(blocked from the prog).
And I want this program so standalone, that it is only the .exe - nothing else.

But like I said, I would even respect the example of doing it with .txt but dll and exe would be nice too!


P.S Speaking of is it for educational use... It is, but how do I make a program which uses different processes without any reason except that it won`t let user to close it cause of that?
« Last Edit: February 25, 2010, 02:35 AM by RedPillow »

Crush

  • Member
  • Joined in 2006
  • **
  • Posts: 402
  • Hello dude!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #5 on: February 25, 2010, 03:25 AM »
You can write to the data hunks in dlls or exes, but its not so easy to do. You have to take care of the file structure, offsets and pointers. It could be a problem to the program task to write at its own file while running - I think the OS will not allow this (try to delete an .exe while running it in the background).
Viruskillers or other security software will treat these changes as an illegal operation and attack.
Reading and writing to normal text-files is incomparable to the others and much more easier to do.
You seem to need a console application. The listing of the complete content after each new number is for bigger amounts not very nice to look at.
It´s also a little bit strange how to implement a "virtual screen with colors" in a command prompt? Did you try to write to the vga-buffer directly (ASM)?
« Last Edit: February 25, 2010, 03:31 AM by Crush »

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #6 on: February 25, 2010, 04:20 AM »
This one to make the screen:

[PASCAL]
  Procedure SetMCGA;
  BEGIN
    asm
          mov     ax,0013h
          int     10h
    end;
  END;
[C++]
  void SetMCGA() {
    _AX = 0x0013;
    geninterrupt (0x10);
  }


This one to Delete it:

[PASCAL]
  Procedure SetMCGA;
  BEGIN
    asm
          mov     ax,0013h
          int     10h
    end;
  END;
[C++]
  void SetMCGA() {
    _AX = 0x0013;
    geninterrupt (0x10);
  }


I don`t know what to do with those exactly thou ;S
But can I get that reading/writing example by using txt-file to store the data?

Also, How to make ini-file for example so that I can set text-color to ini-file and the program will read and obey it on startup?

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #7 on: February 25, 2010, 05:31 AM »
Forget about that color-stuff you posted, it's 16-bit junk and doesn't apply to modern times - I assume you want to do win32 console apps. Forget about writing to DLLs, it's dirty & error-prone.
- carpe noctem

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #8 on: February 25, 2010, 05:45 AM »
Got that.

But I still would like to have that read/write example with a txt-file please :]

Crush

  • Member
  • Joined in 2006
  • **
  • Posts: 402
  • Hello dude!
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: C++ - Saving / Loading data
« Reply #9 on: February 25, 2010, 07:50 AM »
Outputs via DOS-interrupts is nowadays outdated and not working any more if you don´t use a VGA-Emulator like DosBox.
Here and here are some examples (at the bottom).
Additional ones are these: Here and here.
All you need is there inside. You only have to mix the file-output and text-input together - so you still have a little thing to do.
« Last Edit: February 25, 2010, 07:57 AM by Crush »