标准IO相关函数

简介: 标准IO相关函数

一: – fgets( )


#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:从文件中读取内容
参数:    
  s:保存读取到的内容    
  size:每次读取的最大个数    
  stream:文件指针
返回值:    
  成功:读取的数据的首地址   
  失败:NULL   如果文件内容读取完毕,也返回NULL


案例:


#include <stdio.h>#include <string.h>
int main(int argc, char const *argv[]){    
//使用fgets从终端读取数据#if 0    
//注意事项:    
//  如果输入的字符串的长度小于fgets的第二个参数,则会将换行符也当做一个字符保存在第一个参数里面    
//char buf[32] = {0};    
//fgets(buf, sizeof(buf), stdin); //"hello world\n"    
//处理由fgets从终端获取字符串中的\n    
//buf[strlen(buf) - 1] = '\0';
    //  如果输入的字符串的长度大于fgets的第二个参数,则只能读取到第二个参数-1个字符,最后一个位置自动补\0    
    //char buf[32] = {0};    //fgets(buf, 5, stdin);
    //  如果保存fgets读取数据的字符串不够大,则会出现内存溢出的错误,所以保证第一个参数足够大    
    char buf[5] = {0};    
    fgets(buf, 10, stdin);    
    printf("buf = %s\n", buf);#endif
    if(argc < 2)    
    {        
    fprintf(stderr, "Usage: %s filename\n", argv[0]);       
    return -1;    
    }
    //使用fgets从文件中读取数据    
    FILE *fp;    
    if((fp = fopen(argv[1], "r")) == NULL)    
    {        
    perror("fail to fopen");        
    return -1;    
    }
    //注意:如果fgets从文件中读取内容    
    //     1、不管一次读多少个字节,只要遇到每一行的行结束符\n,就会立即结束读取    
    //     2、如果fgets的第二个参数小于一行的内容,只会读取fgets的第二个参数-1个字符,最后一个位置补\0
    #if 0    
    char buf[32] = {};    
    fgets(buf, 8, fp);    
    printf("[%s]\n", buf);
    fgets(buf, 8, fp);    
    printf("[%s]\n", buf);
    #endif
    char buf[32] = {0};    
    while(fgets(buf, sizeof(buf), fp) != NULL)    
    {        
    printf("%s", buf);    
    }
    return 0;
    }


二:-- fputs( )


#include <stdio.h>
int fputs(const char *s, FILE *stream);
功能:向文件写入数据
参数:    
  s:要写入的内容    
  stream:文件指针
返回值:    
  成功:写入文件内容的字节数    
  失败:EOF


案例:


#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[]){
    //使用fputs向终端写入数据    
    //fputs("hello world", stdout);    
    //fflush(stdout);    
    //sleep(10);
    //使用fputs向文件写入数据    
    FILE *fp;    
    if((fp = fopen("file.txt", "w+")) == NULL)    
    {        
    perror("fail to fopen");        
    return -1;    
    }
    fputs("hello world\n", fp);    
    fputs("nihao beijing\n", fp);
    //由于fputs写入内容后改变了文件的偏移量,所以此时文件的偏移量在最后一个字节的下一个位置   
    //所以读取数据是什么也读取不到    
    // char buf[32] = {0};    
    // fgets(buf, 32, fp);    
    // printf("buf = %s\n", buf);
    return 0;}


三:-- fread( )


#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从文件中读取数据
参数:    
  ptr:保存读取的数据    
  size:每次读取的字节数    
  nmemb:一共读取的次数    
  stream:文件指针
返回值:    
  成功:实际读取的次数(对象数、块数)    
  失败:0    如果文件内容读取完毕,返回0


案例:


#include <stdio.h>
typedef struct{    
int a;    
int b;    
char c;    
char d[32];}MSG;
int main(int argc, char const *argv[]){    
//使用fread从文件中读取数据    
FILE *fp;    
if((fp = fopen("file.txt", "r")) == NULL)    
{        
perror("fail to fopen");        
return -1;    }
    // char buf[32] = {0};    
    // fread(buf, 2, 5, fp);    
    // printf("buf = %s\n", buf);        
    // int num;    
    // fread(&num, 4, 1, fp);    
    // printf("num = %d\n", num);
    // int b[4] = {0};    
    // fread(b, 4, 4, fp);    
    // printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]);        
    MSG msg;    
    fread(&msg, sizeof(msg), 1, fp);    
    printf("%d - %d - %c - %s\n", msg.a, msg.b, msg.c, msg.d);        
    return 0;
    }


四:-- fwrite( )


#include <stdio.h>
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:向文件中写入数据
参数:    
  ptr:要写入的数据    
  size:一次写入的字节数    
  nmemb:一共写入的次数    
  stream:文件指针
返回值:    
  成功:实际写入的次数    
  失败:0


案例


#include <stdio.h>
typedef struct{    
int a;    
int b;    
char c;    
char d[32];}MSG;
int main(int argc, char const *argv[])
{    
//使用fwrite向文件写入数据    
FILE *fp;    
if((fp = fopen("file.txt", "w")) == NULL)    
{        
perror("fail to fopen");        
return -1;    
}
    //写入字符串    
    //char buf[] = "1234567890";    
    //fwrite(buf, 2, 5, fp);        
    //写入整数    
    //int a = 97868;    
    //fwrite(&a, 4, 1, fp);
    //写入数组    
    //int a[4] = {100, 200, 300, 400};    
    //fwrite(a, 4, 4, fp);
    //写入结构体    
    MSG msg = {666, 888, 'w', "zhangsan"};    
    fwrite(&msg, sizeof(msg), 1, fp);
    return 0;
    }


