文件输入\输出

fopen
它的第1个参数是待打开文件的名称,更确切地说是一个包含
该文件名的字符串地址。

这里写图片描述

getc()和putc()函数与getchar()和putchar()函数类似
ch = getc(fp);//从指定的文件中获取一个字符
putc(ch, fpout);// 把字符ch放入FILE指针fpout指定的文件中
putc(ch, stdout)与putchar(ch)的作用相同。//stdout作为与标准输出相关联的文件指针
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main() {
  int ch;
  FILE * fp;
  fp = fopen("asd.txt", "r");
  while ((ch = getc(fp)) != EOF) {
    putchar(ch);
  }
  fclose(fp);// 如果成功关闭,fclose()函数返回0,否则返回EOF
  return 0;
}


实列:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define LEN 40
int main() {

  FILE * in, * out;
  //指向输出输入文件的文件指针
  int ch, count = 0;
  //ch存储字符, count标记只读位置为3的倍数的字符 
  char fileName[] = "test.txt";

  if ((in = fopen(fileName, "r")) == NULL) {
    fprintf(stderr, "I couldn't open the file \"%s\"\n", fileName);
  }
  if ((out = fopen("test2.txt", "w")) == NULL) {
    fprintf(stderr, "Can't create output file.\n");
  }else {
    //getc一个一个读取字符 
    //putc一个一个输出字符 
    while ((ch = getc(in)) != EOF) {

      if (count++ % 3 == 0) {
        putchar(ch);

        putc(ch, out);
      }
    }
  }

  fclose(in), fclose(out);
  return 0;
}

文件I/O函数fprintf()和fscanf()函数的工作方式与printf()和scanf()类似,区别在于前者需要用第1个参数指定待处理的文件
rewind()函数的用法
实列:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define MAX 41
int main() {
  //fprintf, fscanf, rewind
  FILE * fp;
  char words[MAX];
  if ((fp = fopen("wordy", "a+")) == NULL) {
    fprintf(stdout, "Can't open \"wordy\" file.\n");
  }
  puts("Enter words to add to the file; press the #");
  puts("key at the beginning of a line to terminate.");
  while ((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#')) {
    //最大读入40个字符
    fprintf(fp, "%s\n", words);
  }
  puts("File contents:");
  rewind(fp);//返回到文件开始出
  while (fscanf(fp, "%s", words) == 1) puts(words);
  puts("Done!");
  if (fclose(fp) != 0) {
    fprintf(stderr, "Error closing file\n");
  }
  return 0;
}
fgets和fputs
fgets(buf, STLEN, fp);
第1个参数和gets()函数一样,也是表示储存输入位置的地址(char * 类型)、第2个参数是一个整数,表示待输入字符串的大小、最后一个参数是文件指针,指定待读取的文件。
fgets()函数读取输入直到第 1 个换行符的后面,或读到文件结尾,或者读取STLEN-1 个字符(以上面的 fgets()为例)。然后,fgets()在末尾添加一个空字符使之成为一个字符串。
字符串的大小是其字符数加上一个空字符。如果fgets()在读到字符上限之前已读完一整行,它会把表示行结尾的换行符放在空字符前面。fgets()函数在遇到EOF时将返回NULL值,可以利用这一机制检查是否到达文件结尾;如果未遇到EOF则之前返回传给它的地址
fputs(buf, fp);
fputs()函数接受两个参数:第1个是字符串的地址;第2个是文件指针。该函数根据传入地址找到的字符串写入指定的文件中。和 puts()函数不同,fputs()在打印字符串时不会在其末尾添加换行符。
fseek()和ftell()
有了 fseek()函数,便可把文件看作是数组,在 fopen()打开的文件中直接移动到任意字节处。我们创建一个程序演示fseek()和ftell()的用法。注意,fseek()有3个参数,返回int类型的值;ftell()函数返回一个long类型的值,表示文件中的当前位置。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define CNTL_Z '\032'// DOS文本文件中的文件结尾标记
#define SLEN 81
//倒序输出文件内容 
int main() {
  char file[SLEN];
  char ch;
  FILE * fp;
  long count, last;
  puts("Enter the name of the file to be processed:");
  scanf("%80s", file);
  if ((fp = fopen(file, "rb")) == NULL) {
    printf("reverse can't open %s\n", file);
  }
  fseek(fp, 0L, SEEK_END);//定位到文件末尾
  last = ftell(fp);
  for (count = 1L; count <= last; count++) {
    fseek(fp, -count, SEEK_END);//回退
    ch = getc(fp);
    if (ch != CNTL_Z && ch != '\r') putchar(ch);//MS-DOS 文件 
  } 
  putchar('\n');
  fclose(fp);

  return 0;
}

fseek()的第2个参数是偏移量(offset)。该参数表示从起始点开始要移动的距离(参见下表列出的起始点模式)。该参数必须是一个long类型的值,可以为正(前移)、负(后移)或0(保持不动)。
fseek()的第3个参数是模式,该参数确定起始点。
fgetpos()和fsetpos()新增了两个处理较大文件的新定位函数

这里写图片描述

一些IO函数
int ungetc()函数把c指定的字符放回输入流中。
int fflush(FILE *fp); 调用fflush()函数引起输出缓冲区中所有的未写入数据被发送到fp指定的输出文件。这个过程称为刷新缓冲区。如果  fp是空指针,所有输出缓冲区都被刷新。在输入流中使用fflush()函数的效果是未定义的。只要最近一次操作不是输入操作,就可以用该函数来更新流(任何读写模式)。
fread()和fwrite()函数:
fread()和 fwrite函数用于以二进制形式处理数据,实际上,所有的数据都是以二进制形式储存的,甚至连字符都以字符码的二进制表示来储存。如果文件中的所有数据都被解释成字符码,则称该文件包含文本数据。如果部分或所有的数据都被解释成二进制形式的数值数据,则称该文件包含二进制数据(另外,用数据表示机器语言指令的文件都是二进制文件)。
size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb,FILE * restrictfp);
fwrite()函数把二进制数据写入文件。size_t是根据标准C类型定义的类型,它是sizeof运算符返回的类型,通常是unsigned  int,但是实现可以选择使用其他类型。指针ptr是待写入数据块的地址。size表示待写入数据块的大小(以字节为单位),nmemb表示待写入数据块的数量。和其他函数一样,fp指定待写入的文件。例如,要保存一个大小为256字节的数据对象(如数组),可以这样做:
char buffer[256];
fwrite(buffer, 256, 1, fp);
size_t fread(void * restrict ptr, size_t size, size_t nmemb,FILE * restrict fp);
fread()函数接受的参数和fwrite()函数相同。在fread()函数中,ptr是待读取文件数据在内存中的地址,fp指定待读取的文件。
double buffer[256];
fread(earnings, 256, 1, fp);
feof()和ferror()函数用于区分这两种情况。当上一次输入调用检测到文件结尾时,feof()函数返回一个非零值,否则返回0。当读或写出现错误,ferror()函数返回一个非零值,否则返回0。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int main() {
  char buff[100] = "123456789";
  char buff2[100];
  double a, b;
  a = 1.0/3.0;
  FILE * fp;
  fp = fopen("test", "a+");
  fwrite(buff, 99, 1, fp);
  fwrite(&a, sizeof(double), 1, fp);
  fclose(fp);
  fp = fopen("test", "r");
  fread(buff2, 99, 1, fp);
  fread(&b, sizeof(double), 1, fp);
  printf("%s\n", buff2);
  printf("%lf\n", b);
  fclose(fp);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/zwhsoul/article/details/80607852