length_of_my_string+1


 
Thread Tools Search this Thread
Top Forums Programming length_of_my_string+1
# 1  
Old 05-31-2005
length_of_my_string+1

my problem is quite simple;
with printf("%d %s",strlen(tmp[i]),tmp[i]);
im getting length_of_my_string+1
i.e if it reads "ls" from the file, it prints 3 instead of 2;
any solution?

Code:
	char tmp[nLINES][LINE_LENGTH];
		
	FILE *stream = fopen( config_file , "r" );
	if ( stream == NULL ) {
		fprintf (stdout , "fopen %s failed" , *config_file );
		return;
	}
	
	while( (int *) fgets( tmp[i] , LINE_LENGTH , stream ) != NULL ){		                printf("%d %s",strlen(tmp[i]),tmp[i]);
		i++;
	}
	fclose (stream);

# 2  
Old 06-01-2005
Quote from man fgets:
Quote:
The fgets() function reads at most one less than the number of characters
specified by size from the given stream and stores them in the string
str. Reading stops when a newline character is found, at end-of-file or
error. The newline, if any, is retained.
You are getting a newline character at the end of the line, and, it is retained. i.e. ls is stored as tmp[0][0]=l tmp[0][1]=s and tmp[0][2]='\n'
# 3  
Old 06-01-2005
is there any way or function to read word by word and stored them in an array?
# 4  
Old 06-02-2005
Here's a solution:

if(tmp[i][strlen(tmp[i])-1]=='\n') {
tmp[i][strlen(tmp[i])-1]='\0';
}

This will strip the trailing '\n' character.
# 5  
Old 06-25-2005
is there any way or function to read word by word and stored them in an array?

There is simple way if you use a pointer variable in it.

for instance,

void main()
{
char *ch,ch1[];
int i=0;
scanf("%s",ch)

while (*ch!='\o')
{
ch1[i++]=*ch;
*ch++
}

.............
............

this might be solve your problem. Try it revert me the feedback

All the best.

regards

senthil K
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question