C语言笔记

 

函数说明
gcvt() 将浮点型数转换为字符串(四舍五入)
index() 查找字符串并返回首次出现的位置
rindex() 查找字符串并返回最后一次出现的位置
strcasecmp() 判断字符串是否相等(忽略大小写)
strcpy() 复制字符串
strdup() 复制字符串
strncasecmp() 比较字符串的前n个字符

 

http://www.cnblogs.com/Shirlies/p/4282182.html

 

1. 打开文件

int openFile1(void)
{
    FILE *file;
    int ch;
    if((file=fopen("test.txt", "r")) == NULL)
    {
        printf("the file can't be opened!\n");
        exit(1);
    }

     //int fgetc(FILE *stream); 将把由流指针指向的文件中的一个字符读出    
    while((ch=fgetc(file)) != EOF)
    {
        putchar(ch);
        //fputc(ch,stdout);
    }
    
    if(fclose(file) == 0)
    {
        printf("Succeed closed!\n");
    }
    
    //关闭多个文件,fcloseall()
    //fcloseall();
    
    return 0;
}

int openFile2(void)
{
    FILE *file;
    char str[128];
    
    if((file=fopen("test.txt", "r")) == NULL)
    {
        printf("the file can't be opened!\n");
        exit(1);
    }
    
    while(!feof(file))
    {
        if(fgets(str,2,file)!=NULL)
        printf("%s\n", str);
    }
        
    if(fclose(file)!=0) {  
        printf("File cannot be closed\n");   
        exit(1);   
    }   
    else  
        printf("File is now closed\n"); 
}

 

posted @ 2017-04-03 18:55  辛梓  阅读(170)  评论(0编辑  收藏  举报