how do i parse a custom delimited file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 10
Reputation: Lance Wassing is an unknown quantity at this point 
Solved Threads: 0
Lance Wassing Lance Wassing is offline Offline
Newbie Poster

how do i parse a custom delimited file

 
0
  #1
Apr 2nd, 2007
Hello Everyone,

My question is pretty basic, i hope. Basically, what i have is an extremely large text file, which acts as a flat file database... in essence.

Text file formatting looks like this:
CLIENT CODE | CLIENT NAME| CLIENT ADDRESS 1 ...

This pattern goes on for about 23 fields, including one field per line, and there is about 1100 records in the "flat file database at the moment". My thinking was to use the strtok function, and extract the fields into aarays, for the full 1100 records. so that wrap some functions around it like get next record... etc.

what i need to know is, does anyone know of a fast, and efficient way to extract this data, from the delimited text file, and basically place the data into my gui line by line, as requested by the button click events. If it helps, i am using BCB 6 Professional.

Thankyou for any help
Lance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 10
Reputation: Lance Wassing is an unknown quantity at this point 
Solved Threads: 0
Lance Wassing Lance Wassing is offline Offline
Newbie Poster

Re: how do i parse a custom delimited file

 
0
  #2
Apr 2nd, 2007
Sorry... i meant "including one memo field per line" not "including one field per line"
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how do i parse a custom delimited file

 
0
  #3
Apr 2nd, 2007
I'd suggest you start with the simple obvious thing of using strtok (in C), or using getline() with a delimiter (C++).

File I/O is going to have a lot more impact on how long this is going to take than what you might do in finding and splitting a string based on a delimiter.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: how do i parse a custom delimited file

 
0
  #4
Apr 2nd, 2007
u could memory map the file (unix mmap/mmap64 or windows CreateFileMapping/MapViewOfFile) to reduce the i/o overhead (or use a large file buffer with C/C++ file i/o).
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 10
Reputation: Lance Wassing is an unknown quantity at this point 
Solved Threads: 0
Lance Wassing Lance Wassing is offline Offline
Newbie Poster

Re: how do i parse a custom delimited file

 
0
  #5
Apr 2nd, 2007
Well guys,

I have tried the obvious.

I think I may be doing something a little wrong, or the strtok(str, delims) function is not going to suffice.

The test file i made looks like this
BLUE | GREEN
RED | ORANGE
PURPLE | PINK
YELLOW | BLACK

and the code looks like this:
  1. //Open the file, and copy the file into the buffer
  2. FILE * f;
  3. char * buffer;
  4. long size;
  5. f = fopen("test.txt","rt+");
  6. if( f == NULL) exit(1);
  7.  
  8. fseek(f,0,SEEK_END);
  9. size = ftell(f);
  10. rewind(f);
  11.  
  12. buffer = (char*) malloc(size);
  13. if(buffer == NULL) exit(2);
  14.  
  15. fread(buffer, 1, size, f);
  16. fclose(f);
  17.  
  18. //Now we're going to parse the file
  19. char * temp;
  20. String * Array = new String[5];
  21. temp = strtok(buffer,"|");
  22. RichEdit1->Lines->Add(temp);

unfortunately, the output in the RichEdit1 Box, is BLUE. Does anyone know why? or how i can fix this. If so please... PLEASE... respond. Im a bit stumped here.

Thanks,
Lance
Last edited by WaltP; Apr 2nd, 2007 at 10:02 pm. Reason: Fixed CODE tags -- doesn't anyone know what the PREVIEW button is for???
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how do i parse a custom delimited file

 
0
  #6
Apr 2nd, 2007
http://www.daniweb.com/techtalkforum...cement8-3.html

Well for starters, fread() doesn't append a \0, so the thing you read isn't a proper string.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 10
Reputation: Lance Wassing is an unknown quantity at this point 
Solved Threads: 0
Lance Wassing Lance Wassing is offline Offline
Newbie Poster

Re: how do i parse a custom delimited file

 
0
  #7
Apr 3rd, 2007
Well everyone, i have figured out a way of doing this sort of.

What i have done is created strings, and used the substring function within a huge for loop. This method works better than strtok, mainly because i can't figureout strtok... not quite, but the problem is that, the file must be hard coded intot he executable, but i want to read the file to a string array, and then use the substring function to parse.

I guess my question, and im sorry to ask such a novice question as i am a novice in c, how do i load my file, preferably with the fopen function of stdio.h and stdlib.h, int String arrays?

bassically i would like
  1. String Lines = new String[i]
  2. //Where i is the number of lines in the file
  3. //load the file into the String array so that it //looks like this:
  4. Lines[0] = "Lance , Wassing , 11 Wonderway";
  5. /*
  6. and so on then i can just loop through the array doing the Lines[i].SubString(int, int); function */
if you have something that will work for this purpose, or know i how i can do this, please let me know. Your help is MUCH appreciated!

Thankyou,
Lance
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: how do i parse a custom delimited file

 
0
  #8
Apr 3rd, 2007
fopen(), fgets(), fclose()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC