Hi Guys,


I have written the below code but still need to remove the null terminator at the end of each string but don't know how. I've try strtcpy , srtcncpy but doesn't work. Any ideas would be highly appreaciated. thanks.

while (!feof(fp))  {
      fgets(line,LINE_BUF,fp);
      count++;
      strcat(string1,line);
      printf( " %s %d %d\n ", string1, strlen(string1) ,count );
      strcpy(string1,BLANK);
}

harbry.

I don't know why you would want to do that... and as far as I know, all the C string functions automatically copy the terminating NULL charecter, because it's needed to tell the end of the string.

Why exactly do you need to remove the null charecter? Of course, strlen() always returns the length of the string including the null charecter, but if you subtract 1 you get the actual string length.

If you insist on doing it, I think it's possible to write your own function if you manually copy each letter from one string to another... but I still don't think it's a good idea. And of course you'd have to keep track of the string's length yourself, because there's no way of knowing when the string ends...

Hi There,

Don't worry I found the way to do it. See below:

while (!feof(fp)) {
fgets(line,LINE_BUF,fp);
count++;
i=strlen(line);
strncat(string1,line,i-1);
printf( " %s %d %d\n ", string1, strlen(string1) ,count );
strcpy(string1,BLANK);

The reason why I needed to remove the null terminator is I need to load this data into a linklist. I don't much about C so a little help would be handy. Thanks.

That won't work. As I said before, to my knowledge, all C string functions automatically copy the terminating null charecter. Here's a link to what strncat does:
http://www.cplusplus.com/reference/clibrary/cstring/strncat.html

Appends the first num characters of source to destination, plus a terminating null-character.

The reason why I needed to remove the null terminator is I need to load this data into a linklist.

I still don't see why you need to do this. A linked list can quite easily use strings that have null-terminated strings; and removing them would make it harder.

I agree with Joe. You have a buffer. There is a string in it. You can't remove a character from the buffer -- it's part of the buffer. All you can do is overwrite a character. And in this case, overwriting the terminator may simply change "This is a test" to "This is a test ⌂@╝@ Ç☺"

Why would you want to do such a silly thing?

Of course, strlen() always returns the length of the string including the null charecter, but if you subtract 1 you get the actual string length.

Well now thats news to me, since I always thought it returned the number of characters which comprised the string, excluding the null character...;)

Thanks guys for all your replies. Thanks.

Well now thats news to me, since I always thought it returned the number of characters which comprised the string, excluding the null character...;)

Ah, you are correct. I guess it shows when I only use C++ :).

The strlen() function returns the length of str (determined by the number of characters before null termination).

> while (!feof(fp)) {
> fgets(line,LINE_BUF,fp)
Don't use feof() in a control loop
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
Do something like while ( fgets(line,LINE_BUF,fp) != NULL ) Perhaps he means remove the newline which fgets returns.
Which would be

while ( fgets(line,LINE_BUF,fp) != NULL ) {
  char *p = strchr( line, '\n' );
  if ( p != NULL ) *p = '\0';
  // line no longer has a \n
}

Hi ,

I'm trying to get rid of the new line with the code that you passed me but I'm getting a core dump. Basically this is what I'm doing:

while( fgets(line,LINE_BUF,fp) != NULL) {
*p = strchr( line, '\n' );
if ( p != NULL ) *p = '\0';
/*print line no longer has a \n */
Len=strlen(line) ;
}

Any ideas?. thanks

Hi

Hi! :)

I'm trying to get rid of the new line with the code that you passed me but I'm getting a core dump. Basically this is what I'm doing:

while( fgets(line,LINE_BUF,fp) != NULL) {
*p = strchr( line, '\n' );
if ( p != NULL ) *p = '\0';
/*print line no longer has a \n */
Len=strlen(line) ;
}

Any ideas?. thanks

It looks like you declared p previously and didn't initialize it. You're probably getting the core dump from trying to dereference it. Change *p = strchr( line, '\n' ); to p = strchr( line, '\n' ); and all will be well. :)

Hi! :)

It looks like you declared p previously and didn't initialize it. You're probably getting the core dump from trying to dereference it. Change *p = strchr( line, '\n' ); to p = strchr( line, '\n' ); and all will be well. :)

Hi ,

Thanks again. Now it works but now my strcmp() doesnt work anymore. don't know why.

strcmp(current->word,line) == 0

before it was working fine.

Show us some code. Otherwise we have no way of knowing. Perhaps you've taken a newline out of line but not out of current->word ?

Hi ,

Here is my code. Basically what I'm trying to do is loading a linklist with words extracted form a text file.

while( fgets(line,LINE_BUF,fp) != NULL) {
     p = strchr( line, '\n' ); */
     if ( p != NULL )  *p = '\0';  */
     /*print  line no longer has a \n */
      Len=strlen(line) ;
      if ( (newNode=malloc(sizeof(WordLinkType))) == NULL)
      {
         fprintf(stderr,"\nListInsert: Memory Allocation failed! Aborting.\n");
         break;
      }
      current = head;
      previous = NULL;[/B]
[B]      /* Search to find where in insert new list node */
      while (current != NULL )
      {
         previous = current;
         current = current->nextWord;
      }
   if ( Len > 0)  {
      strcpy(newNode->word,line);
      count++;
     printf("word : %s Len: %d Number : %d\n", newNode->word,Len,count);
      }
      newNode->nextWord = current;
      if (previous == NULL)
      {
         head = newNode;
         dictionary->headWords[ALPHABET_LEN]=head;
      }
      else
      {
         previous->nextWord = newNode;
      }
  }

I can't read your code. Please use code tags and proper indentation. Thanks.

I just wodenring does strcmp() needs '\0' at the end of the string or something else. Thanks.

I just wodenring does strcmp() needs '\0' at the end of the string or something else.

The terminating null charecters are ignored. Once the strcmp() finds the \0, it simply stops and returns the difference between the 2 strings.

Hi again,

I jsut wonder if strcmp() expects/needs '\0' or '\b' or '\n' at the end of the string to be able to compare them.

thanks sorry I didn't see you reply before. Thanks.

thanks sorry I didn't see you reply before. Thanks.

Hi ,

Actually I think I believe what the problem is. I think I need to use the same code you gave me to remove the newline I'm retrieving after because I have a option where you can search the dictionary. Since I added the same code is working but I'm getting the below warning and I don't know how to remove it/fix it. Any ideas. thanks. Much much appreciated.


"wordlink_options.c", line 270.8: 1506-068 (W) Operation between types "unsigned char*" and "int" is not allowed.

Which line of code generates that warning? Perhaps it's as simple as casting the types.

Probably the best thing to do if you can't solve the strcmp() error is to use a debugger to check the contents of the strings before comparing them. Then you can see why strcmp() fails, and what you've done wrong.

Which line of code generates that warning? Perhaps it's as simple as casting the types.

Probably the best thing to do if you can't solve the strcmp() error is to use a debugger to check the contents of the strings before comparing them. Then you can see why strcmp() fails, and what you've done wrong.

In the line where I'm getting the warning the code is:

p = strchr( WORD, '\n' );

Now the strcmp() works fine but the warning is now occurring. Thanks.

Did you declare p as an unsigned char? The conversion won't work very good. Declare 'p' as a regular char, and all should go well.

Did you declare p as an unsigned char? The conversion won't work very good. Declare 'p' as a regular char, and all should go well.

I declared p as:


char *p;

Thanks!!.

Did you declare p as an unsigned char? The conversion won't work very good. Declare 'p' as a regular char, and all should go well.

I guess you wanted to say "p should be a char pointer" since strchr returns a character pointer to the first occurance of the character in the string if it was found otherwise a NULL...

Hi,

So if the whole code

char *p;
p = strchr( WORDP1, '\n' );
if ( p != NULL ) *p = '\0';

How can I get rid of the warning. Thanks!!

What warning do you get ? Paste the exact warning and the line number of warning...

This is the warning:

"wordlink_options.c", line 75.7: 1506-068 (W) Operation between types "unsigned char*" and "int" is not allowed.

and the code is:

char *p;

and the full code is basically:
char *p;
p = strchr( WORDP1, '\n' );
if ( p != NULL ) *p = '\0';

The code which you have posted looks good. How have you declared WORDP1 ? If possible post the relevant code snippet ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.