[prog] Strings in C

Diggy Bell diggy at dbsoftdev.com
Fri Oct 18 10:38:20 EST 2002


A couple of quick C topics...

1. Strings are represented in C as an array of characters terminated with
'\0'.  This means that for any string that you use, you will need to account
for an additional character.  If you want to allow 10 characters, you would
define the string as follows:

    char myString[11];

The value "TESTING123" would be stored as:

T, E, S, T, I, N, G, 1, 2, 3, \0

2. Reading user input from stdin doesn't provide a great deal of control.
If this were a production application, fgets wouldn't be the best option,
but that probably isn't a concern here.  But, since you're using fgets, you
might want to use it to limit the number of characters a bit.  The fgets()
call will also properly \0 terminate your string.

    char email[41]; // the final variable for the email address (include 1
character for the \0)
    int email_len; // the length of the string returned

    // print the user message
    printf("Please enter your email address: ");
    // make sure fgets doesn't return NULL
    if(fgets(email, 40, stdin))
    {
        email_len = strlen(email); // will always be 40 or less
    }

Voila!  You should now have a string containing the email address with a
length of 40 or less.

3. When your ready to go to the database, it's time to create some SQL.
Here's the easiest way I can think of at the moment (1st cup of coffee
still...):

    char sqlstring[512]; // this string is often too large, but you gotta
allow for it

    sprintf(sqlstring, "insert into mytable (emailaddr) values ('%s');",
email);

The SQL string is now ready to execute against your PostgreSQL database.

Good luck!

William D. 'Diggy' Bell
Principal
DB Software Development
http://www.dbsoftdev.com

----- Original Message -----
From: "Therese Gustafsson" <th_gustafsson at yahoo.com>
To: <programming at linuxchix.org>
Sent: Friday, October 18, 2002 2:02 AM
Subject: [prog] Strings in C


> Hi!
>
> I'm trying to write a C-program that accesses a
> postgresql-database. I'm having trouble with the
> stringhandling and I'm hoping someone here can help
> me.
>
> I have a short code-example and I'm going to try to
> explain what my problem is.
>
> Here are my variables:
> char email[256],l_email[40];
> int email_length;
>
> This is where the user input is read in.
> printf("Input email-address: ");
> fgets(email, 256, stdin);
>
> Now here comes my problem. The email field in the
> database is only 40 characters long, so now I want to
> make sure that the email string is max 40 characters
> long and I don't want the "\0"-character at the end of
> the string.
>
> Here's what I've done. I get the length of the string
> the user input with strlen. (I don't understand why I
> have to subtract one, but if I don't the stringlength
> is one character to long. I thought strlen didn't
> count the "\0"-character?) Then if the length of the
> user input is longer than the field in the database I
> just truncate it.
> email_length = strlen(email)-1;
> if (email_length > 40)
> {
>         email_length = 40;
> }
>
> Now I copy the user input to another string that's as
> long as the field in the database. This is to copy all
> user input except for the "\0".
> strncpy(l_email,email,email_length);
>
> Problem number 1: If the string the user wrote is less
> then 40 characters the end of the l_email string
> sometimes contains "junk"-characters. How can I get
> rid of those? I want the rest of the string empty.
>
> Problem number 2: Isn't there an easier way to do
> this? It seems as if I've made it to complicated.
>
> I'm sorry if I haven't explained it so you can
> understand. English isn't my first language.
>
> /Therese
>
> __________________________________________________
> Do you Yahoo!?
> Faith Hill - Exclusive Performances, Videos & More
> http://faith.yahoo.com
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://www.linuxchix.org/mailman/listinfo/programming
>




More information about the Programming mailing list