By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

Reading in Tab Delimited File (C++)

P: 2
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 .

Expand|Select|Wrap|Line Numbers
  2. typedef{
  3. char key[25];
  4. char meaning[100];
  5. } Record;
Seems simple enough yet I cant seem to mange it.
Thanks for the help
Nov 11 '06 #1
Share this Question
Share on Google+
1 Reply

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

Expand|Select|Wrap|Line Numbers
  2.      FILE* f;
  3.      f = fopen(FILENAME.c_str(), "r");
  4.      char temp[128];
  5.      while(fgets(temp, 128,f)){
  6.           Record r;
  7.           sscanf(temp,"%s%*[\r\t ]%s", r.key,r.meaning);
  8.           cout << r.key << "-" <<  r.meaning << endl;
  9.      }
  10.      fclose(f);
It worked somehow.
Very weird situation, dont know why this worked to be honest
Nov 11 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.