Linux-C应用开发-标准IO

标准IO

IO:Input & Output

IO是一切操作的基础,IO分为
  • 标准IO (stdio)
  • 系统调用IO (sysio) 文件IO

两者都可以使用的情况下优先使用标准IO,标准IO依赖于系统调用IO

sdio 标准IO函数
fopen:(打开文件流)

函数原型:

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

path : 指定打开的文件路径

mode:打开文件的方式

​ the argument mode points to a string beginning with one of the following sequences.

  • r : Open text file for reading.The stream is positioned at the beginning of the file.
  • r+ : Open for reading and writing.The stream is position at the beginning of the file.
  • w : Truncate file to zero length or create text file for writing.The stream is positioned at the beginning of the file.
  • w+ : Open for reading and writing.The file is created if it does not exist,ohterwise it is truncated.The stream is positioned at the beginning of the file.
  • a : Open for appending (writing at end of file).The file is created if it does not exist.The stream is positioned at the end of the file.
  • a+ : Open for reading and appending (writing at end of file).The file is created if it does not exits.The initial file position for reading is at the beginning of the file,but output is always appended to the end of the file.

返回值说明:

successful return a FILE pointer. Otherwirse, NULL is returned and errno is set to indicate the error.

fclose:(关闭文件流)

函数原型:

int fclose(FILE * fp);

返回值说明:

Upon successful completion 0 is returned.Otherwirse EOF is returned and errno is set to indicate the error .

Linux 一个进程中最大打开文件个数为1024

fgetc:(从流中获取单个字符)

函数原型:

int fgetc(FILE *stream);

函数说明:

reads the next character from stream and returns it as an unsigned char cast to an int , or EOF on end of file or error.

返回值说明:

return the character read as an unsigned char cast to an int or EOF on end of file or error.

fputc:(将字符c写入文件流中)

函数原型:

int fputc(int c,FILE* stream);

函数说明:

writers the character c, cast to an unsigend char, to stream.

返回值说明:

return the character written as an unsigned char cast to an int or EOF on error.


实现mycpy,将srcfile内容拷贝到destfile中

#include<stdio.h>

int main(int argc,char** argv){
    if(argc < 3){
        fprintf(stderr,"Usage : %s <source_file> <dest_file>",argv[0]);
        return -1;
    }
	FILE *fps = fopen(argv[1],"r");
    if(fps == NULL){
		perror("fopen() fps");
        return -1;
    }
    FILE *fpd = fopen(argv[2],"w");
    if(fpd == NULL){
        fclose(fps);
		perror("fopen() fpd");
        return -1;        
    }
    
    while(1){
        char ch = fgetc(fps);
        if(ch == EOF) break;
        fputc(ch,fpd);
    }
    
    fclose(fps);
    fclose(fpd);
    return 0;
}

fgets:(从指定流中获取size个字符)

函数原型:

char* fgets(char *s,int size,FILE *stream);

函数说明:

reads in at most on less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline.If a newline is read,it is storedinto the buffer.A terminating null byte(‘\0’) is stored after the last character in the buffer.

返回值说明:

return s on success,and NULL on error or when end of file occurs while no characters have been read.

结束情况:

  • 读取到了size - 1个字节(最后一个字节用于添加 ‘\0’ )
  • 读取到了 ‘\n’
fputs:(将指定串输入到指定流)

函数原型:

int fputs(const char *s,FILE *stream);

函数说明:

writes the string s to stream,without its terminating null byte(‘\0’).

返回值说明:

return a nonnegative number on success,or EOF on error.

fread:(在指定流中读取nmemb个size大小的数据存储到ptr中)

函数原型:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

函数说明:

reads nmemb elements of data,each size bytes long,from the stream poninted to by stream,storing them at the location given by ptr.

返回值说明:

return the number of items read or wirtten.If an error occurs, or the end of the file is reached,the return value is a short item count (or zero).

fwrite:(向指定流中写入nmemb个size大小的数据)

