Thread: Text Files and Variables

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    Text Files and Variables

    Good Day All!

    Here is the situation I am struggling with:

    I have a text file with four variable names in it (eg. AA, BB, CC and DD - each on a different line).

    In the body of my code, I want to define the variables as some kind of text (eg. AA=store, BB=bank, CC=gym and DD=park).

    Also in the body of my code, I have four sentences. (eg. The man went to the <insert variable AA>, The woman went to the <insert variable BB>, The girl went to the <insert variable CC>, and The boy went to the <insert variable DD>).

    The ultimate goal is to define a number of variables in the body of the code and only have to edit the text file (add new variables EE, FF, GG, etc.) and change the <insert variable name>. I do not know how to call the variables from the text file and have them run properly in the code though. Can anyone help point me in the right direction? Thanks.

    -jjiimmyy101

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, jjiimmyy101!

    The AA-DD and beyond, variables, would fit nicely into index values - maybe with a scheme where "AA" = 'A'+'A'-'('A'+'A') = 0, "BB" = 'B'+'B'-('B'+'A) = 1, and DD = 'D'+'D'-('D'+'A') = 4.

    That could give you:
    Code:
    char str[4][80] = {
    {"store"},
    {"bank"},
    {"gym"},
    {"park"}
    };
    After you opened the text file, and read in the first four values, into the str[][] array.

    As long as each two letter designation will equate to a different integer value (and a low value to start with, preferably zero), you're good to go.

    The actual insertion of the text will be done with string functions, like strcpy() or strcat() (string copy or string concatenation).

    fgets() will nicely read in each line of text from the file. If you haven't used these functions yet, spend some time working with them. Perhaps from the tutorials on here (liked to at the top of this page).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between variables and normal text
    By kocmohabt33 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2010, 01:24 PM
  2. Getting variables and numbers from a text file.
    By snoikey in forum C Programming
    Replies: 29
    Last Post: 07-13-2010, 07:26 AM
  3. Writing variables to a text doc?
    By Cielo in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2008, 04:10 AM
  4. Replies: 3
    Last Post: 11-28-2006, 03:44 PM
  5. Displaying Text and Variables to a window
    By StevejLuke in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2002, 01:36 PM