C言語基礎11

8599 ワード

                     ,      
int *values[3];

               

           ,             ,      
int (*p_values)[3];
               
       ,              

FILE                 

fopen          ,       NULL,    FILE        。
        
                ,    
'r' :          ,        
'w' :          ,             
          ,         
'a' :          ,             
             

       '+'  ,     。                。

       'b'  ,     。                    。

fclose          ,     FILE       

fputc               ,       EOF。
fgetc                  ,       EOF。

fread fwrite                     

rewind                   

fseek                   ,       
SEEK_SET  : 0          
SEEK_CUR  : 1              
SEEK_END  : 2          

ftell                          

fgets            。
fputs              。

fprintf                 
fscanf                   

atoi              
atof               
sprintf                    
sscanf                        



/*
	      
*/
#include <stdio.h>
int main(int argc, char *argv[]) {
    int values[] = {6, 3, 7}, loop = 0;
	 int *p_value = NULL;
	 int *p_values[3] = {values, values + 1, values + 2};
    /*p_value = p_values[0];
	 p_values[0] = p_values[1];
	 p_values[1] = p_value;*/
	 p_value = p_values[1];
    p_values[1] = p_values[2];
	 p_values[2] = p_value;

	 p_value = p_values[0];
    p_values[0] = p_values[1];
	 p_values[1] = p_value;
	 for (loop = 0; loop <= 2; loop++) {
        printf("%d ", *p_values[loop]);
	 }
	 printf("
"); return 0; } /* */ #include <stdio.h> #include <stdlib.h> #include <string.h> int compare(const void* p_value, const void* p_value1) { const char** p_str = (const char**)p_value; const char** p_str1 = (const char**)p_value1; return strcmp(*p_str, *p_str1); } int main() { char *str[] = {"China", "France", "England", "Russia", "America"}; int loop = 0; qsort(str, 5, sizeof(char*), compare); for (loop = 0; loop <= 4; loop++) { printf("%s
", str[loop]); } return 0; } /* */ #include <stdio.h> int main() { int values[2][3] = {0}; //int **pp_values = values; int (*p_values)[3] = values; /*printf("%p
", pp_values[0]); printf("%d
", pp_values[0][0]);*/ printf("%d
", values[0][0]); printf("%d
", p_values[0][0]); values[1][1] = 3; printf("%d
", p_values[1][1]); printf("%p, %p
", p_values, p_values + 1); printf("%p, %p
", values, values + 1); return 0; } /* */ #include <stdio.h> int main() { FILE *p_file = fopen("a.txt", "w"); if (p_file) { // fclose(p_file); p_file = NULL; } return 0; } /* */ #include <stdio.h> int main() { FILE *p_file = fopen("a.txt", "w"); if (p_file) { // char str[] = "Hello China!"; int pos = 0; while (str[pos] != 0) { if (EOF == fputc(str[pos], p_file)) { break; } pos++; } fclose(p_file); p_file = NULL; } return 0; } /* */ #include <stdio.h> int main() { FILE *p_file = fopen("a.txt", "r"); if (p_file) { // char ch = 0; while (EOF != (ch = fgetc(p_file))) { printf("%c", ch); } printf("
"); fclose(p_file); p_file = NULL; } return 0; } /* */ #include <stdio.h> int main() { FILE *p_file = fopen("a.txt", "r"); if (p_file) { // char buf[20]; int size = 0; while (EOF != (buf[size] = fgetc(p_file))) { size++; } fclose(p_file); p_file = NULL; p_file = fopen("a.txt", "w"); if (p_file) { size--; while (size >= 0) { if (EOF == fputc(buf[size], p_file)) { break; } size--; } fputc('
', p_file); fclose(p_file); p_file = NULL; } } return 0; } /* */ #include <stdio.h> int main() { int values[] = {1, 2, 3}; FILE *p_file = fopen("a.bin", "wb"); if (p_file) { // int size = fwrite(values, sizeof(int), 3, p_file); printf(" %d
", size); fclose(p_file); p_file = NULL; } return 0; } /* */ #include <stdio.h> int main() { int values[3] = {0}, loop = 0; FILE *p_file = fopen("a.bin", "rb"); if (p_file) { // int size = fread(values, sizeof(int), 3, p_file); printf(" %d
", size); for (loop = 0; loop <= 2; loop++) { printf("%d ", values[loop]); } printf("
"); fclose(p_file); p_file = NULL; } return 0; } /* */ #include <stdio.h> int main() { FILE *p_file = fopen("a.txt", "r"); if (p_file) { // char ch = 0; fseek(p_file, 3, SEEK_SET); ch = fgetc(p_file); printf("%c ", ch); //rewind(p_file); fseek(p_file, 3, SEEK_CUR); ch = fgetc(p_file); printf("%c ", ch); //rewind(p_file); fseek(p_file, -2, SEEK_END); ch = fgetc(p_file); printf("%c ", ch); printf("
"); fclose(p_file); p_file = NULL; } return 0; } /* */ #include <stdio.h> #include <stdarg.h> int max (int cnt, ...) { int ret = 0, loop = 0; va_list v; va_start(v, cnt); for (loop = 0; loop < cnt; loop++) { int value = va_arg(v, int); if (value > ret) { ret = value; } } va_end(v); return ret; } int main() { printf(" %d
", max(3, 6, 32, 17)); printf(" %d
", max(4, 2, 9, 7, 3)); return 0; } static void trace(const char *format, ...){ FILE *fp = NULL; va_list args; fp = fopen("/var/tmp/whj.txt", "a+"); if (fp){ va_start(args, format); vfprintf(fp, format, args); fflush(fp); va_end(args); fclose(fp); } } */ #include <stdio.h> #include <stdlib.h> int main() { char str[20] = {0}; int value = atoi("328"); double d = atof("4.78"); printf("value %d
", value); printf("d %lg
", d); printf("%d %lg
", value, d); sprintf(str, "%d %lg
", value, d); printf("%s", str); value = 0; d = 0.0; sscanf(str, "%d %lg", &value, &d); printf("value %d,d %lg
", value, d); return 0; }