Results 1 to 8 of 8

Thread: C++ Permanent Variable whose value can be changed after runtime

  1. #1
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts

    C++ Permanent Variable whose value can be changed after runtime

    I've written a program and need some help in C++.
    The code is currently about 720 lines and i need one thing i'm not sure of.

    Can we have a variable of type 'char[n]' (an array of characters), whose value the user will input during runtime and the value stays even when the program is closed and can be accessed later.

    How do i do this??

  2. #2
    Keldorn's Avatar
    Keldorn is offline Net Builder
    Join Date
    Dec 2008
    Location
    Canada
    Posts
    400
    Thanks
    21
    Thanked 60 Times in 52 Posts
    Quote Originally Posted by Snak3 View Post
    I've written a program and need some help in C++.
    The code is currently about 720 lines and i need one thing i'm not sure of.

    Can we have a variable of type 'char[n]' (an array of characters), whose value the user will input during runtime and the value stays even when the program is closed and can be accessed later.

    How do i do this??

    You might have to save it into a .dat or .xml file and save in their user folder, which you would open up on runtime when they start it again.

    I do not think you can access memory registers after the program is closed, although the memory might be still be there, if you didn't make any garbage collection.
    Submit new proxies -

  3. Thanked by:

    Snak3 (26 November, 2009)

  4. #3
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts
    Actually i want to somehow avoid making another file as the value i want stored will be a 6-letter pin. Is there ANY way i can somehow store the value and make it editable within the program itself such that the value will be retained??

    I don't want to access any external files.

  5. #4
    Abdullah is offline Newbie Net Builder
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    12
    Thanked 6 Times in 5 Posts
    Quote Originally Posted by Snak3 View Post
    Is there ANY way i can somehow store the value and make it editable within the program itself such that the value will be retained??

    I don't want to access any external files.
    Certainly not. The memory allocated during the previous execution will be lost.

  6. #5
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts
    Quote Originally Posted by Abdullah View Post
    Certainly not. The memory allocated during the previous execution will be lost.
    Yes, I am aware of that.

    Just to make it clearer, what i exactly wanted is to know if there is a way to have some variable whose value can be changed at runtime(same memory space), i.e. say i want 'SYD' stored in an array of 3 characters(char[3]) to be changed to 'XYZ', and it can retain the value.

    The only way possible without using an external file is to somehow make the program manipulate the .exe file(itself) and store the value. I don't think its possible, even if possible it surely isn't easy.
    If any1 knows how it can be done or even if it is possible, please let me know.

  7. #6
    Ban Me is offline Unknown Net Builder
    Join Date
    Sep 2009
    Posts
    28
    Thanks
    3
    Thanked 6 Times in 4 Posts
    Yes it is possible to store a number in the EXE of the program.

    For example - To store a single character in a file :

    Code:
    #include<stdio.h>
    
    main()
    {
        FILE *fp;
        unsigned int end_pos, head[3];
        char data = 'a';
        if((fp=fopen("abc.exe", "rb+"))==NULL)
        {
            printf("Cannot open file"); exit(0);
        }
        if((fread(head,sizeof(int),3,fp))!=3)
        {
            printf("Can't read data"); fclose(fp); exit(0);
        }
        end_pos = 512 * (head[2]-1) + head[1] +1;
        fseek(fp, end_pos, SEEK_SET);
        fwrite(&data, sizeof(data), 1,fp);
        fclose(fp);
    }

  8. Thanked by:

    Snak3 (26 November, 2009)

  9. #7
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts
    Quote Originally Posted by Ban Me View Post
    Yes it is possible to store a number in the EXE of the program.

    For example - To store a single character in a file :

    Code:
    #include<stdio.h>
    
    main()
    {
        FILE *fp;
        unsigned int end_pos, head[3];
        char data = 'a';
        if((fp=fopen("abc.exe", "rb+"))==NULL)
        {
            printf("Cannot open file"); exit(0);
        }
        if((fread(head,sizeof(int),3,fp))!=3)
        {
            printf("Can't read data"); fclose(fp); exit(0);
        }
        end_pos = 512 * (head[2]-1) + head[1] +1;
        fseek(fp, end_pos, SEEK_SET);
        fwrite(&data, sizeof(data), 1,fp);
        fclose(fp);
    }
    Thanks

    It just went over my head, i'll print it out and ask my prof to explain it to me.
    Thanks again.

  10. #8
    Keldorn's Avatar
    Keldorn is offline Net Builder
    Join Date
    Dec 2008
    Location
    Canada
    Posts
    400
    Thanks
    21
    Thanked 60 Times in 52 Posts
    Quote Originally Posted by Ban Me View Post
    Yes it is possible to store a number in the EXE of the program.

    For example - To store a single character in a file :

    Now that I think about, that is probably possible. As PuTTY does this, how else does the PuTTY remember your "saved sessions" while still being able to be moved anywhere, computer from computer.
    Submit new proxies -

Similar Threads

  1. Mod DB and style DB totally changed on php.com
    By StephenM in forum Community Building
    Replies: 5
    Last Post: 7 May, 2010, 16:22 PM
  2. Setting up HTTP/HTTPS Variable in PHP
    By Zash in forum Programming
    Replies: 10
    Last Post: 11 August, 2009, 12:57 PM
  3. Pictures That Changed The World
    By Snak3 in forum General Chat
    Replies: 4
    Last Post: 4 August, 2009, 14:43 PM
  4. Changed the gear
    By GeeOne in forum General Chat
    Replies: 1
    Last Post: 4 June, 2009, 06:37 AM
  5. Replies: 0
    Last Post: 2 June, 2009, 23:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •