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

#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[],但不知道如何使用。

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

要复制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

如果将结构体的字段保留为指向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\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

PS:我在示例中使用了此命令:Removing trailing newline character from fgets() input

本文链接:https://www.f2er.com/3011443.html

大家都在问