五:-- ftell( )


#include <stdio.h>
long ftell(FILE *stream);
功能:获取当前文件的偏移量
参数:    
  stream:文件指针
返回值:    
  获取当前文件的偏移量


六:-- fseek()


#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
功能:设置文件位置指针的偏移量
参数:    
  stream:文件指针    
  offset:偏移量        可正可负也可为0    
  whence:相对位置        SEEK_SET 文件起始位置       SEEK_CUR 文件当前位置      SEEK_END 文件末尾位置(最后一个字符后面一个位置)
返回值:    
  成功:0    
  失败:-1


七:-- rewind( )


#include <stdio.h>
void rewind(FILE *stream);
功能:将文件位置定位到起始位置
参数:    
  stream:文件指针
返回值:无
  rewind(fp) <==> fseek(fp, 0, SEEK_SET);


案例:


#include <stdio.h>
int main(int argc, char const *argv[]){    
//写操作的文件偏移量#if 0    
FILE *fp;    
if((fp = fopen("file.txt", "w")) == NULL)    
{        
perror("fail to fopen");        
return -1;    
}
    fputs("abcdefghijklmnopq", fp);    
    //使用ftell获取文件偏移量    
    printf("offset = %ld\n", ftell(fp));
    //使用fseek修改文件偏移量   
    //rewind(fp);    
    //fseek(fp, 0, SEEK_SET);        
    fseek(fp, -5, SEEK_END);    
    fputs("12345", fp);#endif
#if 0    
FILE *fp;    
if((fp = fopen("file.txt", "r")) == NULL)    
{        
perror("fail to fopen");        
return -1;    
}
    char buf[32] = {0};   
    fgets(buf, 6, fp);    
    printf("buf = %s\n", buf);
    printf("offset = %ld\n", ftell(fp));   
    fseek(fp, 3, SEEK_SET);    
    printf("offset = %ld\n", ftell(fp));
    fgets(buf, 6, fp);    
    printf("buf = %s\n", buf);
    #endif
    FILE *fp;    
    //如果fopen打开或者创建一个文件时使用的是的a或者a+权限    
    //则写操作的偏移量无法改变,只能在文件的末尾位置    
    //但是读操作没有任何影响    
    if((fp = fopen("file.txt", "a+")) == NULL)    
    {        
    perror("fail to fopen");        
    return -1;    
    }
    // char buf[32] = {0};    
    // fgets(buf, 6, fp);    
    // printf("buf = %s\n", buf);
    // printf("offset = %ld\n", ftell(fp));    
    // fseek(fp, 3, SEEK_SET);    
    // printf("offset = %ld\n", ftell(fp));
    // fgets(buf, 6, fp);    
    // printf("buf = %s\n", buf);
    fputs("888888888", fp);    
    printf("offset = %ld\n", ftell(fp));    
    fseek(fp, 3, SEEK_SET);    
    printf("offset = %ld\n", ftell(fp));        
    fputs("666666", fp);
    return 0;}


相关文章
|
3月前
|
存储 监控 Linux
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
523 0
|
9月前
|
Linux API 开发工具
常用的Linux系统的IO函数
常用的Linux系统的IO函数
56 0
|
19天前
|
存储 Java Unix
(八)Java网络编程之IO模型篇-内核Select、Poll、Epoll多路复用函数源码深度历险!
select/poll、epoll这些词汇相信诸位都不陌生,因为在Redis/Nginx/Netty等一些高性能技术栈的底层原理中,大家应该都见过它们的身影,接下来重点讲解这块内容。
|
3月前
|
人工智能 数据挖掘 Python
Python pandas中read_csv函数的io参数
Python pandas中read_csv函数的io参数
51 5
|
3月前
|
存储 编译器 C语言
C语言进阶⑱(文件上篇)(动态通讯录写入文件)(文件指针+IO流+八个输入输出函数)fopen+fclose(下)
C语言进阶⑱(文件上篇)(动态通讯录写入文件)(文件指针+IO流+八个输入输出函数)fopen+fclose
34 0
|
3月前
|
C语言
C语言进阶⑱(文件上篇)(动态通讯录写入文件)(文件指针+IO流+八个输入输出函数)fopen+fclose(中)
C语言进阶⑱(文件上篇)(动态通讯录写入文件)(文件指针+IO流+八个输入输出函数)fopen+fclose
47 0
|
3月前
|
存储 数据库 C语言
C语言进阶⑱(文件上篇)(动态通讯录写入文件)(文件指针+IO流+八个输入输出函数)fopen+fclose(上)
C语言进阶⑱(文件上篇)(动态通讯录写入文件)(文件指针+IO流+八个输入输出函数)fopen+fclose
39 0
|
3月前
|
Linux
Linux io多块读写readv函数和writev函数
fd参数是被操作的目标文件描述符。iov参数的类型是iovec结构数组,该结构体描述一块内存区。iovcnt参数是iov数组的长度,即有多少块内存数据需要从fd读出或写到fd。readv和writev在成功时返回读出/写入fd的字节数,失败则返回-1并设置errno。readv函数将数据从文件描述符读到分散的内存块中,即分散读;writev函数则将多块分散的内存数据一并写入文件描述符中,即集中写。
37 0
|
3月前
|
Linux
Linux网络编程(多路IO复用select函数使用)
Linux网络编程(多路IO复用select函数使用)
48 0
|
3月前
|
API 开发工具 C语言
解决新版本ffmpeg找不到avpriv_io_delete函数等问题
解决新版本ffmpeg找不到avpriv_io_delete函数等问题
57 0