Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

How to retrieve data from text file (notepad) to 2d array?

Last post 08-03-2008 8:01 PM by Mohammad Rastkar. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-15-2008 5:08 PM

    • onglim
    • Not Ranked
    • Joined on 06-15-2008
    • Malaysia
    • New Member
    • Points 30

    How to retrieve data from text file (notepad) to 2d array?

    Hi.

    How to retrieve data from text file (notepad) to 2d array? Example, info[3][3]

    Data in the text file:

    Well     12    4567

    hello     15    4532

    weeee   17   2432

    info[0][0]= "Well" , info[0][1] = 12 , info[0][2] = 4567 , info[1][0]= "hello" . . . . .

    Thanks

    OL

     

    • Post Points: 15
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 08-02-2008 3:02 PM In reply to

    • daniel_t
    • Not Ranked
    • Joined on 08-01-2008
    • United States
    • New Member
    • Points 15

    Re: How to retrieve data from text file (notepad) to 2d array?

    I'll try again... First there are some questions to be answered... Is this for C++ or C? Do you want a 2d array of strings, or were you hoping to have a string and two ints for each entry?

    Assuming the answers are "C++" and "strings" then the code below will do the job.

    istream& readRow( istream& is, vector< string >& vec )
    {
        vec.clear();
        string input;
        if ( getline( is, input ) ) {
            istringstream iss( input.c_str() );
            copy( istream_iterator<string>( iss ), istream_iterator<string>(), back_inserter( vec ) );
        }
        return is;
    }

    int main( int argc, char *argv[] )
    {
        if ( argc != 2 ) {
            cout << "Usage " << argv[0] << " file.txt\n";
            exit( -1 );
        }
        ifstream file( argv[1] );
        vector< vector< string > > stuff;
        vector< string > input;
        while ( readRow( file, input ) ) {
            stuff.push_back( input );
        }
        // stuff is your 2d array
    }

    • Post Points: 5
  • 08-03-2008 8:01 PM In reply to

    Travel [ap] A simple way...

    #include <string>

    #include <fstream> // file interactions

    #include <iostream>

    using namespace std;

    int main()

    {

    ifstream ifs("data.txt");

    string infos[3];

    // for something like : Well(string), 12(string), 4567(string)

    ifs >> infos[0] >> infos[1] >> infos[2]; // for first line of file, then just repeat this for second one

    // here fill array with infos

    ifs.close();

    return 0;

    }

    • OR :
    int i[2];

    string info;

     // for something like : Well(string), 12(string), 4567(string)

    ifs >> info >> i[0] >> i[1];

    - NetTimeSaver : My software that logs your connecting informations (like duration).
    • Post Points: 5
Page 1 of 1 (3 items)