C语言fgets函数了解

原型是:char *fgets(char *s, int n, FILE *stream);

从文件指针stream中读取n-1个字符,存到以s为起始地址的空间里,直到读完一行,如果成功则返回s的指针,否则返回NULL。

例如:一个文件是hello,world,

fgets(str1,4,file1);  

执行后str1="hel",读取了4-1=3个字符.

而如果用而如果用fgets(str1,23,file1);

则执行str1="hello,world",读取了一行(包括行尾的'\n',并自动加上字符串结束符'\0')。

The fgets function reads a string from the input stream argument and stores it in strfgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in str is appended with a null character. The newline character, if read, is included in the string.   ----from MSDN

DEMO1:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. int main(void)  
  3. {  
  4.     FILE *stream;  
  5.     char line[23];  
  6.     if (fopen_s(&stream,"abc.txt","r")==0)   // hello,world  
  7.     {  
  8.         if (fgets(line,4,stream) == NULL)  
  9.         {  
  10.             printf("fgets error \n");  
  11.         }  
  12.         else  
  13.         {  
  14.             printf("%s\n",line);  
  15.             //printf("len is %d\n",strlen(line));  
  16.         }  
  17.         fclose(stream);  
  18.     }  
  19.       
  20.     system("pause");  
  21.     return 0;     
  22. }  

DEMO2:

[cpp] view plaincopy
  1. int main()  
  2. {  
  3.     FILE *stream;  
  4.     char string[]="This is a test";  
  5.     char msg[20];  
  6.     /*w+打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。*/  
  7.     stream=fopen("abc.txt","w+"); /*open a file for update*/  
  8.     fwrite(string,strlen(string),1,stream); /*write a string into the file*/  
  9.     fseek(stream,0,SEEK_SET);  /*seek to the start of the file*/  
  10.     fgets(msg,strlen(string)+1,stream);  
  11.     printf("%s",msg);  
  12.     fclose(stream);  
  13.     system("pause");  
  14.     return 0;  
  15. }  
【FROM MSDN && 百科】

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/510271.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PCL PCD文件读写

/* 时隔一年&#xff0c;又回来做双目视觉方向&#xff0c;需要重新启航&#xff0c;加油&#xff01;&#xff01;&#xff01; */ #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> int main() { //----------------…

判断两颗棵二叉树是否相等

