第1个回答 2009-03-27
fgetc(fp)一个一个的读入字符
读入的字符 不是v 继续读
读到v 则
改用
long int n=0,k;
long int x[100];
while ( fscanf(fp,"%d",&k) == 1) {
x[i] = k;
n = n + 1;
}
不用组合,读入的n 个数在 数组 x 里
第2个回答 2009-03-27
#include <stdio.h>
#include <stdlib.h>
int returnvalue(int c)
{
int temp=0;////貌似足够5位数了
while(c!=' ')
{
temp+=c-'0';
temp=temp*10;
}
return temp;
}
int main()
{
FILE *stream;
int c,value;
int a[100],i=0;
if((stream=fopen("这里改成你的文件位置", "r"))==NULL)
{
perror("cannot open the file !\n");
exit(1);
}
while((c=fgetc(stream))!=EOF && c!='v')
continue;
while((c=fgetc(stream))!=EOF)
{
value=returnvalue(c);
a[i++]=value;
}
while(i>0)
printf("%d\t", a[i--]);
return 0;
}
第3个回答 2009-03-27
你就不想一次把一行读完
char line[100];
fgets( line, 100, fp);
然后判断 line[0] == '.' && line[1] == 'v'
如果个数一定可以用sscanf(line,"%d %d %d",&d1,&d2,&d3);来读取
Get a string from a stream.
char *fgets( char *string, int n, FILE *stream );
Example
/* FGETS.C: This program uses fgets to display
* a line from a file on the screen.
*/
#include <stdio.h>
void main( void )
{
FILE *stream;
char line[100];
if( (stream = fopen( "fgets.c", "r" )) != NULL )
{
if( fgets( line, 100, stream ) == NULL)
printf( "fgets error\n" );
else
printf( "%s", line);
fclose( stream );
}
}