|
Operating Systems [22c:112] |
| Tutorial 4: Something more about C |
|
In this tutorial (I'm still writing it), I will explore some additional functions and
features of C, that you may want to know when you write your programs.
Extracting data from strings
What do to if you need to extract data from a string? No panic, PERL would have done it,
but C can also be useful. In particular, you need to look at the function sscanf().
This function work in the same way as scanf() but it take the input from a string
instead that from the standard input. The syntax of it's format string (the second
parameter) is the same of the printf (%d for integer, %s for string, %f for float, ...). The
output, will be stored in the 3rd, 4th, 5th, n-th parameters, that have to be pointers.
So if you are extracting a string, you will use the pointer to a memory space that you have
previously reserved using malloc(). If you are extracting an integer, you will use the memory
address of that variable (use the symbol & before the variable).
The common solution
Unfortunatly, the common (and my be the only) solution used to parse and extract relevant informations from a string
in C is a chain of WHILEs and IFs, that scan character by character the string, stop when the
relevant informations start, and copy them inside a buffer until there is another non-relevant
character. I will try to put an example of that as soon as I can.
Example #1: use of sscanf()
The purpose of this example is to demonstrate how to use the sscanf() function to extract
data from text strings. Here the string is declared as an array of characters. Clearly in your
software you can either read this string from a file, or from the user input. I used two unusual
constructions in this example: day and month declared as char, instead that as integer
and str declared as an array of character instead of a pointer to char.
I did the first unusual construction, because I want you to understand that a computer see a
char as a byte. For you a char is a letter, but for a computer, an "A", a space, a dot, and a
digit, are all the same thing: a byte. Bytes goes between 0 to 255. Integer use two bytes of
memory to be represented and goes between 0 to 32767, pointer use 4 bytes of memory. Thus,
since we know that months and days will surely be less than 255, why should you waste a
byte?
Declaring a string as an array of char is a smart idea when it's contents is already defined. You
cannot declare a string that contain the name of the user before that the user write on
the prompt, but if you already know a string, that will be constant for all the program's life,
it's reasonable declare it in this way, and avoid pointers, malloc, and free. In fact, a string is
an array of chars, and str is a pointer to it.
#include <stdio.h>
#include <stdlib.h>
int main() {
char *first, *last, *city, *country;
int year;
char day,month;
char str[] = "Alessio Signorini 08 12 1982 Pisa Italy";
first = (char*) malloc(32); memset(first,0,32);
last = (char*) malloc(32); memset(last,0,32);
city = (char*) malloc(32); memset(city,0,32);
country = (char*) malloc(32); memset(country,0,32);
sscanf(str,"%s %s %d %d %d %s %s",first,last,&month,&day,&year,city,country);
printf("STR: %s\n",str);
printf("%s, %s, %s, %s, %d/%d/%d\n",country,city,first,last,month,day,year);
free(first); free(last); free(city); free(country);
return(0);
}
|