i/o assigning variables

This is a discussion on i/o assigning variables within the C++ Programming forums, part of the General Programming Boards category; let's say the following is in a text file Code: age = 86 favorite number = 1 i know i ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    67

    i/o assigning variables

    let's say the following is in a text file

    Code:
    age = 86 
    favorite number = 1

    i know i can use a_file.get() and functions like that but what if i want to get those numbers out of the text file and assign it to variables? also, how do i change those numbers in the text file without adding onto it or rewriting the text?

    example: i assign the value 86 from the text file to a variable called test. print test to the screen. ask for a new value. change the value of 86 in the text file to new value.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The easy way is with something like this:
    Code:
    struct name_value_pair {
        std::string name;
        int value;
    };
    
    ostream & operator << (std::ostream &os, const name_value_pair &p) {
        return os << p.name << " = " << p.value;
    }
    
    std::istream & operator >> (std::istream &is, name_value_pair &p) {
        std::string name;
        int value;
        if(std::getline(is,name,'=') >> value) {
            p.name.swap(name); p.value = value;
        }
        return is;
    }
    This lets you use name_value_pairs more or less like you would a simple int. Both operators will work just as well on text files or string streams or whatever decends from istream. Some things you might want to do are define a < operator so that you can have a std::set of name_value_pairs (really just a std::map the hard way). You also may want to make name private and normalize it to make comparisons case insensitive and ignore whitespace. Currently "Age=22" is different from "age = 22". Normalizing is the process of converting something that has many different, but semantically identical forms to one particular form. For example, all lower case with no leading or trailing whitespace.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you want to change values stored in a file then you need to read the entire file into the program, change the data, and write the entire file contents back to the file, unless you can pinpoint the location of the data to change because the file is set up in a perfectly predicatble fashion. If the file is perfectly predictable you can use fseek() etc to position the file pointer and overwrite the specific value as long as the overwrite takes up the exact amount of memory in the file that the original did.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by elad
    If you want to change values stored in a file then you need to read the entire file into the program, change the data, and write the entire file contents back to the file
    not necessarily... you can rewrite one record at a time, like I do... I open a temporary file, write one record at a time, replacing records that are being updated, and when that's done, copy the temp file into the original and delete the temp file...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    major_small can u show me a code example of what u do

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 05:42 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 05:39 PM
  5. .dat I/O not loading variables
    By 7smurfs in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2004, 05:43 AM

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