[SOLVED]Question about vectors

Get answers to all your basic programming questions. No Ogre questions, please!
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

[SOLVED]Question about vectors

Post by ranar »

Okay so in making a file updater i need to tell the program where to place dozens of files and folders so my plan was that i would parse a xml file and read the information in a tag. in there it would look like

Code: Select all

FileName Destination
Example
abc.txt /test/new/

or it could look like
abc.txt
/test/new/

with the name of the file on a separate line and than the destination under it.
I have a example code that puts the file in a vector but i dont know how to read every two lines.
read line 1 and 2 place file name which is line1 to destination which is line2 than go on and read lines 3 and 4 do the same till there are no more lines.

here is the code for the example

Code: Select all

#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;
}
Hopefully you understand this i tried to explain it good. long story short i need to pick every two lines (depending on the file format) in a vector or an array i guess if you want to use that and be able to use them to move files.
Last edited by ranar on Sat Nov 23, 2013 9:03 pm, edited 1 time in total.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: Question about vectors

Post by Klaim »

Code: Select all

const int increment = is_two_line_format() ? 2 : 1;
for( std::size_t i = 0; i < line_count; i +=1 ) {
What's wrong with something like that?
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Question about vectors

Post by ranar »

Klaim wrote:

Code: Select all

const int increment = is_two_line_format() ? 2 : 1;
for( std::size_t i = 0; i < line_count; i +=1 ) {
What's wrong with something like that?
So your example did not quite work i dont know what is_two_line_format() ? 2 : 1; is

I also thought that i would explain this better i have a file that list all the names and destinations to move the file. so for example a file that looks like this

Code: Select all

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.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: Question about vectors

Post by Klaim »

My example was pseudo code, not code to copy paste in your project. I didn't understand that the LINES have different format, not the source file.
Anyway, I don't see the problem here, what prevents you from storing even lines on one side and odd lines on the other.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: Question about vectors

Post by Kojack »

Using your existing reading code, here's one way:

Code: Select all

std::size_t line_count = user_vec.size();
    for( std::size_t i = 0; i < line_count; i+=2 ) {
    // user_vec[i] is the filename
    // user_vec[i+1] is the destination
    // do copy stuff here
    }
ranar
Kobold
Posts: 25
Joined: Fri Oct 18, 2013 4:50 am

Re: Question about vectors

Post by ranar »

Kojack wrote:Using your existing reading code, here's one way:

Code: Select all

std::size_t line_count = user_vec.size();
    for( std::size_t i = 0; i < line_count; i+=2 ) {
    // user_vec[i] is the filename
    // user_vec[i+1] is the destination
    // do copy stuff here
    }
Yup that worked it kinda is disappointing how i could not figure that out lol, i just never really worked with vectors much.