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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

truncating strchr() return value

Status
Not open for further replies.

Malachi

Programmer
Aug 13, 2001
38
US
I am using strchr() to read a string from a file. The string consists of a filename followed by a '\t', followed by an integer. The returned string is prefixed with the search character; '\t'. I want to remove the tab from the subsequent returned string, while retaining the filesize integer. Any suggestions? Here is a snippet of the code:

if( (fh_IElist = fopen( ielist, "r" )) != NULL )
{
p_Line = fgets( iefile.p_fname, FILENAME_MAX, fh_IElist );

if( p_Line != NULL )
{
stat( iefile.p_fname, &buf );
printf( "%s%s \n", "File Name is: ", iefile.p_fname );
}
else if( p_Line == NULL )
{
printf( "Cannot fgets file \n" );
}

p_Fname = strchr( iefile.p_fname, '\t' );
printf( "%s%s", "Pointer to first occurance of tab is:", p_Fname );
}

Thanks in advance for help!!
 
It seems to me that your question is how to find the *second* instance of the tab character, rather than repeatedly finding the first instance.

If that's the question, the key is to start your subsequent searches starting with the character after the TAB you previously found. This is just an example, not designed for efficiency, but to make it more clear:

[ignore]
// first I'll set up a sample line of format:
// TAB FILENAME TAB FILESIZE

char sLine[512];
sprintf(sLine, "\t%s\t%i", "Filename.txt", 32000);

// search for first TAB character
char* psTab = strchr(sLine, '\t');

if (psTab != NULL)
{
// first string starts right after first TAB
char* psFileName = psTab + 1;

// overwrite TAB with NULL character
*psTab = '\0';

// search for next TAB character
// USING psFileName, NOT sLine...
psTab = strchr(psFileName, '\t');

if (psTab != NULL)
{
// second string starts right after second TAB
char* psFileSize = psTab + 1;

// overwrite TAB with NULL character
*psTab = '\0';

// then use atoi() to convert psFileSize to numeric...
}
}
[/ignore]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top