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

Newbie Question: How to count pipes in a string / output to file

Status
Not open for further replies.

bigbalbossa

Programmer
Mar 21, 2002
87
US
I have been a perl programmer for years and now trying to teach myself C...seemed like a good idea :)

My exercise for the day is to create a program that:

1) Reads a pipe delimited file.
2) Counts the number of pipes per line
3) Outputs two files, one with the correct number of pipes, one with all others.

I envision this program to accept two command line arguments: a) input file b) number of pipes

I've tried to use sscanf to find pipes, but I can't get any further. Any help is GREATLY appreciated.


 
Try strchr(), which finds characters in strings.

Or just a for loop to step through the string, counting pipe characters as you go.

--
 
And look at <stdio.h> for file based operations.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Use "strcpy ( char * dest, const char * src )" to copy the content pointed by src to dest, and strlen to determine the number of characters in the character pointer before the terminating null-character.

/m
 
msalichos said:
Use "strcpy ( char * dest, const char * src )"
You should always use strncpy() instead of strcpy().
strcpy() is dangerous because you could write more data than the destination can hold.
 
You should always use strncpy() instead of strcpy().
Now, what's the issue with [tt]strncpy[/tt] that you didn't mention -- which also can be dangerous?
 
MSDN said:
If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.
i.e. The size of the destination string should be greater than or equal to the size of the source string. Also don't do anything that is obviously stupid like copying the same string ontop of itself...
 
...and that it does not necessarily null terminate the destination string.

But as for "always use [tt]strncpy[/tt] instead of...", I'd say, "No, avoid [tt]strncpy[/tt] as well.
 
Well according to that site, you can either add the '\0' first and use strncat(), or use strncpy() then add the '\0'. I don't see much difference either way; although I guess strncat() might be a tiny bit faster since it doesn't pad the destination with NULLs.

The problem with strncat() (if you're using it to append rather than overwrite) is that you need to know how big the destination string is and subtract it from the total buffer size, to make sure you have enough room. Otherwise you might do something like this:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
	char dest[10] = "Hello ";

	strncat( dest, "World", 5 ); /* Boom! */
	printf( "%s\n", dest );

	return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top