CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Location
    bucharest, ro
    Posts
    53

    problem with a vector

    Hello,


    I have a file called "test.txt" which contains something like this:

    aaaaaaa, 12312311, zzzzzz, 2312312, asdasdas, 1232312
    ddddddd, 12312, cccccccccccc, 312312312, sdasdasds, 1112
    eqweeqeeqw, 12312311, dasdass, 2312312, asdasdas, 1232312
    ccaascadc, 12312, ccacacacecaeeda, 312312312, sdasdasds, 1112
    eqweeqeeqw, 12312311, dasdass, 2312312, asdasdas, 1232312
    ccaascadc, 12312, ccacacacecaeeda, 312312312, sdasdasds, 1112

    What I need is to create a file called "test2.txt" which contains only selected informations from the file mentioned above. For example it would contain only

    aaaaaaa, zzzzzzzz
    ddddddd, cccccccccccc

    ...and so on, in other words, the destination file must contain only two of the columns from the first file.

    Now, what Ive tried to do to achieve this was to copy all the contents of the first file in a vector<string>, thinking that afterwards I could select and extract only the data I need from that vector.

    Code:
    #include <fstream>
    #include <vector>
    #include <string>
    using namespace std;
    
    void main()
    {
    fstream file1;
    ofstream file2;
    
    string s, s2;
    vector<string> v;
    
    file1.open("C:\\test.txt");
    file2.open("C:\\test2.txt");
    
    while(getline(file1, s2))
    {
      s += s2 + "\n";
    
      v.push_back(s);
    
    }
    
    for (int i=0;i<v.size();i++)
    { file2<< v[i]; }
    
    
    
    file1.close();
    file2.close();
    }
    So now I have a vector that has in it the contents of the first file, but I dont know how to extract what I need from it.

    Thank you in advance for any suggestion.

    Regards,
    the doer alone learneth. (nietzsche)

  2. #2
    Join Date
    Jan 2003
    Location
    Cambridge, UK
    Posts
    752

    Re: problem with a vector

    here is similar question
    Cheers,

    Alex
    Please rate this post if you find it helpful

  3. #3
    Join Date
    Jun 2004
    Location
    bucharest, ro
    Posts
    53

    Re: problem with a vector

    Hello,


    Thank you very much for your help.
    the doer alone learneth. (nietzsche)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured