读书人

fscanf怎么读取文件中保存的多行数据

发布时间: 2012-06-06 16:44:11 作者: rapoo

fscanf如何读取文件中保存的多行数据
文件中的数据类似这样的
123.456,
235.89,
947,345
请问如何用fscanf读取每行数据并存储到一个数组中,
我用
for(int i =0;i<n;i++)
fscanf(fp,"%lf",aa[i]);
每次只是重复读取第一行数据


[解决办法]

C# code
[User:liangdong Time:22:58:15 Path:~/c]$ cat main.c #include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char* argv[]) {        char buf[100];        FILE *fp = fopen("data.txt", "r");        if (!fp) {                return -1;        }        while (fgets(buf, 100, fp) != NULL) {                int len = strlen(buf);                if (buf[len - 1] == '\n') {                        buf[len - 1] = '\0';                }                char *sep = strchr(buf, ',');                if (sep != NULL) {                        *sep = '\0';                }                printf("%s\n", buf);        }        return 0;}
[解决办法]
int i=0;
while(!feof(p)) //p为句柄
{
fscanf(p,"%s\n",a[i]);
i++;

}
可以把每行存入到数组中

读书人网 >C语言

热点推荐