Reading in Tab Delimited File (C++)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnnyloot
    New Member
    • Nov 2006
    • 2

    Reading in Tab Delimited File (C++)

    Hello all,
    I have been struggling with this for about two hours now and it has become very frustrating and I cant seem to get it to work.

    I am trying to read in a file which is a dictionary. The file is structured as such,
    Key Meaning
    There is a tab character inbetween them.
    I know the key is a fixed 25 characters and the meaning is a fixed 100 characters.
    I am trying to read the two into seperate char array to assign to a struct.

    I need to while loop though the file reading in both keys and meanings, but they need to be read in the same loop so they can be assigned after that pass.

    I have tried using >>, fgets, read, readsome, getline and I cant seem to get them to work in the form I want.

    I just want to read them in and assing them to a record struct .

    Code:
     
    typedef{
    char key[25];
    char meaning[100];
    } Record;
    Seems simple enough yet I cant seem to mange it.
    Thanks for the help
  • johnnyloot
    New Member
    • Nov 2006
    • 2

    #2
    I somehow solved this though a lot of guess and check, very ineffecient but it had to be done.

    Code:
         FILE* f;
         f = fopen(FILENAME.c_str(), "r");
         char temp[128];
         while(fgets(temp, 128,f)){
              Record r;
              sscanf(temp,"%s%*[\r\t ]%s", r.key,r.meaning);
              cout << r.key << "-" <<  r.meaning << endl;
         }
         fclose(f);
    It worked somehow.
    Very weird situation, dont know why this worked to be honest

    Comment

    Working...