Chinaunix首页 | 论坛 | 博客
  • 博客访问: 513184
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: WINDOWS

2010-01-04 15:42:07

CIO

1.      动态内存分配

Mallocfree函数:

Void* malloc(size_t size);

分配size字节大小的内存, 返回指向内存首地址的指针。

Void free(void* p);

释放由malloc, calloc, realloc分配的地址。

#include

#include

#include

#define BUFSIZE 20

 

int main() {

       char* buffer;

       char str[] = "hello world";

 

       buffer = malloc(BUFSIZE * sizeof(char));

       memcpy(buffer, str, sizeof(str));

       printf("%s\n", buffer);

       free(buffer);

}

Reslut: hello world

 

Callocrealloc:

Void* calloc(size_t num, size_t ele_size);

Void* realloc(void* ptr, size_t new_size);

 

#include

#include

 

int main() {

       int* pInt;

       int* temp;

       int i;

 

       pInt = (int*)calloc(10, sizeof(int));

       temp = pInt;

      

       if(pInt == NULL)

              exit(1);

       for(i = 0; i < 10; i++) {

              *pInt = i * i;

              pInt++;

       }

       pInt = temp;

       for(i = 0; i < 10; i++)

              printf("the value is %d\n", *pInt++);

       pInt = temp;

       free(pInt);

       return 0;

}

 

Free的参数一定要是刚开始分配时的地址,如果--或者++后再free就会出大问题了!!!!!!!!

 

2.      CIO

A.    专用于标准输入与输出

(1)   getchar putchar

int getchar(void)

int putchar(int character)

 

#include

#include

 

int main() {

       char c;

       for(;(c = getchar()) != '\n';)

              putchar(c);

}

这是一个非常简单的例子。

(2)   gets puts

char* gets(char* str);

int puts(const char* str);

这两个函数有些特殊的地方。

一般Getsstdin读入,当我们按回国换行后停止, ‘\n’键并不包括在*str中,且一定要注意str的大小, 可能会出现溢出这样的情况, 比如为str分配的内存太小。

 

puts在输出时会自动在str后面多加一个’\n’ 这点要注意!!!!!!!!

 

#include

#include

 

int main() {

       char* pStr;

       pStr = malloc(50);

       if(pStr == NULL)

              exit(1);

       gets(pStr);

       puts(pStr);

}

 

(3)   printfscanf, 这两个函数最经典了, 但我觉得它们最不好用的……

int printf ( const char * format, ... );
int  scanf ( const char * format, ... );
这是两个可变参数的函数。
 
#include 
#include 
 
int main() {
   float pi = 3.14159;
   double pii = 3.1415926;
   printf("%f\n", pi);
   printf("%10f\n", pi);
   printf("%10.4f\n", pi);
   printf("%+10.4f\n", pi);
   printf("%010f\n", pi);
   printf("%-10f\n", pi);
   
   printf("%f\n", pi);
}
Result: 
 

我们可以看到printf的用法了吧, 有点复杂了,呵呵,这只是一部分了, 我觉得了解常见的差不多的。

#include

#include

 

int main() {

       float pi;

       int ii;

       char str[50];

       double pii;

 

       puts("please input a float number and an int:");

       scanf("%f%d", &pi,&ii);

       printf("your input is %f and %d\n", pi, ii);

       puts("please input a string:");

       scanf("%s", str);

       printf("your input is %s\n", str);

puts("input a double:");

// 小心!!!!!!!!!!!!!!!!!!!!

       scanf("%lf", &pii);

       printf("your input is %f\n", pii);

}

了解一些基本的用法就好, 没必要钻牛角尖!!!!!!

 

B.用于文件的IO库函数:

(1) fgetc, getc, fputc, putc

Getcputc为宏定义, 建议不用.

Int fgetc(FILE* stream);

Int fputs(int character, FILE* stream);

 

#include

#include

 

int main() {

       FILE* fp;

       char ch;

       fp = fopen("my.txt", "r");

       if(fp == NULL) {

              perror("can not open file");

              exit(1);

       }

       while((ch = fgetc(fp)) != EOF)

              putchar(ch);

}

当处于文件结尾时,fgetc返回的是一个EOF的常量!!!!!!!!!!!!!!!!

 

#include

 

int main() {

       FILE* fp;

       char ch;

   

       fp = fopen("alpha.txt", "w");

       if(fp == NULL) {

              perror("alpha.txt");

              exit(1);

       }

    for(ch = 'a'; ch <= 'z'; ch++)

              fputc((int)ch, fp);

       fclose(fp);

}

 

B.     fgets, fputs使用

char* fgets(char* str, int num, FILE* stream);

int fputs(const char* str, FILE* stream);

fgets看起来复杂些,我们可以把它与gets来比较:

fgets用于文件流,gets用于标准输入流,gets只有一个char*的参数, 用于保存读取的字符串,并且去掉’\n’, ’\0’, fgets从文件中读取,最多读取num – 1, 如果读取到了’\n’, 不会删除而保留, fgets也在后面加上’\0’

#include

 

int main() {

       FILE* fp;

       char* str = (char*)malloc(50);

 

       fp = fopen("io.c", "r");

       if(fp == NULL) {

              perror("error");

              exit(1);

       }

    while(fgets(str, 50, fp))

              // we can't use puts here, remember?

              printf("%s", str);

       fclose(fp);

}

 

Fputs就比较简单, *str写进文件流, 不包括’\0’的。

#include

#include

 

int main() {

       FILE* fp;

       char str[50];

       int i;

       // warning: this step is necessary!!!!!!!

       memset(str, '\0', 50);

       for(i = 'a'; i <= 'z'; i++)

              str[i - 'a'] = i;

       fp = fopen("fputs.txt", "w");

       if(fp == NULL) {

              perror("error");

              exit(1);

       }

      

       for(i = 0; i < 10; i++) {

              fputs(str, fp);

              fputc('\n', fp);

             

       }

       fclose(fp);

}

 

C fscanf fprintf

Int fprintf(FILE* stream, const char* format, …);

Int fscanf(FILE* stream, const char* format, …);

 

#include

#include

 

int main() {

       FILE* fp;

       char* pStr = "hello world";

       int i;

      

       fp = fopen("fprintf.txt", "w");

       if(fp == NULL) {

              perror("error");

              exit(1);

       }

    for(i = 0; i < 10; i++) {

              fprintf(fp, "%20s", pStr);

              fputc('\n', fp);

       }

}

 

 

C.     fread, fwrite

size_t fread(void* ptr, size_t elements, size_t count, FILE* stream);

size_t fwrite(void* ptr, size_t elements, size_t count, FILE* stream); 

阅读(709) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~