c - C- 查找我打开的文本文件的当前行
问题描述
我需要一个解决方案来在文本文件中找到当前使用的行。我打开它:
FILE *fp;
fp=fopen(“data.txt”, “ r+“);
解决方案
要跟踪包含您当前正在处理的文本行的文件中的哪一行,您可以使用计数变量。这是一个简单的例子
while (fgets(buffer, BUFLEN, fp) != NULL)
{
printf("Line %d: %s", line, buffer);
if (buffer[strlen(buffer)-1] != '\n')
// fgets didnt read an entire line, so increment the file pointer to
// the start of the next line. also, output the newline since it wasn't
// part of buffer above
{
int ch;
do
{
ch = fgetc(fp);
}
while (ch != '\n' && ch != EOF);
putchar('\n');
}
line++;
}
推荐阅读
- android - 在 Google Play 上为测试订单退款
- entity-framework - 带有 ef 的域模型不正确
- javascript - NodeJS HTTPS 请求返回 Socket 挂断
- perl - Perl:msgsnd 的参数
- apache-spark - org.apache.spark.sql.AnalysisException:流数据帧/数据集不支持多个流聚合;
- javascript - Vue 组件在不同状态下的行为如何不同?
- php - 在 woocommerce 我的帐户页面中添加银行详细信息选项卡和输入字段
- javascript - 在javascript中将一个大数字切成三位数
- android - TableRow 不适合屏幕宽度
- java - JPanel 中的随机多边形:缩放坐标、GridBagLayout 和缩放坐标、不同的布局管理器,还是完全不同的东西?