惭愧……初学pro*c,现在被一个很简单的问题难倒。
  有一个txt文档。里面的数据是从别的表里读出来的,已经排好。
  1  2  3  4
  5  6  7  8
  ……
  现在要从这个txt里面读出其中几列,insert 到一个刚建好的空表里面。
  现在的问题是:找不到函数,是fopen,fread吗?又看不到例子,不懂用。情急之间没空去买书,只好上网来……
  
是不是先FILE *cfptr 一个指针
  然后 if ((cfptr=fopen("???.txt","r")) ==NULL
  写出错信息
  else
  fscanf....后面的用法就不知道了,应该是逐行读取写入一个数组之类的变量  拜托大虾,拯救一个就要失去学习兴趣的小生。

解决方案 »

  1.   

    Example
    /* FSCANF.C: This program writes formatted
     * data to a file. It then uses fscanf to
     * read the various data back from the file.
     */#include <stdio.h>FILE *stream;void main( void )
    {
       long l;
       float fp;
       char s[81];
       char c;   stream = fopen( "fscanf.out", "w+" );
       if( stream == NULL )
          printf( "The file fscanf.out was not opened\n" );
       else
       {
          fprintf( stream, "%s %ld %f%c", "a-string", 
                   65000, 3.14159, 'x' );      /* Set pointer to beginning of file: */
          fseek( stream, 0L, SEEK_SET );      /* Read data back from file: */
          fscanf( stream, "%s", s );
          fscanf( stream, "%ld", &l );      fscanf( stream, "%f", &fp );
          fscanf( stream, "%c", &c );      /* Output data read: */
          printf( "%s\n", s );
          printf( "%ld\n", l );
          printf( "%f\n", fp );
          printf( "%c\n", c );      fclose( stream );
       }
    }Output
    a-string
    65000
    3.141590
    x
      

  2.   

    用fgets()试试,fscanf用法跟scanf一样
      

  3.   

    对上面的原码的几个问题:
    1。(w+)不是写吗?我现在要的是~读~呀。
    2,小生要的是取出我的那个例子里面的2,3,6,7,插入到空表里……
    snakezzg兄的例子好像……
      

  4.   

    Example
    /* FSCANF.C: This program writes formatted
     * data to a file. It then uses fscanf to
     * read the various data back from the file.
     */#include <stdio.h>FILE *stream;void main( void )
    {
        
       char line[1024];
       int col1,col2,col3,col4;   stream = fopen( "fscanf.out", "r" );
       if( stream == NULL )
          printf( "The file fscanf.out was not opened\n" );
       else
       {
          while(!feof(stream))
         {
           //read a line
           fgets(line, 1024, stream)
           sscanf(line,"%d%d%d%d",&col1,&col2,&col3,&col4)  ;
           //do something
          }
          fclose( stream );
       }
    }Output
    a-string
    65000
    3.141590
    x
      

  5.   

    问题最后还是用fgets解决了…………谢谢出手的大虾们看得起菜鸟小生。分分……由于小生初到,不符合(1、捐赠人只能是中级会员(专家分1000分以上)、高级会员(获得一定头衔的用户);
    ),
    所以…………sorry了snakezzg兄,有机会来日报答。