龙空技术网

16.C语言-文件操作2

数字双碳行者 55

前言:

现在你们对“2014年3月二级c语言考试”大体比较注重,看官们都需要了解一些“2014年3月二级c语言考试”的相关资讯。那么小编也在网上收集了一些有关“2014年3月二级c语言考试””的相关文章,希望看官们能喜欢,我们快快来学习一下吧!

fprintf与fscanf

int fprintf(FILE *fp,char *format,...)

将格式化的数据写入流

stream

指向一个指针FILE标识输出流的对象。

format

包含要写入流的文本的C字符串。

它可以选择包含嵌入式_格式说明符_这些值将替换为后续附加参数中指定的值,并按要求设置格式。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  char name[100];  f = fopen("d:\\4.txt", "w");  for (int i = 0; i < 3; i++)  {    puts("请输入姓名:");    gets(name);    fprintf(f, "姓名:%d,%10s\n", i + 1, name);  }  fclose(f);}
rewind 函数

C 库函数 *void rewind(FILE stream) 设置文件位置为给定流 stream 的文件的开头。

void rewind(FILE *stream)

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  char str1[10], str2[10], str3[10];  int year;  FILE* fp;  fp = fopen("file.txt", "w+");  fputs("We are in 2014", fp);  rewind(fp);  fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);  printf("Read String1 |%s|\n", str1);  printf("Read String2 |%s|\n", str2);  printf("Read String3 |%s|\n", str3);  printf("Read Integer |%d|\n", year);  fclose(fp);}
ferror函数

测试给定流 stream 的错误标识

int ferror(FILE *stream)

stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。

如果设置了与流关联的错误标识符,该函数返回一个非零值,否则返回一个零值。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  f = fopen("d:\\41.txt", "r");  if (f == NULL) {    printf("读取失败");  }  else {    printf("读取成功");    if (ferror(f) == 0) {      printf("文件正常");    }    else {      printf("文件出错");    }  }  fclose(f);}
perror 函数

把一个描述性错误消息输出到标准错误 stderr。首先输出字符串 str,后跟一个冒号,然后是一个空格。

void perror(const char *str)