int CompTree(TreeNode *tree1, TreeNode *tree2) { bool isTree1Null (tree1 NULL); bool isTree2Null (tree2 NULL); //其中一个为NULL,而另一个不为NULL,肯定不相等 if (isTree1Null ! isTree2Null) return 1; //两个都为NULL,一定相等 if (isTree1Null && isTr…

sdut 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

第一次写BFS的题&#xff0c;从开始一脸懵逼到慢慢分析&#xff0c;期间没有看书没查博客&#xff0c;自己安静的做了四十分钟终于做出来了&#xff0c;满满的成就感&#xff0c;我很水&#xff0c;但是我很努力&#xff0c;Mr杰要努力&#xff01; #include<stdio.h> #i…

最简逆波兰模板

#include<iostream> #include<math.h> using namespace std; double exp() { char a[10]; scanf("%s",a);//注意scanf取入字符串遇到‘\0’停止 switch(a[0]){ case :return exp()exp(); case -:return exp()-exp(); case *:return exp()…

windows 静态IP设置

1.打开cmd&#xff0c;输入ipconfig 记录 IP address 子网掩码 网关 打开 wlan 属性 选中一个网络右键 “属性” 设置 ipv4&#xff0c;选择“使用下面的IP地址” 对应填写 ip地址 子网掩码 网关 tips&#xff1a;DNS建议和网关一样

算术表达式的前缀式、中缀式、后缀式相互转换

中缀表达式&#xff08;中缀记法&#xff09;中缀表达式是一种通用的算术或逻辑公式表示方法&#xff0c;操作符以中缀形式处于操作数的中间。中缀表达式是人们常用的算术表示方法。 虽然人的大脑很容易理解与分析中缀表达式&#xff0c;但对计算机来说中缀表达式却是很复杂的&…

sdut 二叉排序树

二叉排序树 Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic DiscussProblem Description 二叉排序树的定义是&#xff1a;或者是一棵空树&#xff0c;或者是具有下列性质的二叉树&#xff1a; 若它的左子树不空&#xff0c;则左子树上所有结点的值均小于它的根结点…

“CV_RGB2BGR”: 未声明的标识符

#include "opencv2/imgproc/types_c.h" 添加以上头文件

sdut 图的深度遍历

图的深度遍历Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic DiscussProblem Description请定一个无向图&#xff0c;顶点编号从0到n-1&#xff0c;用深度优先搜索(DFS)&#xff0c;遍历并输出。遍历时&#xff0c;先遍历节点编号小的。Input输入第一行为整数n&…

windows pytorch环境安装配置

1.下载anaconda windows版本下载地址

赫夫曼编码

http://blog.csdn.net/webzhuce/article/details/53105831

哈佛大学凌晨四点半

哈佛老师经常给学生这样的告诫&#xff1a;如果你想在进入社会后&#xff0c;在任何时候任何场合下都能得心应手并且得到应有的评价&#xff0c;那么你在哈佛的学习期间&#xff0c;就没有晒太阳的时间。作为闻名于世的学府&#xff0c;哈佛大学培养了许多名人&#xff0c;他们…

优先队列c++ STL用法

优先队列(priority queue)普通的队列是一种先进先出的数据结构&#xff0c;元素在队列尾追加&#xff0c;而从队列头删除。在优先队列中&#xff0c;元素被赋予优先级。当访问元素时&#xff0c;具有最高优先级的元素最先删除。优先队列具有最高级先出 &#xff08;first in, l…

window 远程linux

1.我们需要在Linux安装ssh服务 sudo apt-get install openssh-server 2.然后开启ssh服务 sudo /etc/init.d/ssh start 3.在window上安装PUTTY 下载网址 4.填写Linux ip 查看ip: 在Linux输入&#xff1a;ifconfig 5.然后填写登陆Linux的账户名和密码 大功告成。。。。。…

赫夫曼编码长度计算问题?

例题&#xff1a;一组字符(a,b,c,d)在文中出现的次数分别为(7,6,3,5),字符&#xff07;d&#xff07;的哈夫曼编码的长度为&#xff1f; 题解&#xff1a; 首先构造huffman树 每一步都将所有数字排序 方法如下: 1: 3 5 6 7 2: 6 7 8 / \ 3 5 3: 8 13 / \ / \ 3 5 6 7 4: 21 /…

windows + cmake + vs2019 编程

1.安装minGW64 2.安装cmake 3.安装vs2019 4.组建代码文件结构&#xff1a; 5.在build 文件下打开git bash&#xff0c;执行一下指令 cmake .. -G"Visual Studio 16 2019" tips( vs2017 对应 cmake .. -G"Visual Studio 15 2017" ) cmake 模板…

sdut 数据结构实验之二叉树六:哈夫曼编码

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std;int main() {char s[10000];while(scanf("%s",s)!EOF){priority_queue < int,vector<int>,greater<int> > Q;//利用优先…

Eigen基本操作

// 矩阵 Eigen::Matrix<float,行,列> // Eigen 中所有向量和矩阵都是Eigen::Matrix&#xff0c;它是一个模板类。它的前三个参数为&#xff1a;数据类型&#xff0c;行&#xff0c;列// 声明一个2*3的float矩阵Eigen::Matrix<float, 2, 3> matrix_23;//float类型…

所感

记住别太善良了&#xff0c;别太大方了&#xff0c;也别太能干了&#xff0c;时间久了人家会觉得&#xff0c;你做的一切都是应该的。即使有一天你撑不住&#xff0c;哭了累了&#xff0c;也没人心疼你。 因为在他们眼里这都是你愿意的。有时候心眼也别太好了不要什么事都为别人…

PCL库使用中遇到的一些问题及解决方法

a. pcl::PointCloud对象变量 与pcl::PointCloud::Ptr 对象指针 的相互转换 #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/point_cloud.h> // 对象指针 pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPointer(new pcl:…