i have a file that list all the names and destinations to move the file. so for example a file that looks like this

def.txt
/names/new/
person.obj
/media/people/
panel.jpg
/random/pictures/

so as you can see here there are three files that need to be moved in a specific destination. How can i read every two lines and get the name of the file in a string, than the destination as another string? There is no telling how many files there might be it could be 10, 50, 12, 200 who knows. I might not even need vectors for this but i was going to parse a xml file with different information in it.

As i said i was going to parse a xml file and the lines inbetween the tags <newfile> would be like above that is what would be in it. Right now it is just a simple txt file. The code below puts the contents in a vector but i dont know how to read it with increments of 2 since every two lines is a new file and destination. here is what i have.

#include <iostream>
#include <string>
#include <vector>
#include <fstream>


void load_users() {
    std::ifstream d_file("users.txt");
    std::string line;
    std::vector<std::string> user_vec;

    while( std::getline( d_file, line ) ) {
        user_vec.push_back( line );
    }

    // To keep it simple we use the index operator interface:
    std::size_t line_count = user_vec.size();
    for( std::size_t i = 0; i < line_count; ++i ) {
        std::cout << i << "    " << user_vec[i] << std::endl;
    }
    // d_file closes automatically if the function is left
}

int main()
{
   load_users();
   system("PAUSE");
   return 0;

Recommended Answers

All 3 Replies

Instead of ++i, use i = i + 2 in your for statement.

Your problem is number 12 in the while loop. The logic was not right.
try this

string data;
static int count=0;
while(getline(d_file,line)){
    data +=line;
    count++;
    if(count==2){
        user_vec.push_back(data);
        count=0;
    }
}

you caould have had the data setup the way you want before inserting into the vector. Its called planning.

Instead of ++i, use i = i + 2 in your for statement.

Yeah that is what i needed for this situation thanks.

thank you richieking also you gave me an idea using that code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.