三屏网站建设怎么拥有自己的小程序

当前位置: 首页 > news >正文

三屏网站建设,怎么拥有自己的小程序,空间购买后打不开网站,万江网站建设深入了解语言级别(C语言)文件操作的读与写 在学习前#xff0c;我们先要知道在Linux下的一个原则#xff1a;一切皆是文件 如何理解呢#xff1f;举个外设的例子#xff0c;比如键盘和显示器#xff0c;这两个外设也可以其实本质上也是文件读与写 在学习前我们先要知道在Linux下的一个原则一切皆是文件 如何理解呢举个外设的例子比如键盘和显示器这两个外设也可以其实本质上也是文件通过与内存的交互来交换信息而交互的过程其实就是读与写的过程。而键盘和显示器这两个文件是会默认被打开的。 所以想要理解读与写也就是对文件操作那就必须得从内存的角度去看待问题 从“人”的角度看人读书写字这个过程其实就是书通过文字来传达出信息人读取信息后并记录文字在纸上。人读书其实就是向人大脑进行的一个输入知识的过程这个过程称为“读”人记录信息文字在纸上也就是人大脑向纸进行输出的过程这个过程称为“写”。从“内存”的角度看以显示器和键盘这两个文字为例键盘就好比书上的文字内存读取键盘时就相当于人读取信息的过程相对于内存而言这是一种输入人通过键盘向内存传达信息也就是内存输入信息的过程也就是“读”。而内存向显示器上打印文字的过程就好比人在纸上写字的过程这是内存对外的一个输出过程也就是“写”。 因此从内存的角度来看问题的话一切都简单起来了 键盘无外乎就是一种“读”对应C/C就是scanfcin是input输入的过程 显示器无外乎就是一种“写”对应C/C就是printfcout是output输出的过程 这样我们再来看看什么是文件呢 文件站在系统的角度能够被input或者output写出的设备就叫文件 狭义上的文件普通磁盘文件。 广义上的文件显示器键盘网卡声卡显卡磁盘几乎所有的外设都可以称为文件 语言级文件读写操作函数 学习完这个小知识接下来就来看看文件读写操作的相关语言级别的函数 功能函数名适用于字符输入函数fgetc所有输入流-读字符输出函数fputc所有输出流-写文本行输入函数fgets所有输入流-读文本行输出函数fputs所有输出流-写格式化输入函数fscanf所有输入流-读格式化输出函数fprintf所有输出流-写二进制输入fread文件二进制输出fwrite文件 fgetc和fputc int fputc( int c, FILE *stream ); int fgetc( FILE *stream );fputc是一种写操作属于输出流能够向指定文件种写入一个char类型可以理解成char put file字符放入文件若失败则返回EOF fgetc是一种读操作属于输入流能够读取指定文件的一个字符若读取失败则返回EOF 代码演示 int main() {//以键盘显示器为例//键盘显示器是默认打开的文件因此不用进行open和close操作int ch0;while((chfgetc(stdin))!EOF){fputc(ch,stdout);}return 0; }fgets 和 fputs int fputs( const char *string, FILE *stream ); char *fgets( char *s, int size, FILE *stream );fputs 和 fgets 类似于fputc 和 fgetc fputs 是一种写操作属于输出流能够向指定文件种写入一个string类型可以理解成char put string字符串放入文件但不包括 ‘\0’ 若失败则返回EOF fgets 是一种读操作属于输入流能够读取指定文件中至多比size大小小1的字符串然后将他放入指定的buffer若提前遇到EOF文件末尾或者 ‘\n’ 则读取提前终止‘\n’ 也会被读入读入完成后将自动填充 ‘\0’ 。若读取成功则返回s字符串失败或者什么都没读取到则返回NULL 代码示例 int main() {//以键盘显示器为例//键盘显示器是默认打开的文件因此不用进行open和close操作char buffer[64];while((fgets(buffer,sizeof buffer,stdin)!NULL)){fputs(buffer,stdout);}return 0; }fscanf 和 fprintf int fscanf(FILE *stream,const char *format,…); int fprintf(FILE *stream,const char *format,…);fscanf文档介绍 The scanf() family of functions scans input according to format as described below. This format may contain conversion specifications; the results from such conversions, if any, are stored in the locations pointed to by the pointer arguments that follow format. Each pointer argument must be of a type that is appropriate for the value returned by the corresponding conversion specification. If the number of conversion specifications in format exceeds the number of pointer arguments, the results are undefined. If the number of pointer arguments exceeds the number of conversion specifications, then the excess pointer arguments are evaluated, but are otherwise ignored. These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error. fprintf文档介绍 The functions in the printf() family produce output according to a format as described below. fprintf() write output to the given output stream;Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).If an output error is encountered, a negative value is returned. fscanf 是一种 ”读” 操作属于一种输入流他可以向指定的文件中读取信息并放入格式化参数中 但若读取的信息大于格式化参数的大小则结果就不被定义读取成功则返回读取的信息的数量失败则返回EOF fprintf 是一种 “写” 操作属于一种输出流他可以向指定文件中写入指定的格式化参数写入成功则返回写入的char类型的数量但不包括 ’\0‘ 如果失败则返回一个负值 他们两个的用法其实跟scanf和printf一致只不过需要指定对应的文件流而scanf和printf则默认第一个参数为stdin和stdout。 代码示例 int main() {//以键盘显示器为例//键盘显示器是默认打开的文件因此不用进行open和close操作char s[20];while(fscanf(stdin,%s,s)!EOF)fprintf(stdout,%s\n,s);return 0; }注意格式化的使用方法遇到空格就代表一个占位符的读取已经读取完毕若空格后还有单词且没有新的占位符就不会进行读入。 fwrite和fread size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE stream); fwrite 是一种 “写” 操作属于输出流可以从内存中指定位置的数据中取出数据向指定文件中写入基本单位大小为size个数为nmemb个size单位的数据包括二进制数据,若写入成功则返回实际写入的基本单位的个数 fread 是一种 “读” 操作属于输入流可以从指定文件中读取基本单位大小为size个数为nmemb个size单位的数据包括二进制数据放入被指向的内存位置中若读取成功则返回实际读取的基本单位的个数 int main() {FILE fpfopen(log.txt,w);if(fpNULL){perror(fopen);}const char* s1hello fwrite\n;fwrite(s1,strlen(s1),1,fp);if(fwrite){fwrite(s1,strlen(s1),1,stdout);}fclose(fp);return 0; }; fwrite(s1,strlen(s1),1,fp); if(fwrite) { fwrite(s1,strlen(s1),1,stdout); } fclose(fp); return 0; } [外链图片转存中…(img-GPl5d72L-1707560962937)]