函数原型:

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *steam);

函数说明:

writes nmemb elements of data,each size bytes long,to the stream pointed to by stream, obtaining them from the location given by ptr.

返回值说明:

return the number of items read or wirtten.If an error occurs,or the end of the file is reached,the return value is a short item count (or zero).

printf (族)

函数原型:

int printf(const char* format, ..... );

int fprintf(FILE* stream, const char* format, .....);

int sprintf(char* str, const char* format, ..... );

int snprintf(char* str, size_t size, const char* format, .... );

scanf(族)

函数原型:

int scanf(const char* format, ....);

int fscanf(FILE* stream, const char* format, ....);

int sscanf(const char *str, const char* format, ....);

fseek(定位文件文件位置指针)

函数原型:

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

函数描述:

sets the file position indicator for the stream pointed to by stream.The new position, measured in bytes,is obtained by adding offset bytes to the position specifiled by whence. If whence is set SEEK_SET,SEEK_CUR, or SEEK_END, the offset is realtive to the start of the file, the current position indicator,or end-of-file,respectively.

返回值说明:

successful return 0.Otherwise, -1 is returned and errno is set to indicate the error.

ftell(返回当前文件位置指针的位置)

函数原型:

long ftell(FILE* stream);

函数描述:

obtain the current value of the file position indicator for the stream pointed to by steam.

返回值说明:

return the current offset.Otherwise, -1 is returned and errno is set to indicate the error.

rewind(文件位置指针返回文件开头位置)

函数原型:

void rewind(FILE* stream);

函数描述:

sets the file position indicator for the stream pointed to by stream to the beginning of the file.Is is equivalent to:

void fseek(stream,0L,SEEK_SET);

fflush(刷新流)

函数原型:

int fflush(FILE* stream);

返回值说明:

Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error

缓冲区

作用:合并系统调用

  • 行缓冲:换行时刷新、满了的时候刷新、强制刷新,如stdio,因为是终端设备
  • 全缓冲:满了的时候刷新、强制刷新(默认情况,除了终端设备)
  • 无缓冲:需要立刻输出的内容,如stderr

setvbuf函数用于设置文件缓冲类型


实现getline函数功能
ssize_t myGetline(char **lineptr,size_t *n,FILE* stream){
    
    char* buf = *lineptr;
    char c = ' ';
    int index = 0;
    if(buf == NULL || *n == 0){
        *lineptr = malloc(sizeof(char) * 10);
        if(*lineptr == NULL) return -1;
        buf = *lineptr;
        *n = 10;
    }
    
    while((c = fgetc(stream)) != '\n'){
        if(c == EOF){
            return -1;
        }
        if(index < *n - 2){
            buf[index++] = c;
        }else{
            *n += 10;
         	buf = realloc(buf,*n);
            buf[index++] = c;
        }
    }
    buf[index++] = '\n';
    buf[index] = '\0';
    *lineptr = buf;
    return index;
}

临时文件:
  • 如何创建临时文件
  • 如何创建临时文件不冲突
  • 如何销毁临时文件
tmpnam

函数原型:

char* tmpnam(char* s);

返回值说明:

The function returns a pointer to a unique temporary filenaem, or NULL if a unique name cannot by generated.

tmpfile

函数原型:

FILE* tmpfile(void);

函数说明:

The function opens a unique temporary file in binary read/write (w+b) mode. The file will be automatically deleted when it is closed and the program terminates.

返回值说明:

The function returns a stream descriptor, or NULL if a unique filename cannot by generated or the unique file cannot be opened.In the latter case, errno is set to indicate the error.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 根据所提供的华清远见-c0rtexas9资料路径,在实验中涉及到了Linux系统标准IO的应用实验。Linux系统标准IO是指Linux操作系统提供的一套标准化的IO库,可以用于操作文件、输入输出等。以下是对该实验的简单描述: 这个实验主要是关于Linux系统标准IO的应用实验。在该实验中,我们可以找到程序的源代码,该源代码是用Linux应用程序的方式编写的。通过这个实验,我们可以了解和学习Linux系统标准IO的基本操作和使用方法。 Linux系统标准IO是一套非常强大和灵活的IO库,提供了一系列的函数和方法来操作文件和进行输入输出。在实验源码中,我们可以看到一些常见的标准IO函数,比如fopen、fclose、fprintf和fscanf等。 通过这些函数,我们可以打开和关闭文件,从文件读取数据或向文件写入数据。实验源码中包含了一些常见的操作,比如读取文件、写入文件、复制文件和查找文件等。通过实验,我们可以了解到如何使用这些函数来完成不同的操作。 此外,在实验源码中可能还会涉及到一些其他的Linux系统标准IO相关的知识点,比如文件指针、文件描述符和文件流等。这些知识点对于理解和使用Linux系统标准IO函数非常重要。 通过实验中提供的源代码,我们可以进行相应的实验操作,从而更好地理解和学习Linux系统标准IO的使用。同时,我们也可以通过对实验源码的分析和调试,更深入地了解Linux系统标准IO的内部工作原理。 总之,通过这个实验,我们可以掌握Linux系统标准IO的基本操作和使用方法,从而提高我们在Linux系统中进行文件操作和输入输出的能力。 ### 回答2: 华清远见(英文名称为Far Infrared Technologies International Limited)是一家专注于红外光学技术研发及应用的企业。该公司的产品主要应用于安防监控、无人机、机器人、智能家居等领域。 关于"06. linux系统标准io实验"的实验资料包括了程序源码。在Linux操作系统中,标准I/O(Input/Output)库是用于进行文件读写和输入输出操作的一套API(Application Programming Interface)。标准I/O库提供了一系列函数,包括文件打开、读取、写入、关闭等操作,方便开发人员进行文件操作和输入输出。 这份实验资料中的源码,应该是用于展示和实践Linux系统标准I/O库的使用方法和技巧。通过编译和运行这些源码,可以了解如何使用标准I/O库来进行文件的读写和输入输出操作,包括打开文件、读取文件内容、写入文件等。 这些实验源码可以作为学习和实践Linux系统标准I/O库的参考资料。通过实际操作,可以加深对标准I/O库的理解和掌握,提高在Linux环境下进行文件操作和输入输出的能力。 总之,"06. linux系统标准io实验"的实验资料中的程序源码是用于学习和实践Linux系统标准I/O库的使用方法,通过编译和运行这些源码,可以加深对标准I/O库的理解和掌握,提高在Linux环境下进行文件操作和输入输出的能力。 ### 回答3: 华清远见-c0rtexa9资料中的程序源码目录为\linux应用实验源码\06. linux系统标准io实验\。这个实验中包含了一些关于Linux系统标准输入输出的代码示例。 在Linux中,标准输入输出是三个预先定义好的文件描述符,分别是0(stdin),1(stdout),2(stderr)。这些文件描述符与输入输出设备关联,比如键盘和屏幕。使用标准输入输出可以实现用户与程序之间的交互以及程序的输出显示。 在实验源码中,我们可以看到许多用于标准输入输出的函数,包括printf、scanf、getchar、putchar等。这些函数可以帮助我们进行输入输出操作。 在实验中,我们可以通过这些代码示例学习如何从标准输入读取用户输入,如何向标准输出显示结果。例如,可以使用scanf函数读取用户输入的数据,使用printf函数将处理结果输出显示。 通过实验中的代码示例,我们可以学习到如何处理标准输入输出的错误,如何进行格式化输入输出,如何使用缓冲区进行高效的输入输出等。 总之,华清远见-c0rtexa9资料中的\linux应用实验源码\06. linux系统标准io实验\中包含了一些关于Linux系统标准输入输出的代码示例,通过这些示例我们可以学习到如何进行标准输入输出操作,以及一些与之相关的技巧和注意事项。这对于学习Linux系统编程和开发应用程序非常有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值