gpt4 book ai didi

c 程序将两个特定字之间的数据从一个文件复制到另一个文件

转载 作者:行者123 更新时间:2023-11-30 19:22:21 25 4
gpt4 key购买 nike

with header;
with linker;
package body(* some text )
vin float32=2.0;
mis float32=3.0;
raj pointtodatatype.array(.234,-.2344323343,.234555656,.2334445344)
rex float32=3*3.142345634;
procedure

我想从文本文件中读取此数据,并将数据复制到以 package 开头的行到以 procedure 开头的行之间。

因此输出将是另一个包含以下内容的文本文件:

vin float32=2.0;
mis float32=3.0;
raj pointtodatatype.array(.234,-.2344323343,.234555656,.2334445344)
rex float32=3*3.142345634;

最佳答案

#include <stdio.h>
#include <string.h>

int main(void)
{
char line[4096];

while (fgets(line, sizeof(line), stdin) != 0)
{
if (strncmp(line, "package ", sizeof("package ")-1) == 0)
{
while (fgets(line, sizeof(line), stdin) != 0)
{
if (strncmp(line, "procedure\n", sizeof("procedure\n")-1) == 0)
break;
fputs(line, stdout);
}
break;
}
}
return 0;
}

代码从标准输入读取并写入标准输出。如果 file1 中有数据并希望在 file2 中得到结果,请使用:

./program <file1 >file2

这是标准输入/标准输出过滤器和 shell I/O 重定向的优点之一。

已测试。假设procedure之后没有尾随空格,代码现在查找“procedure\n”。如果您必须允许可能存在尾随空格或没有尾随空格,则 procedure 的比较会更加繁琐。

关于c 程序将两个特定字之间的数据从一个文件复制到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16728290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com