Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: c++ read from file question

  1. #1
    Join Date
    Feb 2008
    Beans
    42
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    c++ read from file question

    Hi.
    I am quite new to c++, but I figured I'd like to learn it so I'm playing around with some things.

    Currently I am trying to read from a text file with the following content:

    "city1", "city2", "city3"...

    i.e. a file with several strings in "" separated with commas. I would like to store each of these strings into a cell in an array. (Rewriting the text file format is out of the question, because really there are thousands of strings in it).

    So far I am using this code:

    ifstream inFile; // Declaring an input file stream (ifstream) variable.

    inFile.open("/home/..../test2.txt");

    if (!inFile) { // Checking that the file could be read
    cerr << "Unable to open file test2.txt";
    exit(1); // call system to stop
    }

    while (inFile >> x){ // read from the stream

    }

    inFile.close(); // Closing the input stream to free memory
    Now, the problem is obviously the (inFile >>) command. I dont know how to use syntax so the that I load each string from the file into a separate array cell. All I have been able to do is load all the text in the textfile into a single variable at once. (of course, while using another, shorter textfile).

    Any help is appreciated, thanks.
    How to become immortal:
    Read this signature tomorrow and follow its advice.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: c++ read from file question

    Any reason you are using C++? That is not the most useful language for text processing...

  3. #3
    Join Date
    Feb 2008
    Beans
    42
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: c++ read from file question

    Not for this, no.
    I want to learn some c++, and this is just part of one of many different "problems" i found on a website.
    How to become immortal:
    Read this signature tomorrow and follow its advice.

  4. #4
    Join Date
    May 2007
    Location
    Belgrade, Serbia
    Beans
    172
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: c++ read from file question

    Code:
    while (   !inFile.eof()  ){ // read from the stream
    inFile >> x;
    }

    You don't need the following :
    Code:
    inFile.close(); // Closing the input stream to free memory
    ifstream destructor does the job when exiting from function.

    You should get used to close files without close() method , for example in the stuff like try/catch blocks. But, don't worry about that for now , you'll see it yourself in practice .
    Last edited by dempl_dempl; March 14th, 2008 at 01:54 AM.
    http://www.stosha.net/ - Collection of linux widgets and libraries.

  5. #5
    Join Date
    Feb 2008
    Beans
    42
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: c++ read from file question

    Thanks a lot. That gets me further.
    But, should it not be ! inFile.eof() ?

    This function reads one character at a time, right. I would like to be able to read an entire string at time. i.e first read out: "Washington", second read out: "Chicago" etc. They are separated with commas in the file.

    If I'm missing something obvious, please bear with me.
    How to become immortal:
    Read this signature tomorrow and follow its advice.

  6. #6
    Join Date
    May 2007
    Location
    Belgrade, Serbia
    Beans
    172
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: c++ read from file question

    Yeap, sorry, my bad .
    I've corrected the post.


    Show me the file.txt , the way it looks like .
    (1) If the names are placed in sequence :
    e.g. :

    Code:
      Belgrade Washington Paris Moscow
    You can do this:

    Code:
    string str;
    ifstream fin ( "myfile");
    while ( !fin.eof()  )
    {
       fin >> str;
     /*do something usefull here :-)*/
    }

    In the case cities are separated by quotes and commas , I can give you this hint :
    I believe you don't need commas when you have quotes, and you don't need quotes when you have commas , because both of them are good separators. I would use the commas or newlines ( "\n" )

    I haven't checked the code, please post if you find something wrong . Spelling and grammar errors are exception .

    Drop the file , the way it looks like, in order to give you precise instructions.

    Cheers!
    Last edited by dempl_dempl; March 14th, 2008 at 02:18 AM.
    http://www.stosha.net/ - Collection of linux widgets and libraries.

  7. #7
    Join Date
    Feb 2008
    Beans
    42
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: c++ read from file question

    Code:
    "Kaneohe","Kapolei","Salt Lake","Ewa Beach","Manoa"
    This is what the file looks like. And it goes on of course.

    EDIT: And all the text is in one line.
    How to become immortal:
    Read this signature tomorrow and follow its advice.

  8. #8
    Join Date
    Mar 2006
    Beans
    837

    Re: c++ read from file question

    look here for more info on I/O with C++ link.

    If you try the above example then you will read the text file line-by-line. Each time you read a line into a "string" variable you should parse the string variable. Eg search the string for , and ".

    More info for string's method here

  9. #9
    Join Date
    Feb 2008
    Beans
    42
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: c++ read from file question

    Thanks, I will definitely check that out.

    I guess I was just hoping that since

    char *cities[3]={"bergen","london","madrid"};

    is a valid statement, and on the same format as my file, I could in a crude way get my file to be loaded as those initializers.

    I'll get some sleep now, but if anyone has a great idea, I'd love to wake up to it.
    How to become immortal:
    Read this signature tomorrow and follow its advice.

  10. #10
    Join Date
    May 2007
    Location
    Belgrade, Serbia
    Beans
    172
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: c++ read from file question

    Ok, I thought there were spaces between the cities.

    Here's the code that will work:

    Code:
    #include <list>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    char ch;
    
    
    ifstream fin ( " myfile.txt" );
    list<string> cities;
    
    string tmp_str;
    
    while(  !fin.eof() ) 
    {
     fin.read(&c,1);
     
     if( c == ',') 
     {
       tmp_str.erase( tmp_str.begin() ) ; // erase the first character ( the opening quote )
       tmp_str.erase( tmp_str.end() ) ; // erase the lase character ( the closing quote )
    
       cities.push_back(  tmp_str );
       tmp_str.clear();
       
     }
    
    tmp_str = tmp_str + c;
    
    }

    Example usage of those strings :

    Code:
    for(  list< string >::iterator  ii = cities.begin() ; ii != cities.end() ; ii++ )
      cout << *ii  << endl; // print the current city
    Last edited by dempl_dempl; March 14th, 2008 at 03:25 AM.
    http://www.stosha.net/ - Collection of linux widgets and libraries.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •