如何将文件中的行读入字符串数组?
How to read lines from file into an array of strings?
#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[]
,但不知道如何使用。
要复制C字符串,您需要使用:
来自标准 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, %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
#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[]
,但不知道如何使用。
要复制C字符串,您需要使用:
来自标准 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, %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