gpt4 book ai didi

c - 如何复制文本直到换行符?

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:57 27 4
gpt4 key购买 nike

我有一个字符数组 list,其中包含文本文件中的文本,例如:

this is the first line
this is the second line

我想将第一行复制到另一个不带\n(和/或\r)的字符数组。

我不知道第一行的确切大小,但我知道它少于 100 个字节。

我的代码片段:

unsigned char *line;
line = (u_char *)calloc(100, sizeof(char));

//read txt file to list

while(list[0] != '\n'){
line[0] = list[0];
list++;
line++;
}

不幸的是行是空的。请注意,我确定列表不为空,并且包含如上所示的文本。

关于此代码或其他解决方案的任何建议?该文件是使用 open() 而不是 fopen() 打开的,因此我必须遍历我的列表数组。

最佳答案

你可以这样做:

for ( int i = 0; list[i] && list[i] != '\n'; ++i ) {
line[i] = list[i];
}

关于c - 如何复制文本直到换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833339/

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