int main(void) { char label[20]; char name[20]; int entries = 0; int loop, age; double salary;
strUCt Entry_struct
{ char name[20]; int age; float salary; } entry[20];
/* Input a label as a string of characters restricting to 20 characters */ printf("/n/nPlease enter a label for the chart: "); scanf("%20s", label); fflush(stdin); /* flush the input stream in case of bad input */
/* Input number of entries as an integer */ printf("How many entries will there be? (less than 20) "); scanf("%d", &entries); fflush(stdin); /* flush the input stream in case of bad input */
/* input a name restricting input to only letters upper or lower case */ for (loop=0;loop<entries;++loop) { printf("Entry %d/n", loop); printf(" Name : "); scanf("%[A-Za-z]", entry[loop].name); fflush(stdin); /* flush the input stream in case of bad input */
/* input an age as an integer */ printf(" Age : "); scanf("%d", &entry[loop].age); fflush(stdin); /* flush the input stream in case of bad input */
/* input a salary as a float */ printf(" Salary : "); scanf("%f", &entry[loop].salary); fflush(stdin); /* flush the input stream in case of bad input */ }
/* Input a name, age and salary as a string, integer, and double */
printf("/nPlease enter your name, age and salary/n"); scanf("%20s %d %lf", name, &age, &salary);
/* Print out the data that was input */ printf("/n/nTable %s/n",label); printf("Compiled by %s age %d $%15.2lf/n", name, age, salary); printf("-----------------------------------------------------/n"); for (loop=0;loop<entries;++loop) printf("%4d %-20s %5d %15.2lf/n", loop + 1, entry[loop].name, entry[loop].age, entry[loop].salary); printf("-----------------------------------------------------/n"); return 0; }
函数名: stat 功 能: 读取打开文件信息 用 法: int stat(char *pathname, struct stat *buff); 程序例:
int main(void) { struct stat statbuf; FILE *stream;
/* open a file for update */ if ((stream = fopen(FILENAME, "w+")) == NULL) { fprintf(stderr, "Cannot open output file./n"); return(1); }
/* get information about the file */ stat(FILENAME, &statbuf);
fclose(stream);
/* display the information returned */ if (statbuf.st_mode & S_IFCHR) printf("Handle refers to a device./n"); if (statbuf.st_mode & S_IFREG) printf("Handle refers to an ordinary file./n"); if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file./n"); if (statbuf.st_mode & S_IWRITE) printf("User has write permission on file./n");
printf("Drive letter of file: %c/n", 'A'+statbuf.st_dev); printf("Size of file in bytes: %ld/n", statbuf.st_size); printf("Time file last opened: %s/n", ctime(&statbuf.st_ctime)); return 0; }
strcpy(string, "This is a string"); ptr = strchr(string, c); if (ptr) printf("The character %c is at position: %d/n", c, ptr-string); else printf("The character was not found/n"); return 0; }
int main(void) { char string[15]; char *ptr, c = 'r';
strcpy(string, "This is a string"); ptr = strrchr(string, c); if (ptr) printf("The character %c is at position: %d/n", c, ptr-string); else printf("The character was not found/n"); return 0; }
int main(void) { char input[16] = "abc,d"; char *p;
/* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, ","); if (p) printf("%s/n", p);
/* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, ","); if (p) printf("%s/n", p); return 0; }
函数名: strtol 功 能: 将串转换为长整数 用 法: long strtol(char *str, char **endptr, int base); 程序例:
#include <stdlib.h> #include <stdio.h>
int main(void) { char *string = "87654321", *endptr; long lnumber;
/* strtol converts string to long integer */ lnumber = strtol(string, &endptr, 10); printf("string = %s long = %ld/n", string, lnumber);