在C语言中将文本文件中的输入读入结构体数组

4
我的结构定义是:
typedef struct {
    int taxid;
    int geneid;
    char goid[20];
    char evidence[4];
    char qualifier[20];
    char goterm[50];
    char pubmed;
    char category[20];    
} gene2go;

我有一个名为“gene2go.txt”的制表符分隔文本文件。
该文件的每一行包含taxID、geneID、goID、evidence、qualifier、goterm、pubmed和category信息。
文件中的每一行都将被保存在一个结构体中。
当程序运行时,它将首先将输入文件的内容读入到一个名为gene2go的数组类型中,我使用了一个名为“readInfo”的函数。
程序还将从命令行接收以下输入参数:输入类型(1表示taxid,2表示geneid,3表示goid)和搜索项。
有一个名为“listData”的函数,用于将与输入类型和搜索项匹配的文件“gene2go.txt”中的行列表写入文件“output.txt”。
这是我的文本文件“gene2go.txt”的一部分。
3702    814629  GO:0003676  IEA -   nucleic acid binding    -   Function
3702    814629  GO:0005575  ND  -   cellular_component  -   Component
3702    814629  GO:0005634  ISM -   nucleus -   Component
3702    814629  GO:0008150  ND  -   biological_process  -   Process
3702    814629  GO:0008270  IEA -   zinc ion binding    -   Function
3702    814630  GO:0005634  ISM -   nucleus -   Component
3702    814636  GO:0008150  ND  -   biological_process  -   Process
3702    814637  GO:0003674  ND  -   molecular_function  -   Function
6239    177883  GO:0008150  ND  -   biological_process  -   Process
6239    177884  GO:0005575  ND  -   cellular_component  -   Component
6239    177884  GO:0008150  ND  -   biological_process  -   Process
6239    177886  GO:0004364  IDA -   glutathione transferase activity    12757851    Function
6239    177886  GO:0005575  ND  -   cellular_component  -   Component
7955    555450  GO:0005634  IEA -   nucleus -   Component
7955    555450  GO:0006355  IEA -   regulation of transcription, DNA-dependent  -   Process

我已经编写了下面命名为ceng301.c的代码,并使用命令行进行了编译: gcc ceng301.c -o ceng301 但是,当我在命令行中输入以下内容: ceng301 1 3702 时,我得到的不是预期结果,而是一个闪烁的下划线 :(
3702    814629  GO:0003676  IEA -   nucleic acid binding    -   Function
3702    814629  GO:0005575  ND  -   cellular_component  -   Component
3702    814629  GO:0005634  ISM -   nucleus -   Component
3702    814629  GO:0008150  ND  -   biological_process  -   Process
3702    814629  GO:0008270  IEA -   zinc ion binding    -   Function
3702    814630  GO:0005634  ISM -   nucleus -   Component
3702    814636  GO:0008150  ND  -   biological_process  -   Process
3702    814637  GO:0003674  ND  -   molecular_function  -   Function

保存在output.txt中。

以下是代码:

#include <stdio.h>
#include <stdlib.h>

/* structure definition */
typedef struct {
    int taxid;
    int geneid;
    char goid[20];
    char evidence[4];
    char qualifier[20];
    char goterm[50];
    char *pubmed;
    char category[20];
} gene2go;

/* function prototypes */
int readInfo( gene2go input[] );
void listData( char *inType, char *searchItem, gene2go input[], int i );

int main( int argc, char *argv[] )
{
   gene2go input[200];
   int i;

   i = readInfo( input );
   listData( argv[1], argv[2], input, i );

   return 0;
}

/* read the input file*/
int readInfo( gene2go input[] ) {
   FILE *fin;
   char *inputName = "gene2go.txt";
   int i = 0;

   fin = fopen( inputName, "r" );

   if( fin == NULL ) {
      printf( "File cannot be opened\n" );
   } /* end if */
   else {
      while( !feof( fin ) ) {
        fscanf( fin, "%[^\t]", &input[i].taxid,
                               &input[i].geneid,
                               &input[i].goid,
                               &input[i].evidence,
                               &input[i].qualifier,
                               &input[i].goterm,
                               &input[i].pubmed,
                               &input[i].category );
        i++;
     } /* end while */

     fclose( fin );
  } /* end else */

  return i;
} /* end function readInfo */

void listData( char *inType, char* searchItem, gene2go input[], int i ) {
   FILE *fout;
   char *outputName = "output.txt";
   int j;
   int inputType = atoi( inType );

   fout = fopen( outputName, "w" );

   switch( inputType ) {
      case 1:
          for( j = 0; j < i; j++ ) {
               if( input[j].taxid == atoi( searchItem ) ) {
                   fprintf( fout, "%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", input[i].taxid,
                                                                      input[i].geneid,
                                                                      input[i].goid,
                                                                      input[i].evidence,
                                                                      input[i].qualifier,
                                                                      input[i].goterm,
                                                                      input[i].pubmed,
                                                                      input[i].category );
               } /* end if */
          } /* end for */
          break;
     case 2:
          for( j = 0; j < i; j++ ) {
              if( input[j].geneid == atoi( searchItem ) ) {
                  fprintf( fout, "%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", input[i].taxid,
                                                                     input[i].geneid,
                                                                     input[i].goid,
                                                                     input[i].evidence,
                                                                     input[i].qualifier,
                                                                     input[i].goterm,
                                                                     input[i].pubmed,
                                                                     input[i].category );
              } /* end if */
          } /* end for */
          break;
     case 3:
          for( j = 0; j < i; j++ ) {
              if( input[j].goid == searchItem ) {
                  fprintf( fout, "%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", input[i].taxid,
                                                                     input[i].geneid,
                                                                     input[i].goid,
                                                                     input[i].evidence,
                                                                     input[i].qualifier,
                                                                     input[i].goterm,
                                                                     input[i].pubmed,
                                                                     input[i].category );
              } /* end if */
          } /* end for */
          break;
 } /* end switch */

 fclose( fout );
} /* end function listData */

我该怎么办?


fscanffgets似乎是解决这个问题的合理函数,并且两者的使用都有很好的文档记录。看起来你已经有了解决这个问题的想法。你还在寻找什么? - WhozCraig
你需要解释如何将 pubmedcategory 字段的字符串转换为 int - j_random_hacker
2
不管你的问题是什么(“学习... fscanf”),为什么你决定使用C语言来完成这样的任务呢?至少20年来,它已经不是最适合所有情况的编程语言了。 - Yury
在你的结构体中,pubmed 不应该是一个 char * 吗? - asbumste
1个回答

0
char mystring[100];
FILE *p = fopen ("gene2go.txt" , "r");
if (p == NULL) perror ("Error opening file");
   else {
     if ( fgets (mystring , 100 , pFile) != NULL )
       puts (mystring);
      pch = strtok (mystring, "\t");
      while (pch != NULL)
      {
          //handle each token here and insert into struct
          pch = strtok (NULL, "\t");
      }
     fclose (pFile);
   }
   return 0;

请参考strtokfgets


谢谢你的帮助。我会按照你的建议使用strtok和fgets,并希望能够编写出完整的代码来解决这个问题。 - fgokmenoglu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接