跳过逗号和列读取 C 输入
Reading C Input with skipping commas and columns
我是 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);
}
以下是我的做法:
int main( void )
{
// open the file
FILE *fp;
if ( (fp = fopen("test.txt", "r")) == NULL )
exit( 1 );
// declare the arrays
char keys[10][32];
float values[10];
// load them up
int i;
for ( i = 0; i < 10; i++ )
if ( fscanf( fp, " ( %31[^ ,] ,%f )", keys[i], &values[i] ) != 2 )
break;
int count = i;
// print them out
printf( "%d\n", count );
for ( i = 0; i < count; i++ )
printf( "%s %.2f\n", keys[i], values[i] );
// close the file
fclose( fp );
}
关键是 scanf
的格式说明符,由 5 个元素组成。
请注意,我使用下划线来显示空格
_(_ skips whitespace, matches the opening parenthesis, skips whitespace
%31[^_,] reads at most 31 characters, stopping on a space or a comma
_, skips whitespace, matches the comma
%f reads a floating point value
_) skips whitespace, matches the closing parenthesis
我是 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);
}
以下是我的做法:
int main( void )
{
// open the file
FILE *fp;
if ( (fp = fopen("test.txt", "r")) == NULL )
exit( 1 );
// declare the arrays
char keys[10][32];
float values[10];
// load them up
int i;
for ( i = 0; i < 10; i++ )
if ( fscanf( fp, " ( %31[^ ,] ,%f )", keys[i], &values[i] ) != 2 )
break;
int count = i;
// print them out
printf( "%d\n", count );
for ( i = 0; i < count; i++ )
printf( "%s %.2f\n", keys[i], values[i] );
// close the file
fclose( fp );
}
关键是 scanf
的格式说明符,由 5 个元素组成。
请注意,我使用下划线来显示空格
_(_ skips whitespace, matches the opening parenthesis, skips whitespace
%31[^_,] reads at most 31 characters, stopping on a space or a comma
_, skips whitespace, matches the comma
%f reads a floating point value
_) skips whitespace, matches the closing parenthesis