【发布时间】:2016-02-13 06:43:30
【问题描述】:
我是 C 新手,我想做文件读取操作。 这里我有 input.txt,其中包含:
(g1,0.95) (g2,0.30) (m3,0.25) (t4,0.12) (s5,0.24)
(m0,0.85) (m1,0.40) (m2,0.25) (m3,0.85) (m4,0.5) (m5,0.10)
现在,我想将 k1、k2、k3 等保存在数组键 [10] 中,并将 0.15、0.10、0.05 保存在数组值 [10] 中
有什么办法可以跳过第一个“(”,忽略“,”和“”而不一一指定?我试图搜索教程,我听说我可以在它之前和之后阅读几个字符,但是我想我误导了他们。有人可以告诉我如何做到这一点吗?
#include <stdio.h>
#define HEIGHT 2
#define WIDTH 6
int main(void)
{
FILE *myfile;
char nothing[100];
char leaf[2];
float value;
char keys[10];
float values[10];
int i;
int j;
int counter=0;
myfile=fopen("input.txt", "r");
for(i = 0; i < HEIGHT; i++)
{
for (j = 0 ; j < WIDTH; j++)
{
fscanf(myfile,"%1[^(],%s[^,],%4f[^)]",nothing,leaf,value);
printf("(%s,%f)\n",leaf,value);
keys[counter]=leaf;
values[counter]=value;
counter++;
}
printf("\n");
}
fclose(myfile);
}
【问题讨论】:
标签: c input filereader scanf file-read