Thread: I/O using a STL container

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    1

    Wink I/O using a STL container

    http://www.shreve-lib.org

    I am new to C++ programming using the STL Library. Can someone demostrate a code snippit that would equate the same performance as this:


    #include <fstream.h>

    #include <iostream.h>



    int main()

    {

    char HaveTextArray[250], *HavePtr,HaveFile[20];
    HaveFile="HaveTextFile.txt";
    HavePtr=&HaveTextArray;

    ofstream Text_file(HaveFile);

    //Creates an instance of ofstream, and opens HaveTextFile.txt

    Text_file<<"This text will be placed into the file HaveTextFile.txt";

    //Outputs to HaveTextFile.txt through Text_file

    Text_file.close();

    //Closes up the file

    ifstream Text_file(HaveFile)
    //Opens for reading the file

    while(HavePtr!=EOF) {
    //Reads a string - up to 250 characters long - from the file

    Text_file>>HavePtr;
    HavePtr++;
    }

    cout<<HaveTextArray;

    Text_file.close();

    //Close the file.

    }

    /*
    Now how do I do the samething using a contain in the STL where a class holds the data container?

    -----------------------------------------------------------------------------------
    */

    // Setup a class to hold the container:
    using STD;
    class StorageContainer {
    std::Map(char Array[250]);
    }

    // I may not have set this up right, but you see what is am trying to do

    /* How would I use "StorageContainer" to load up the "Array" varable, STORE IT into a file called "Text_File" and retrieve the
    data from the "StorageContainer" class's STL container?
    */
    Last edited by LA Mills; 04-19-2003 at 12:21 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Alright, you typed too many comments. Be more specific and get to the point.

    Assuming the source file containers texts, where do you want to store them? Do you want to store them in a container? What container?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    reading a space delimited file with ints into a STL vector container. Uncompiled.
    Code:
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
       int x;
       vector <int> data;
        ifstream fin("MyData.txt");
        fin >> x;
        while(!fin.eof())
        {
            data.push_back(x);
            fin >> x; 
        }
        //do something with data here
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "reading a space delimited file with ints into a STL vector"

    Yeah, but apparently he wants an STL container for chars. Does a string object count?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    fair enough

    change the <int> to <char> and int x to char x and as long as the file is delimited by whitespace everything is cool.

    to use with STL strings add #include <string> change <int> to <string>, change int x to string x, and change fin >> x to getline(fin, x, delimitingChar).

    The underlying structure of the program remains the same, the individual components differ depending on needs/desires.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why not just use istream_iterator and ostream_iterator to read into and out of a container
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stl container to store more than 1 type
    By sujeet1 in forum C++ Programming
    Replies: 7
    Last Post: 05-09-2007, 04:10 AM
  2. inherited classes and stl container
    By rahulsk1947 in forum C++ Programming
    Replies: 9
    Last Post: 05-05-2007, 02:27 PM
  3. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM