Thread: Question regarding termination of strings

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    31

    Question regarding termination of strings

    Below is part of my code. address[] is an array of 100 characters
    I would like to ask how do I correctly terminate the address[] array so that when i call puts(address) only the current address is shown.
    Because now, whenever I sscanf an address, there is a problem. Even though I have already placed '\n' and '\0' at the end of the array, when I use puts(address) I get the current address followed by remnants of the previous address in the loop.

    Example :
    45 5th Street <----- 1st address
    21 Hell <--- 2nd address
    when I puts(address) I will see
    21 Hell Street
    Even though I placed '\0' at the end of hell.

    Thanks.

    Pier.

    Code:
    while (fgets(buffer, 100, fp)!=NULL) {
           if ((strcmp(buffer, "\n"))!=0) {
               switch (counter) {
                   case 1 :  sscanf(buffer, "Account Id = %d", &accountID);
                             break;
                   case 2 :  sscanf(buffer, "Name = %79c", name);
                             name[strlen(name)] = '\0'; /*null terminate the string*/
                             break;
                   case 3 :  //for (i=0; i <= 100; i++) address[i] = ' '; works when I include this but not veri efficient, i'm sure there's another way
                             sscanf(buffer, "Address = %99c", address);
                             printf("LENGTH = %d\n", strlen(address));
                             address[strlen(address)-1] = '\n';
                             address[strlen(address)] = '\0';
                             break;
                   case 4 :  sscanf(buffer, "DOB = %2c-%2c-%4c", dobDay, dobMth, dobYr);
                             dobDay[2] = '\0';
                             dobMth[2] = '\0';
                             dobYr[4] = '\0';
                             break;
                   case 5 :  sscanf(buffer, "Phone Number = %8c", phoneNo);
                             phoneNo[8] = '\0';
                             break;
                   case 6 :  sscanf(buffer, "Account Balance = %lf", &balance);
                             break;
               }      
    
               if (counter==6) {
                   printf("%d\n", accountID);
                   puts(name); 
                   puts(address);
                   puts(dobDay);
                   puts(dobMth);
                   puts(dobYr);
                   puts(phoneNo);
                   printf("%lf\n", balance);
                   counter = 1; /*reset*/
               }
               else counter++;
               for (i=0; i <= 100; i++) buffer[i] = ' ';
               
               
           }
        }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    char buff[100];
    strcpy(buff, "Hello");

    buff[0] = ' ';
    buff[1] = 0;

    The last two lines are equivalent to:

    strcpy(buff, " ");

    I didn't read your question very thoroughly, but I think this is what you're asking.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>sscanf(buffer, "Address = %99c", address);
    When you sscanf() a string you should use %s not %c (with whatever length modifier you desire).

    Hopefully that will fix you problem, post again if not.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Originally posted by Hammer
    >>sscanf(buffer, "Address = %99c", address);
    When you sscanf() a string you should use %s not %c (with whatever length modifier you desire).

    Hopefully that will fix you problem, post again if not.
    I tried changing the %99c to %s and %99s, but it didn't work. This time the address was not even read in ...

    Pier.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Anyway, is there any short way to clear a string?
    Instead of this :
    for (i=0; i <= 100; i++) address[i] = ' ';

    Pier.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Sebastiani
    char buff[100];
    strcpy(buff, "Hello");

    buff[0] = ' ';
    buff[1] = 0;

    The last two lines are equivalent to:

    strcpy(buff, " ");

    I didn't read your question very thoroughly, but I think this is what you're asking.

    You can also do:

    strcpy(buff, "");

    or simply:

    buff[0] = 0;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I tried changing the %99c to %s and %99s, but it didn't work. This time the address was not even read in ...
    Study this snippet and see if it helps you:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buffer[BUFSIZ] = "Address = My Address";
      char address[BUFSIZ];
      
      sscanf(buffer, "Address = %99s", address);
      
      puts(buffer);
      puts(address);
      
      return(0);
    
    /*
    Program output:
    
    Address = My Address
    My
    
    */
    }

    >>Anyway, is there any short way to clear a string?
    Not really, no.

    >>for (i=0; i <= 100; i++)
    This is wrong, you are writing 101 characters to a 100 bytes array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you want to set the entire length of it (usually not necessary) you can write a function or just use

    memset(buff, 0, length);

    or:

    void
    zero(char * str, int length)
    {
    int i;

    for(i = 0; i < length; ++i)
    {
    str[i] = 0;
    }

    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Precisely.... in my case, I do not know how many elements there are in address. Therefore, when I use %s or %99s it can at most read in the 1st element
    eg
    Address = 103 smith street

    when I puts(address) only 103 will be printed.
    Is there any way to get the whole thing? In the case below, it's "My Address"...

    Pier.
    [i]
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buffer[BUFSIZ] = "Address = My Address";
      char address[BUFSIZ];
      
      sscanf(buffer, "Address = %99s", address);
      
      puts(buffer);
      puts(address);
      
      return(0);
    
    /*
    Program output:
    
    Address = My Address
    My
    
    */
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sigh... If I must.

    Code:
    strncpy( address, buffer, 99 );
    address[99] = 0;
    Is it really that hard to figure out?

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    This is not I really want to do....

    I want to extract the address only (everything after Address = ) and put it in the address array.

    Pier.
    Originally posted by quzah
    Sigh... If I must.

    Code:
    strncpy( address, buffer, 99 );
    address[99] = 0;
    Is it really that hard to figure out?

    Quzah.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by MadStrum!
    This is not I really want to do....

    I want to extract the address only (everything after Address = ) and put it in the address array.

    Pier.
    Use strchr() to get a pointer to the = sign, then increment it, and you have a pointer to the address
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question regarding strings
    By babu in forum C Programming
    Replies: 6
    Last Post: 07-31-2007, 05:30 AM
  2. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  3. Question about IOStream and reading strings from files
    By kingpinzs in forum C++ Programming
    Replies: 22
    Last Post: 12-13-2005, 11:29 AM
  4. Hi everyone..Testing strings and if question
    By tinkerbell20 in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2005, 06:23 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM