mardi 3 janvier 2017

Input of multiple text files in 3D Vector

Essential to this problem is, that i am programming on Xcode. I wrote a function that reads in a given amount of text files into my sensor vector: To get the text file Paths i wrote also a function that gives me the filenames inclusive their paths and stores them in a list. The text files contain data that are delimited by a tab and will be stored in excel later on. The problem is they include spaces in their names and Mac has a problem with spaces in file names. I tried to replace the spaces with "\ ". This is what the terminal does with a space when i echo a file with a space in it. I cant open the files to read from them. I appreciate your help.

The path function:

void get_filelist(list<string>& list_in)
{
string full_path;
DIR *dir;
struct dirent *ent;

if((dir = opendir (dir_target.c_str()))!=NULL)
{
    while((ent = readdir (dir)) != NULL)
    {
        if(strstr(ent->d_name, ".txt") && !strstr(ent->d_name, "Summary"))
        {
            full_path = dir_target;
            full_path = full_path + ent->d_name;
            list_in.push_back(full_path);
        }
    }
    closedir(dir);
}
else{
    printf("could not open directory");
    perror("");
}
}

now here is the function that writes into my 3D vector

void fill_vector(list<string> list_in, data_vec& sensors)
{
ifstream myfile;
size_t found;
for(list<string>::iterator it = list_in.begin(); it!= list_in.end(); it++)
{
    string tab = "";
    vector<vector<string> > temp_matrix;
    cout << *it << endl;
    myfile.open(*it);
    if(myfile.is_open())
    {
        vector<string> temp_row;
        while(myfile.is_open())
        {
            getline(myfile, tab, '\t');
            found = tab.find('\n');
            if(found == string::npos) temp_row.push_back(tab);
            else{
                temp_row.push_back(tab.substr(0, found));
                temp_matrix.push_back(temp_row);
                temp_row.clear();
                temp_row.push_back(tab.substr(found+1));
            }
        }
        myfile.close();
    }
    else cout << "unable to open file" ;
    sensors.push_back(temp_matrix);
}
}

Aucun commentaire:

Enregistrer un commentaire