str -- 这是 C 字符串,包含了一个自定义消息,将显示在原本的错误消息之前。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  f = fopen("d:\\41.txt", "r");  if (f == NULL) {    //打印出错误    perror("ERROR:");    return -1;  }  fclose(f);}

Error: : No such file or directory

clearerr 函数

清除给定流 stream 的文件结束和错误标识符。

void clearerr(FILE *stream)

stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。

clearerr的作用是使文件错误标志和文件结束标志置为0.假设在调用一个输入输出函数时出现了错误,ferror函数值为一个非零值。在调用clearerr(fp)后,ferror(fp)的值变为0。

rewind函数

设置文件位置为给定流 stream 的文件的开头。

void rewind(FILE *stream)

stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  f = fopen("d:\\1.txt", "r");  //没有到文件结尾就继续  while (!feof(f))  {    char c = fgetc(f);    putchar(c);  }  printf("\n");  rewind(f);//跳到文件头  char str[100] = { 0 };  while (fgets(str,100,f)!=NULL)  {    printf("%s", str);  }  fclose(f);}
ftell

返回给定流 stream 的当前文件位置。

long int ftell(FILE *stream)

stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。

该函数返回位置标识符的当前值。如果发生错误,则返回 -1L,全局变量 errno 被设置为一个正值。

#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  int len;  f = fopen("d:\\1.txt", "r");  fseek(f, 0, SEEK_END);  len = ftell(f);  fclose(f);  printf("文件长度:%d", len);}
fseek 函数

设置流 stream 的文件位置为给定的偏移 offset,参数 offset 意味着从给定的 whence 位置查找的字节数。

int fseek(FILE *stream, long int offset, int whence)

stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。offset -- 这是相对 whence 的偏移量,以字节为单位。whence -- 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一:

常量

描述

SEEK_SET

文件的开头

SEEK_CUR

文件指针的当前位置

SEEK_END

文件的末尾

如果成功,则该函数返回零,否则返回非零值。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  f = fopen("d:\\2.txt", "w+");  fputs("this is test", f);  fseek(f, 5, SEEK_SET);  //从5这个位置开加覆盖  fputs(" hello ", f);  fclose(f);}

在文件尾添加字符

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  f = fopen("d:\\2.txt", "w+");  fputs("this is test", f);  fseek(f, 0, SEEK_END);  //在最后添加  fputs(" hello ", f);  fclose(f);}

取得最后几个字符

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  FILE* f;  f = fopen("d:\\2.txt", "r");  fputs("this is test", f);  //从最后退6个字节  fseek(f, -6, SEEK_END);  while (!feof(f))  {    char c = fgetc(f);    putchar(c);  }  fclose(f);}
remove 函数

删除给定的文件名 filename,以便它不再被访问。

int remove(const char *filename)

filename -- 这是 C 字符串,包含了要被删除的文件名称。

如果成功,则返回零。如果错误,则返回 -1,并设置 errno。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void main() {  char* path = "D:\\1.txt";  remove(path);}
mktemp 函数

char * mktemp(char * template);

mktemp()用来产生唯一的临时文件名。参数template所指的文件名称字符串中最后六个字符必须是XXXXXX。产生后的文件名会借字符串指针返回。

文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。

头文件是io.h。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#include<io.h>void main() {    int fd;    char template[] = "d:\\template-XXXXXX";    fd = _mktemp(template);    printf("template = %s\n", template);}
stat()函数

头文件

#include <sys/stat.h> #include <unistd.h>

int stat(const char * file_name, struct stat *buf);

struct stat{  dev_t st_dev; //device 文件的设备编号  ino_t st_ino; //inode 文件的i-node  mode_t st_mode; //protection 文件的类型和存取的权限  nlink_t st_nlink; //number of hard links 连到该文件的硬连接数目, 刚建立的文件值为1.  uid_t st_uid; //user ID of owner 文件所有者的用户识别码  gid_t st_gid; //group ID of owner 文件所有者的组识别码  dev_t st_rdev; //device type 若此文件为装置设备文件, 则为其设备编号  off_t st_size; //total size, in bytes 文件大小, 以字节计算  unsigned long st_blksize; //blocksize for filesystem I/O 文件系统的I/O 缓冲区大小.  unsigned long st_blocks; //number of blocks allocated 占用文件区块的个数, 每一区块大小为512 个字节.  time_t st_atime; //time of lastaccess 文件最近一次被存取或被执行的时间, 一般只有在用mknod、utime、read、write 与tructate 时改变.  time_t st_mtime; //time of last modification 文件最后一次被修改的时间, 一般只有在用mknod、utime 和write 时才会改变  time_t st_ctime; //time of last change i-node 最近一次被更改的时间, 此参数会在文件所有者、组、权限被更改时更新};
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#include<io.h>#include<sys/stat.h> void main() {  struct stat buf;  stat("d:/4.txt", &buf);  printf("size:%d", buf.st_size);}
mkdir 函数

int mkdir(const char *pathname, mode_t mode);

以mode方式创建一个以pathname为名字的目录,mode定义所创建目录的权限

mode方式:

S_IRWXU

00700权限,代表该文件所有者拥有读,写和执行操作的权限

S_IRUSR(S_IREAD)

00400权限,代表该文件所有者拥有可读的权限

S_IWUSR(S_IWRITE)

00200权限,代表该文件所有者拥有可写的权限

S_IXUSR(S_IEXEC)

00100权限,代表该文件所有者拥有执行的权限

S_IRWXG

00070权限,代表该文件用户组拥有读,写和执行操作的权限

S_IRGRP

00040权限,代表该文件用户组拥有可读的权限

S_IWGRP

00020权限,代表该文件用户组拥有可写的权限

S_IXGRP

00010权限,代表该文件用户组拥有执行的权限

S_IRWXO

00007权限,代表其他用户拥有读,写和执行操作的权限

S_IROTH

00004权限,代表其他用户拥有可读的权限

S_IWOTH

00002权限,代表其他用户拥有可写的权限

S_IXOTH

00001权限,代表其他用户拥有执行的权限

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#include<io.h>#include<sys/stat.h> #include<sys/types.h>void main() {  mkdir("d:\\head", 00700);}

标签: #2014年3月二级c语言考试 #2014安徽省二级c语言 #c语言rewind是什么意思