Thread: C++ File Read Manipulation

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    C++ File Read Manipulation

    Background: Not too new to C++, never learned professionally, sparatic hobbie.

    I am trying to create a game that requires reading of a file, but in entire line of code.
    I can do it by switching it:

    Code:
    char name[256];
            cout << "Enter Game File: ";
            cin.getline (name, 256);
            ifstream ifs(name);
            string line;
            while(getline(ifs,line))
            {
                    cout << line << endl;
            }
            return 0;

    But the file i am trying to access has values that i need to be able to use, but it is now in bool form. I could use the

    Code:
    main()
    {
        string line;
        while( getline(cin, line) )
            cout << line << '\n';
    }

    but this is not practical because i need more manipulation...

    is there a way to use the first example and parse the bool values back to integers?

    the file would look something like

    [file.txt]
    string of words
    string of words
    2
    string of words
    8 (or some value in the form of an integer)

    Sorry if my english is not well.

    Thank you in advance
    Last edited by Ken Fitlike; 08-19-2006 at 08:33 AM. Reason: added code tags

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Thank you! I forgot about the code tags

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You can make a stringstream out of the line you have read: http://www.cplusplus.com/ref/iostream/stringstream/

    Code:
    stringstream s(string_line);
    int n;
    
    if(s >> n)
    {
          // Successful conversion, not a string of 
          // words (probably, I don't know what those 
          // words are).
    }
    You referred to boolean values. You can do the same sort of thing with them, but if it's actually written as "true"/"false", then you should set the boolalpha flag. http://www.cplusplus.com/ref/iostrea...ratorgtgt.html I don't know whether boolalpha or noboolalpha is the default.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you want to turn a string, say "1234", into the integer 1234, then you can use the atoi function.

    For example, try this:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    int main(int, char**){
         std::string s = "123";
         std::cout << "As a string: " << s << std::endl;
         int x = atoi(s.c_str());
         std::cout << "As an int: " << x << std::endl;
         x += 10;
         std::cout << "With 10 added: " << x << std::endl;
    }
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    i have the files for a runescape private server if those may help....like using the autosave code from it...stuf like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 4
    Last Post: 10-21-2003, 04:28 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM