阅读从文本文件输入到在C结构的阵列 [英] reading input from text file into array of structures in c

查看:197
本文介绍了阅读从文本文件输入到在C结构的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构定义是,

  typedef结构{
    INT taxid;
    INT geneid;
    焦炭GOID [20];
    CHAR [4]证据;
    预选赛字符[20];
    焦炭goterm [50];
    焦炭考研;
    焦炭类[20];
} gene2go;

我有一个名为'gene2go.txt选项卡分隔的文本文件。

该文件的每一行包含 taxID geneID GOID 证据预选赛 goterm 考研类别信息。

文件的每一行将被保存在一个结构

当程序运行时,它会首先读取输入文件的内容入式gene2go的数组,我使用了一种叫做函数 readInfo

该计划还将采取以下输入参数在命令行中,

taxid 2 geneid GOID <

输入类型(1,3 / code>)和搜索字词

有一个名为的ListData 函数写在文件gene2go.txt相匹配的输入类型和搜索词,对文件中的行列表中的输出.TXT。

下面是我的文本文件的一部分gene2go.txt

  3702 814629 GO:0003676 IEA  - 核酸结合 - 功能
3702 814629 GO:0005575 ND - cellular_component - 组件
3702 814629 GO:0005634 ISM - 核 - 组件
3702 814629 GO:0008150 ND - biological_process - 过程
3702 814629 GO:0008270 IEA - 锌离子结合 - 功能
3702 814630 GO:0005634 ISM - 核 - 组件
3702 814636 GO:0008150 ND - biological_process - 过程
3702 814637 GO:0003674 ND - molecular_function - 功能
6239 177883 GO:0008150 ND - biological_process - 过程
6239 177884 GO:0005575 ND - cellular_component - 组件
6239 177884 GO:0008150 ND - biological_process - 过程
6239 177886 GO:0004364 IDA - 谷胱甘肽转移酶的活性12757851功能
6239 177886 GO:0005575 ND - cellular_component - 组件
7955 555450 GO:0005634 IEA - 核 - 组件
7955 555450 GO:0006355 IEA - 转录调控,DNA依赖 - 过程

我已经写了如下命名ceng301.c的code和使用命令行编​​译它

的gcc -o ceng301.c ceng301

,但是当我写

ceng301 1 3702

