C / C++ 读写文件方法

2258阅读 0评论2012-03-06 
分类:C/C++

C 读写文件:
              File *fp;
              fp = fopen(const char* filename, const char* mode);  //mode = "r" "w"  "a" "r+"  "w+"
              int fputc(int a, fp);  //put a char to fp, and return the value of a
              char c = fgetc(fp);  //
              int fseek(fp, int offset, int whence);
              fputs(char *, fp);
              fgets(str, int size, fp);
              fprintf(fp, fmt, arguments);
              fscanf(fp, fmt, &arguments);

               fread(void *ptr, size_t size, size_t n, fp);
               fwrite(const void* ptr, size_t size, n, fp);             
 
C++读写文件:
           读:ifstream if("a.txt");
                  或者
                  ifstream if;
                  if.open("a.txt");
 
                  if>>variable; //空格作为结束符
                  if.getline(string line);
                  getline(if,string line );

           写:ofstream if("a.txt");
                  或者
                  ofstream if;
                  of.open("a.txt");
 
                  of<
上一篇:读经典书计划<持续更新...>
下一篇:C++学习使用中遇到的问题总结