在文件解析中使用fgets() [英] Using fgets() in file parsing

查看:70
本文介绍了在文件解析中使用fgets()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多行的文件.

I have a file which contains several lines.

我正在标记文件,并且如果标记中包含 .word ,我想将其余的行存储在c字符串中.

I am tokenizing the file, and if the token contains contains .word, I would like to store the rest of the line in c-string.

因此,如果: array:.word 0:10 我想将0:10存储在C字符串中.

So if: array: .word 0:10 I would like to store 0:10 in a c-string.

我正在执行以下操作:

if (strstr(token, ".word")) {
    char data_line[MAX_LINE_LENGTH + 1];
    int word_ret = fgets(data_line, MAX_LINE_LENGTH, fptr);
    printf(".word is %s\n", data_line);
}

此问题是 fgets()抓住下一行.我将如何获取当前行的其余部分?有可能吗?

The problem with this is that fgets() grabs the next line. How would I grab the remainder of the current line? Is that possible?

谢谢

推荐答案

strstr()返回一个指向:word"的第一个字符的指针.

strstr() returns a pointer to where the first character of ":word" is found.

这意味着,如果在其中加上:word"的长度(5个字符),则将获得指向:word"之后的字符的指针,这就是您想要的字符串.

This means that if you add the length of ":word" (5 characters) to that, you will get a pointer to the characters after ":word", which is the string you want.

char *x = strstr(token, ".word");
char *string_wanted = x + 5;

这篇关于在文件解析中使用fgets()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