当前位置:网站首页>6-1 从文件读取字符串(*)
6-1 从文件读取字符串(*)
2022-06-11 17:17:00 【西红柿_码农】
6-1 从文件读取字符串(*)
分数 10
全屏浏览题目
切换布局
作者 李祥
单位 湖北经济学院
请编写函数,从文件中读取字符串。
函数原型
void FGetStr(char *str, int size, FILE *file);说明:参数 str 为指示字符数组起始地址的指针,size 为数组尺寸,file 为文件指针。函数从文件输入的字符串(以换行符 '\n' 结束)到字符数组中,并在字符末尾添加字符串结束标记 '\0'。显然,字符串的最大长度应为 size - 1,为字符串结束标记 '\0' 预留空间。若用户输入的字符过多,则函数最多读取 size - 1 个字符,剩余字符仍留在缓冲区中,可以继续被后面的输入函数读取。
要求:不得把文件中的换行符 '\n' 也保存到字符串中。
裁判程序
#include <stdio.h>
#include <string.h>
void FGetStr(char *str, int size, FILE *file);
int main()
{
FILE *f;
char a[10], b[10];
f = fopen("MyStr.txt", "r");
if (f)
{
FGetStr(a, 10, f);
FGetStr(b, 10, f);
puts(a);
puts(b);
fclose(f);
}
return 0;
}创建文本文件 MyStr.txt,复制下面的内容。
- MyStr.txt *
Bob
Mary
输出样例1
Bob
Mary
修改文本文件 MyStr.txt,复制下面的内容。
- MyStr.txt *
Constantine
输出样例2
Constanti
ne
修改文本文件 MyStr.txt,复制下面的内容。
- MyStr.txt *
Francisco
Stevenson
输出样例3
Francisco
Stevensonvoid FGetStr(char *str, int size, FILE *file)
{
int i = 0;
char c;
while(c = fgetc(file), c != '\n' && c != EOF && i < size - 1)
{
str[i] = c;
++i;
}
if(c != '\n')
{
ungetc(c,stdin);
}
str[i] = '\0';
}
边栏推荐
- 导出数据提示--secure-file-priv选项问题的解决方法
- LeetCode-1005. K 次取反后最大化的数组和
- vscode保存代碼時自動eslint格式化
- Leetcode 450. Deleting a node in a binary search tree
- [pytest learning] after the pytest case fails to execute, the others will not be executed
- 从制造到“智造”,探索制造企业破局之道
- Authoring share | understanding saml2 protocol
- LeetCode-384. 打乱数组
- 定制 or 订阅?未来中国 SaaS 行业发展趋势是什么?
- Is the second-class cost engineer worth the exam? What is the development prospect?
猜你喜欢

Dynamic: capturing network dynamics using dynamic graph representation learning

Intranet penetration based on UDP port guessing

活动 | Authing 首次渠道合作活动圆满落幕

vscode保存代碼時自動eslint格式化

Create database instance

Docker安装mysql5.7(开启binlog功能、修改字符)

Axi protocol Basics

Leetcode力扣刷题

Don't you understand the design and principle of thread pool? Break it up and crush it. I'll teach you how to design the thread pool

Authing 双周动态:应用市场上线(5 .10 —5 .22 )
随机推荐
自动化测试-Selenium
搜狐全員遭詐騙,暴露哪些問題?
MATLAB中histogram函数的使用
Intranet penetration based on UDP port guessing
Real time myth -- real-time RTOS multitask performance analysis
ffmpeg CBR精准码流控制三个步骤
从制造到“智造”,探索制造企业破局之道
04_特征工程—特征选择
Centos7 server configuration (IV) -- installing redis
Science popularization genius on the left, madman on the right
DFS and BFS notes (I) breadth first search based on C language
Axi protocol Basics
ASP. Net education OA system source code education industry OA system source code with document
Oracle analysis function over and MySQL achieve similar effects
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
拜登下令强制推行零信任架构
Connection and difference of network streaming media protocol (RTP RTCP RTMP HLS)
LeetCode-384. Scramble array
Authing 背后的计算哲学
信息安全数学基础 Chapter 3——有限域(二)