开发者社区> 问答> 正文

如何从文件中读取行到字符串数组?

#define LINES 4
#define LENGHT 30

typedef struct
{
  char *name;
  char *phoneNumber;
  char *location;
  char *traveltype;
} Client;

int main(int argc, char *argv[])
{
  char *filename = argv[1];
  char clientData[LINES][LENGHT];
  readClientData(filename, clientData);

  /* How to do this client struct */
  Client *client = createClient(clientData[0], clientData[1], clientData[2], clientData[3]);
  return 0;
}

void readClientData(char *filename, char clientData[LINES][LENGHT])
{
  FILE *file = fopen(filename, "r");
  if (file == NULL)
  {
    perror("Error while opening file!");
    exit(1);
  }

  char line[LENGHT];
  int i = 0;
  while (fgets(line, LENGHT, file) != NULL)
    clientData[i] = line; /* How to do this */ 
    i++;
  fclose(file);
}

来自pythonista世界,我有点困惑这里的工作方式。显然,我不能只向列表中添加行,也不能像初始化客户端结构那样添加行。

也许我应该只使用char *clientData[],但不知道如何使用。

展开
收起
kun坤 2019-11-29 11:36:53 564 0
1 条回答
写回答
取消 提交回答
  • 要复制C字符串,您需要使用:

    char * strcpy ( char * destination, const char * source );
    

    从标准C字符串库中获得string.h。

    然而,通知,比Python的不同(你的背景),压痕并没有定义循环的身体,你需要使用大括号!

    因此,您需要这样做:

    while (fgets(line, LENGHT, file) != NULL)
    {
      strcpy(clientData[i], line);
      i++;
    }
    
    

    如果没有大括号,则仅将代码的第一直接行视为循环的主体。因此,在上面的示例中,如果不使用大括号,则循环的主体将只是对复制字符串的方法的调用,而计数器的增量将不会成为循环的一部分!

    您需要定义或只声明一个方法,然后再使用它,而您在示例中没有这样做。

    由于您只需要一个客户端,因此不需要指针,因此只需创建一个即可Client client;。

    为了简单起见,请使用到目前为止所获得的知识,并定义字符串的最大长度(即结构的每个字段都具有的字符数组的大小)。请记住,C字符串必须以NULL终止,因此,它们的最大长度实际上是LENGTH - 1。

    如果将struct的字段保留为的指针char,则需要动态分配内存,或将指针指向clientData数组的相应字符串(类似于对文件名所做的操作)。我建议您先获得一些C方面的经验,然后再尝试实现这两种方法。

    现在,您准备好了:

    strcpy(client.name, clientData[0]);
    ...
    strcpy(client.traveltype, clientData[3]);
    
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define LINES 4
    #define LENGTH 30
    
    typedef struct
    {
      char name[LENGTH];
      char phoneNumber[LENGTH];
      char location[LENGTH];
      char traveltype[LENGTH];
    } Client;
    
    void readClientData(char *filename, char clientData[LINES][LENGTH]);
    void printClient(Client client);
    
    int main(int argc, char *argv[])
    {
    
      char *filename = NULL;
      if(argc > 1)
        filename = argv[1];
      else
      {
        printf("Usage: %s <filename>\n", argv[0]);
      }
      char clientData[LINES][LENGTH];
      readClientData(filename, clientData);
    
      Client client;
      strcpy(client.name, clientData[0]);
      strcpy(client.phoneNumber, clientData[1]);
      strcpy(client.location, clientData[2]);
      strcpy(client.traveltype, clientData[3]);
    
      printClient(client);
    
      return 0;
    }
    
    void readClientData(char *filename, char clientData[LINES][LENGTH])
    {
      FILE *file = fopen(filename, "r");
      if (file == NULL)
      {
        perror("Error while opening file!");
        exit(1);
      }
    
      char line[LENGTH];
      int i = 0;
      while (fgets(line, LENGTH, file) != NULL)
      {
        line[strcspn(line, "\n")] = 0;
        strcpy(clientData[i], line);
        i++;
      }
      fclose(file);
    }
    
    void printClient(Client client)
    {
        printf("%s, %s, %s, %s\n", client.name, client.phoneNumber, client.location, client.traveltype);
    }
    输出:
    
    Georgioss-MBP:Desktop gsamaras$ cat test.txt 
    MegasAlexandros
    3335632320
    Greece
    Cosmos
    Georgioss-MBP:Desktop gsamaras$ gcc main.c
    Georgioss-MBP:Desktop gsamaras$ ./a.out test.txt 
    MegasAlexandros, 3335632320, Greece, Cosmos
    
    2019-11-29 11:37:33
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载