Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Column Delimited Strings

Status
Not open for further replies.

graetzd

Technical User
Jun 29, 2001
17
US
Does anyone have a good fail-safe method to parse out a text string that is column delimited (i.e. columns 1-4, which may have whitespace, is reserved for a particular variable). I have the following set up to read lines of input data that need to be parsed apart:

Code:
while((GetStatus = fgets(TempString,MAX_CHAR_PER_TREE_RECORD+1,READ)) != NULL)
{
	//Now parse through TempString to extract data out of particular columns

	***WHAT IS MY BEST OPTION HERE 

              ***I tried some sscanf calls but had difficulties

}//end while()

The input data has the following format:

FORTRAN
Columns DataType Format Description
1-4 Integer I4 Plot Identification (i.e., plot numbers)
5-7 Integer I7 Tree identification
8-13 Real F6.0 Tree count
14 Integer I1 Tree history code
15-17 Character A3 Species code
.......more I won't list in this request.

An example file that I have looks like (more columns than I have shown above):

1262 1.061WF 135 13 74 0 0562 198 0 0 02113 92 031
1264 41WF 41 8 28 0 0462 198 0 0 02113 92 031
1266 41WF 34 0 0 0 0362 198 0 0 02113 92 031
1270 201OT 16 0 7 0 14762 198 0 0 02113 92 031
1 0 2201OT 1 0 0 0 00 0 0 0 0 0 02113 92 031
1267 41LP 33 13 17 0 0330 362 195 02113 92 031
1271 201OT 14 0 9 0 26641 162 198 02113 92 031
1 0 601OT 1 0 0 0 00 0 0 0 0 0 02113 92 031
1272 1.061WF 139 19 79 0 0315 1 0 0 0 02113 92 031
1273 1.061WF 170 0 0 0 0315 1 0 0 0 02113 92 031

The WHITESPACES are important and they count towards the width of the column, so that every line has exactly the same # of columns (59).

I am greatly appreciative of any recommendations or tips.

David H. Graetz

 
yeah, thats really easy with the strncpy command,

here goes:

char word[size_of_word];

//this is important
memset(word,NULL, sizeof(word));

stncpy(word, TempString, 4);
This will copy the 1st 4 chars of Tempstring into word.

If u want to copy 12 chars from say 30th column, u do :
strncpy(word, TempString + 30 -1, 12);

There u go
 
strtok() may be a good option here, provided that your fields are separated by just spaces (as opposed to tabs and spaces).

Something like this:

char line[LINE_LEN+1];

char *fld;

for (fld=strtok(line," ");fld;fld=strtok(NULL," ")) {
/* fld points to the current "field" in the line */
}

Be wary of the fact that strtok() modifies its first argument. It works by replacing all occurences of the characters in its 2nd argument with the terminating null character. This probably isn't relevant to your application as you appear to discard the contents of the line on each iteration.
Russ
bobbitts@hotmail.com
 
Many thanks to "the_great" for correctly identifying my problem and giving me a great answer to my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top