some most useful functions in standard library
<stdio.h>
//1. standard input and output
//1.1 get a value of a character from stdin stream
int getchar();
//1.2 output a char to the screen
void putchar(char);
//1.3 format output s string to the screen
printf("%s, %c, %d, %f, %g ",sval, cval, dval, fval, gval);
//1.4 sends formatted output to a string pointed to, by str.
int sprintf(char *str, const char *format, ...)
//1.5 format get input value
scanf("%s, %c, %d, %f, %g ",&sval, &cval, &dval, &fval, &gval);
//1.6 reads formatted input from a string.
sscanf(line , "%s, %c, %d, %f, %g ",&sval, &cval, &dval, &fval, &gval);
/*1.7 handle line input reads a line from stdin and stores it into the string pointed to by str. It stops when either te newline character is read or when the end-of-file is reached, whichever comes first.*/
char *gets(char *str)
/*1.8 handle output line
writes a string to stdout up to but not including the null character. A newline character is appended to the output.*/
int puts(const char *str)
//2. file input and output
/* open a file and get pointer that handing the file
mode includes : r(read only) w(write) a(append),a "b" must be appended to the mode string if the file is binagy file
fp is NULL if file not exists or error occurs */
FILE *fp;
fp = fopen("filename","mode");
//2.1 get a value of a character from FILE stream
int getc(FILE *fp);
//2.2 output a char to the screen
int putc(int c, FILE *fp);
//2.3 format output s string to the FILE stream
fprintf(FILE *fp, "%s, %c, %d, %f, %g ",sval, cval, dval, fval, gval);
//2.4 format get from file
fscanf(FILE *fp, "%s, %c, %d, %f, %g ",&sval, &cval, &dval, &fval, &gval);
//2.5 close the FILE stream
int flose(FILE *fp);
//2.5 error handing
fprintf(stderr,format, ...);
//2.6 line input and output
char *fgets(char *line, int maxline, FILE *fp);
int fputs(char *line, FILE *fp);
<string.h>
strcat(s,t) concatenate to to the end of s
strncat(s,t,n) concatenate n characters of t to end of s
strcmp(s,t) return negative zero positive for
s < t, s == t, s > t
strcpy(s,t) copy t to s
strncpy(s,t) copy at most n characters of t to s
strlen(s) return length of s without '\0'
strchr(s,c) return pointer to first c in s, or NULL if not present
strrchr(s,c) return pointer to last c in s, or NULL if not present
strstr(str1,str2) /*Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.The matching process does not include the terminating null-characters, but it stops there. */
<ctype.h>
isalpha(c) non-zero if c is alphabetic, 0 if not
isupper(c) non-zero if c is upper case, 0 if not
islower(c) non-zero if c is lower case, 0 if not
isdigit(c) non-zero if c is digit, 0 if not
isalnum(c) non-zero if isalpha(c) or isdigit(c), 0 if not
isspace(c) non-zero if c is blank, tab, newline, return, formfeed, vertical tab ,0 if not
toupper(c) return c converted to upper case
tolower(c) return c converted to lower case
/* The chapter 7 : Input and Output */
#include "stdfs.h"
/* exercise : 7-1 the argument 2 is tolower : conver upper cese to lower the argument 2 is toupper : conver lower case to upper */
void tolower_toupper(char *argv[])
{
int c;
printf("command name is : %s \n", argv[0]);
if (strcmp(*++argv, "tolower") == 0) {
while ((c = getchar()) != EOF)
putchar(tolower(c));
}
else if (strcmp(*argv, "toupper") == 0)
while ((c = getchar()) != EOF)
putchar(toupper(c));
}
/* exercise 7-2 */
#define MAXLINE 100
#define OCTLEN 6
/* count the pos , newline if the pos beyound 100 */
int inc(int pos, int n)
{
if (pos + n < MAXLINE)
return pos+n;
else {
putchar('\n');
return n;
}
}
/*smart_print : print arbitrary input in a sensible way*/
void smart_print()
{
int c, pos;
pos = 0;
while ((c = getchar()) != EOF)
if (iscntrl(c) || c == ' ') { // non-graphic or blank
pos = inc(pos, OCTLEN);
printf("\\%03o ", c); //o : insigned int ;insigned octal number(without a leading zero)
if (c == '\n')
putchar('\n');
} else { // graphic character
pos = inc(pos, 1);
putchar(c);
}
}
#include <stdarg.h> /*for va_start() va_arg() va_end()*/
/* 7-3 minprintf : minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
va_list ap;// point to each unnamed arg
char *p, *sval;
int i, ival;
unsigned uval;
double dval;
char localfmt[10];
va_start(ap, fmt); // make ap point to 1st unnamed arg
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
i = 0;
localfmt[i++] = *p;
while (*(p+1) == '-' || *(p+1) == '.' || isdigit(*(p+1)))
localfmt[i++] = *++p;
localfmt[i++] = *++p; // store the format letter such as : s c d g
localfmt[i] = '\0';
switch (*p) {
case 'd':
case 'i':
ival = va_arg(ap, int);
printf(localfmt, ival);
break;
case 'x':
case 'X':
case 'u':
case 'o':
uval = va_arg(ap, unsigned);
printf(localfmt, uval);
break;
case 'f':
dval = va_arg(ap, double);
printf(localfmt, dval);
break;
case 's':
sval = va_arg(ap, char *);
printf(localfmt, sval);
break;
default:
printf("%s",localfmt);
break;
}
}
va_end(ap);
}
/* 7-4 minscanf : minimal scanf with variable argument list int a; char str[100]; float d; minscanf("%d %s %f ", &a, str, &d); minprintf("minprintf:%d ,%s, %f \n", a, str, d); */
void minscanf(char *fmt, ...)
{
va_list ap;// point to each unnamed arg
char *p, *sval;
int i, *ival;
unsigned *uval;
double *dval;
char localfmt[100];
va_start(ap, fmt); // make ap point to 1st unnamed arg
for (p = fmt; *p; p++) {
if (*p != '%') {
// localfmt[i++] = *p;
continue;
}
i = 0;
localfmt[i++] = *p;
while (*(p+1) == '-' || *(p+1) == '.' || isdigit(*(p+1)))
localfmt[i++] = *++p;
localfmt[i++] = *++p; // store the format letter such as : s c d g
localfmt[i] = '\0';
switch (*p) {
case 'd':
case 'i':
ival = va_arg(ap, int *);
scanf(localfmt, ival);
break;
case 'x':
case 'X':
case 'u':
case 'o':
uval = va_arg(ap, unsigned *);
scanf(localfmt, uval);
break;
case 'f':
dval = va_arg(ap, double *);
scanf(localfmt, dval);
break;
case 's':
sval = va_arg(ap, char *);
scanf(localfmt, sval);
break;
default:
printf("%s",localfmt);
break;
}
}
va_end(ap);
}
/* getop : get next operator or number */
#define NUMBER '0' //signal that a number was found
int getop(char *s)
{
int rc ;
char c;
float f;
while ((rc = scanf("%c", &c)) != EOF)//skip ' ' and \t
if ((s[0] = c) != ' ' && c != '\t')
break;
s[1] = '\0';
if (c == EOF)
return EOF;
if (!isdigit(c) && c != '.')
return c;
ungetc(c, stdin);
scanf("%f",&f);
sprintf(s,"%f",f);
return NUMBER;
}
/* 7-6 : comp : compare two files, printing first different line */
void comp(char *file1, char *file2)
{
char line1[MAXLINE], line2[MAXLINE];
FILE *fp1, *fp2;
char *lp1, *lp2;
fp1 = fopen(file1, "r");
fp2 = fopen(file2, "r");
do {
lp1 = fgets(line1, MAXLINE, fp1);
lp2 = fgets(line2, MAXLINE, fp2);
if (strcmp(line1, line2) != 0) {
printf("the first differnece in lines \n line1 : %s \n line2 : %s", line1, line2);
return;
}else if (lp1 != line1 || lp2 != line2 ) {
printf("file is end lp1 : %s\n line1: %s \n", lp1, line1);
printf("file is end lp2 : %s\n line2: %s \n", lp2, line2);
return ;
}
}while(1);
fclose(fp1);
fclose(fp2);
}
/*7 - 7*/
/*hoption : handing option*/
char ** hoption(int *argc, char *argv[], int *except, int *number)
{
int c;
while (--*argc > 0 && **++argv == '-') {
//printf(" option is %s ,c : %c\n", *argv, *argv[0]);
while (c = *++argv[0])
switch (c) {
case 'x':
//printf(" option is %s ,c : %c\n", *argv, c);
*except = 1;
break;
case 'n':
//printf(" option is %s ,c : %c\n", *argv, c);
*number = 1;
break;
default:
printf("find : illeagl option %c \n", c);
break;
}
}
return argv;
}
/* mathstr : match the string pattern from file of pointer fp */
void matchstr(FILE *fp,char *fname, char *pattern, int except, int number)
{
char line[MAXLINE];
int lineno = 0;
char *lp;
int flag = 1;
while ((fgets(line, MAXLINE, fp)) != NULL) {
lineno++;
if ((strstr(line,pattern) != NULL) != except) {
if (*fname && flag) {
printf("\t The file name is : %s \n", fname);
flag = 0;
}
if (number)
printf("%3d ", lineno);
printf("%s", line);
}
}
}
/* find : print lines that match pattern form 1st argument */
void find(int argc, char *argv[])
{
int c, except = 0, number = 0, found = 0;
char pattern[MAXLINE];
FILE * fp;
argv = hoption(&argc, argv, &except, &number);
if (argc >= 1)
strcpy(pattern,*argv);
else {
printf("usage: ./a.out [-x] [-n] pattern [file ...]\n");
exit(1);
}
if (argc == 1)//standard input
matchstr(stdin, "\0", pattern, except, number);
else while (--argc > 0) {
if ((fp = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "find can't open %s \n", *argv);
exit(1);
} else {
matchstr(fp, *argv, pattern, except, number);
fclose(fp);
}
}
}
/* 7-8 I will write this codes until i understand the topic */
/* 7-9 */
/*save space */
int isupper(int c)
{
if (c >= 'A' && c <= 'Z')
return 1;
else
return 0;
}
/* save time */
#define isupper(c) ((c) >= 'A' && (c) <= 'Z') ? 1 : 0
main(int argc, char *argv[])
{
find(argc, argv);
return 0;
}