0
void Lexicon::buildMapFromFile(string filename )  //map
{
    ifstream file;
    file.open(filename.c_str() );
    string wow, mem, key;
    unsigned int x = 0;

    while(true) {
        getline(file, wow);
        if (file.fail()) break; //check for error
        while (x < wow.length() ) {
            if (wow[x] == ',') {
                key = mem;
                mem.clear();
                x++; //step over ','
            } else 
                mem += wow[x++];
        }

        list_map0.put(key, mem); //char to string
        list_map1.put(mem, key); //string to char
        mem.clear(); //reset memory
        x = 0;//reset index
    }
    file.close();
}

This function reads a 2-column csv file and creates a map of column2 with column1 as the key. I compiled with g++ and the file is on the university file share, when I run the program with ./foo the csv files [in the same directory folder as foo] are not read... why?

forest.peterson
  • 755
  • 2
  • 13
  • 30

1 Answers1

2

Perhaps you don't have read permission from that file. Issue the command ls -l <csv_file> see if you have right to read from. For more information on file permissions refer to this link https://help.ubuntu.com/community/FilePermissions

Try the following code works perfect for me

   #include <iostream>
#include <stdio.h>
#include <map>
#include <string>
#include <fstream>
using namespace std;


int main(void )  //map
{
   map<string, string> list_map0;

   map<string, string> list_map1;
    string filename = "csv";
    ifstream file;
    file.open(filename.c_str() );
    string wow, mem, key;
    unsigned int x = 0;

    while(true) {
        getline(file, wow);
        if (file.fail()) break; //check for error
        while (x < wow.length() ) {
            if (wow[x] == ',') {
                key = mem;
                mem.clear();
                x++; //step over ','
            } else
                mem += wow[x++];
        }

        list_map0[key] = mem; //char to string
        list_map1[mem] = key; //string to char
        mem.clear(); //reset memory
        x = 0;//reset index
    }
    printf("%d\n", list_map0.size());
    file.close();
}
hmatar
  • 2,437
  • 2
  • 17
  • 27
  • that compiled in g++ and VS2008 but there is still something going on with the map - I will try some more testing and try to better scope the problem – forest.peterson Jan 11 '13 at 18:55
  • Don't write the < and >, it is just `ls -l filename` – Marc Glisse Jan 11 '13 at 19:02
  • Thanks marks. In Linux community we use those marks to indicate a variable property. – hmatar Jan 11 '13 at 19:04
  • the software developer here thinks he found the problem - a bug in my code parsing for ',' when I mistakenly used ',' for one of the keys, appreciate the help – forest.peterson Jan 11 '13 at 20:49
  • followup - after further checks the unicode , and , appear equal but are difference chars - best guess is that VS2008 can see they are different and the linux does not - rewriting code to use a different approach – forest.peterson Jan 11 '13 at 21:28