在命令行中,我得到什么,但一个闪烁下划线:(代替

  3702 814629 GO:0003676 IEA  - 核酸结合 - 功能
3702 814629 GO:0005575 ND - cellular_component - 组件
3702 814629 GO:0005634 ISM - 核 - 组件
3702 814629 GO:0008150 ND - biological_process - 过程
3702 814629 GO:0008270 IEA - 锌离子结合 - 功能
3702 814630 GO:0005634 ISM - 核 - 组件
3702 814636 GO:0008150 ND - biological_process - 过程
3702 814637 GO:0003674 ND - molecular_function - 功能

保存在 output.txt的

下面是code,

 的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;/ *结构定义* /
typedef结构{
    INT taxid;
    INT geneid;
    焦炭GOID [20];
    CHAR [4]证据;
    焦炭预选赛[20];
    焦炭goterm [50];
    字符*考研;
    焦炭类[20];
} gene2go;/ *函数原型* /
INT readInfo(gene2go输入[]);
无效的ListData(字符*法菜单,字符* searchItem,gene2go输入[],int i)以;INT主(INT ARGC,CHAR *的argv [])
{
   gene2go输入[200];
   INT I;   I = readInfo(输入);
   的ListData(的argv [1],的argv [2],输入,一);   返回0;
}/ *读取输入文件* /
INT readInfo(gene2go输入[]){
   FILE *鳍;
   char *之inputName =gene2go.txt;
   INT I = 0;   鳍= FOPEN(inputName,R);   如果(翅== NULL){
      的printf(文件无法打开\\ n);
   } /* 万一 */
   其他{
      而(!的feof(FIN)){
        的fscanf(翅,%[^ \\ t],&安培;输入[I] .taxid,
                               &安培;输入[I] .geneid,
                               &安培;输入[I] .goid,
                               &安培;输入[I] .evidence,
                               &安培;输入[I] .qualifier,
                               &安培;输入[I] .goterm,
                               &安培;输入[I] .pubmed,
                               &安培;输入[I] .category);
        我++;
     } / *结束,而* /     FCLOSE(翅);
  } / *结束还有* /  返回我;
} / *函数结束readInfo * /无效的ListData(字符*法菜单,字符* searchItem,gene2go输入[],int i)以{
   FILE * FOUT;
   char *之outputName =output.txt的;
   诠释J;
   INT的inputType =的atoi(法菜单);   FOUT =的fopen(outputName,W);   开关(inputType下){
      情况1:
          为(J = 0; J&LT; I; J ++){
               如果(输入[J] .taxid ==与atoi(searchItem)){
                   fprintf中(FOUT,%d个\\ t%d个\\ t%S \\ t%S \\ t%S \\ t%S \\ t%S \\ t%S \\ n,输入[I] .taxid,
                                                                      输入[I] .geneid,
                                                                      输入[I] .goid,
                                                                      输入[I] .evidence,
                                                                      输入[I] .qualifier,
                                                                      输入[I] .goterm,
                                                                      输入.pubmed [I]
                                                                      输入[I] .category);
               } /* 万一 */
          } / *末尾* /
          打破;
     案例2:
          为(J = 0; J&LT; I; J ++){
              如果(输入[J] .geneid ==与atoi(searchItem)){
                  fprintf中(FOUT,%d个\\ t%d个\\ t%S \\ t%S \\ t%S \\ t%S \\ t%S \\ t%S \\ n,输入[I] .taxid,
                                                                     输入[I] .geneid,
                                                                     输入[I] .goid,
                                                                     输入[I] .evidence,
                                                                     输入[I] .qualifier,
                                                                     输入[I] .goterm,
                                                                     输入.pubmed [I]
                                                                     输入[I] .category);
              } /* 万一 */
          } / *末尾* /
          打破;
     案例3:
          为(J = 0; J&LT; I; J ++){
              如果(输入[J] .goid == searchItem){
                  fprintf中(FOUT,%d个\\ t%d个\\ t%S \\ t%S \\ t%S \\ t%S \\ t%S \\ t%S \\ n,输入[I] .taxid,
                                                                     输入[I] .geneid,
                                                                     输入[I] .goid,
                                                                     输入[I] .evidence,
                                                                     输入[I] .qualifier,
                                                                     输入[I] .goterm,
                                                                     输入.pubmed [I]
                                                                     输入[I] .category);
              } /* 万一 */
          } / *末尾* /
          打破;
 } / *结束开关* / FCLOSE(FOUT);
} / *函数结束的ListData * /

我应该怎么办?


解决方案

 字符的myString [100];
FILE * p =的fopen(gene2go.txt,R);
如果(P == NULL)PERROR(错误打开文件);
   其他{
     如果(与fgets(MyString的,100,PFILE)!= NULL)
       看跌期权(myString的);
      PCH = strtok的(MyString的,\\ t的);
      而(PCH!= NULL)
      {
          //这里处理每个令牌,并插入结构
          PCH =的strtok(NULL,\\ t的);
      }
     FCLOSE(PFILE);
   }
   返回0;

请参照 的strtok 与fgets

My structure definition is,

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

I have tab-seperated text file called `"gene2go.txt".

Each line of this file contains taxID, geneID, goID, evidence, qualifier, goterm, pubmed and category information.

Each line of the file will be kept in a structure.

When the program is run, it will first read the content of the input file into an array of type gene2go, I used a function called readInfo.

The program will also take the following input arguments from the command line,

input type ( 1 for taxid, 2 for geneid, 3 for goid ) and search term

There is a function called listData to write the list of lines in the file "gene2go.txt" that match the input type and search term, to the file "output.txt".

Here is a part of my text file "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

I have written the code below named ceng301.c and compiled it using the command line

gcc ceng301.c -o ceng301

, but when I write

ceng301 1 3702

in the command line, I get nothing, but a blinking underscore :( instead of

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

saved in output.txt

Here is the code,

#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 */

What should I do?

解决方案

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;

Refer to strtok, fgets

这篇关于阅读从文本文件输入到在C结构的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