如何逐行读取后,我读了文字到缓冲? [英] How to read line by line after i read a text into a buffer?

查看:117
本文介绍了如何逐行读取后,我读了文字到缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我通过调用FREAD读取文本到缓冲区中,然后我要一行一行地读它,怎么办呢?我尝试使用sscanf的,但似乎不工作。

First , I read a text into a buffer by calling fread, and then I want to read it line by line, how to do it? I try to use a sscanf , but it seems not to work.

char textbuf[4096];
char line[256];
FILE *fp;
fp = fopen(argv[1],"r");
memset(textbuf, 0, 4096);
fread(textbuf, 1, 4096, fp);

我知道使用与fgets是一个好办法。我只是想知道天气这种方法可以做同样的事情。

I know using fgets is a good way. I just want to know weather this method can do the same thing.

推荐答案

试试这个:

fgets(textbuf, sizeof(textbuf), fp);

对于线,您可以使用读行:与fgets(行,128,FP)函数getline(安培;线,&安培;大小, FP);

修改

如果您想从一个变量读它,看看的strtok()功能:

If you want to read it from a variable, look at strtok() function:

char * line = strtok(strdup(buffer), "\n");
while(line) {
   printf("%s", line);
   line  = strtok(NULL, "\n");
}

这篇关于如何逐行读取后,我读了文字到缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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