各种的函数

来源:互联网 发布:淘宝买篮球鞋的好店 编辑:程序博客网 时间:2024/05/16 08:17

1. fopen

Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE object whose pointer is returned

FILE * fopen ( const char * filename, const char * mode );

"r"Open a file for reading. The file must exist."w"Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a"Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist."r+"Open a file for update both reading and writing. The file must exist."w+"Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file."a+"Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

2. sscanf() - 从一个字符串中读进与指定格式相符的数据.

Int sscanf( const char *, const char *, ...);

参数可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' | 非%符号}

集合操作

 %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)

 %[aB'] 匹配a、B、'中一员,贪婪性

 %[^a] 匹配非a的任意字符,贪婪性

#include   <stdio.h>  
   
  int   main()  
  {  
          const   char*   s   =   "iios/12DDWDFF@122";  
          char   buf[20];  
   
          sscanf(   s,   "%*[^/]/%[^@]",   buf   );  
          printf(   "%s/n",   buf   );  
   
          return   0;  
  }

 

3. fgets和gets

fgets原型是char *fgets(char *string, int n, FILE *stream);

是c的文件操作函数
负责从文件流中读取字符串
gets则是标准输入流中获取字符串

char *fgets(
  
char* string,
  
int n,
   FILE
*stream
);

char *gets(
  
char* buffer
);

4. mkdir

创建文件夹的命令,直接用字符串形式给出路径和文件名就可以

 

5. strtok

extern char *strtok(char *s, char *delim);

  功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
 
  说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
        strtok在s中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
        返回指向下一个标记串。当没有标记串时则返回空字符NULL。
6. clrscr

清屏

 

7. fwrite

#include<direct.h>
#include<fstream>
#include<string.h>
#include <stdio.h>
main()
{
FILE *fp;
char pathname[80]="D://mkdir1" ;
char name[20];
puts("input name");
gets(name);
mkdir("D://mkdir1");
strcat(pathname,"//");
strcat(pathname,name);
fp=fopen(pathname,"w");
fwrite("111",3,1,fp);    //输入buffer或者数据,字节数,重复数,文件指针
fclose(fp);
return 0;
}

 

8. sprintf

#include<direct.h>#include<fstream>
#include
<string.h>
int main()
{
    FILE
*fp;
   
char name[20],fullpath[260]={0};
    puts(
"input name");
    gets(name);
    mkdir(
"D://mkdir1");
   
    sprintf(fullpath,
"%s%s","D://mkdir1//",name); //格式化字符输出到字符串中
    
    fp
=fopen(fullpath);
   
return 0;
}

原创粉丝点击