尝试使用 strtok 从文件中分离字符串并存储在结构数组中
Trying to use strtok to separate string from a file and store in structure array
我写了下面的代码:
struct customer {
char name[30]
int age, phoneNo;
struct address {
int houseNumber;
} homeAddress;
};
void search()
{
char search[50];
char record[100];
const char s[2] = ",";
char* pStr;
struct customer c;
FILE* fPtr;
fPtr = fopen("customer.txt", "r");
// flag to check whether record found or not
int foundRecord = 0;
printf("Enter name to search : ");
// fgets gets name to search
scanf("%s", search);
//remove the '\n' at the end of string
while (fgets(record, 100, fPtr))
{
// strstr returns start address of substring in case if present
if (strstr(record, search))
{
char* pStr = strtok(record, ",");
if (pStr != NULL) {
strcpy(c.name, pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.age = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.phoneNo = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.homeAddress.houseNumber = atoi(pStr);
}
foundRecord = 1;
printf("Your details: ");
printf("%s, %d, %d, %d\n", c.name, c.age, c.phoneNo, c.homeAddress.houseNumber);
}
if (!foundRecord)
printf("%s cannot be found\n", search);
}
fclose(fPtr);
}
当我在我的 main 中调用 void search 函数时,代码运行不正常(我无法输入任何内容进行搜索,我得到一堆没有意义的符号)。基本上,我有一个名为 customer 的结构,我将其设为一个结构数组,我试图输入客户的详细信息(例如他们的姓名)以搜索他们的详细信息,将其存储到结构数组中并显示它。客户详细信息存储在如下文本文件中:
John Doe,10,123456789,5,Streets,Paris,P,60393
Mary Ann,39,12935837,5,Streetss,Cities,C,48354
我可以知道如何修复我的代码以使其正常工作吗?
要详细说明该函数在没有数组的情况下如何工作,它可能类似于:
void search()
{
char search[30]; // Match the length of the name in the structure
char record[100];
struct customer c;
FILE* fPtr;
fPtr = fopen("customer.txt", "r");
// TODO: Should really check for failure here
printf("Enter name to search : ");
scanf(" %29[^\n]", search); // Read at most 29 characters, plus the string terminator makes 30
// Read lines from the file
while (fgets(record, sizeof record, fPtr))
{
// strstr returns start address of substring in case if present
if (strstr(record, search))
{
// Found the record we're searching for
char* pStr = strtok(record, ",");
if (pStr != NULL) {
strcpy(c.name, pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.age = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.phoneNo = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.homeAddress.houseNumber = atoi(pStr);
}
printf("Your details: ");
printf("%s, %d, %d, %d\n", c.name, c.age, c.phoneNo, c.homeAddress.houseNumber);
// There's no need to read more data from the file
// So break out of the loop
break;
}
}
fclose(fPtr);
}
我写了下面的代码:
struct customer {
char name[30]
int age, phoneNo;
struct address {
int houseNumber;
} homeAddress;
};
void search()
{
char search[50];
char record[100];
const char s[2] = ",";
char* pStr;
struct customer c;
FILE* fPtr;
fPtr = fopen("customer.txt", "r");
// flag to check whether record found or not
int foundRecord = 0;
printf("Enter name to search : ");
// fgets gets name to search
scanf("%s", search);
//remove the '\n' at the end of string
while (fgets(record, 100, fPtr))
{
// strstr returns start address of substring in case if present
if (strstr(record, search))
{
char* pStr = strtok(record, ",");
if (pStr != NULL) {
strcpy(c.name, pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.age = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.phoneNo = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.homeAddress.houseNumber = atoi(pStr);
}
foundRecord = 1;
printf("Your details: ");
printf("%s, %d, %d, %d\n", c.name, c.age, c.phoneNo, c.homeAddress.houseNumber);
}
if (!foundRecord)
printf("%s cannot be found\n", search);
}
fclose(fPtr);
}
当我在我的 main 中调用 void search 函数时,代码运行不正常(我无法输入任何内容进行搜索,我得到一堆没有意义的符号)。基本上,我有一个名为 customer 的结构,我将其设为一个结构数组,我试图输入客户的详细信息(例如他们的姓名)以搜索他们的详细信息,将其存储到结构数组中并显示它。客户详细信息存储在如下文本文件中:
John Doe,10,123456789,5,Streets,Paris,P,60393
Mary Ann,39,12935837,5,Streetss,Cities,C,48354
我可以知道如何修复我的代码以使其正常工作吗?
要详细说明该函数在没有数组的情况下如何工作,它可能类似于:
void search()
{
char search[30]; // Match the length of the name in the structure
char record[100];
struct customer c;
FILE* fPtr;
fPtr = fopen("customer.txt", "r");
// TODO: Should really check for failure here
printf("Enter name to search : ");
scanf(" %29[^\n]", search); // Read at most 29 characters, plus the string terminator makes 30
// Read lines from the file
while (fgets(record, sizeof record, fPtr))
{
// strstr returns start address of substring in case if present
if (strstr(record, search))
{
// Found the record we're searching for
char* pStr = strtok(record, ",");
if (pStr != NULL) {
strcpy(c.name, pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.age = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.phoneNo = atoi(pStr);
}
pStr = strtok(NULL, ",");
if (pStr != NULL) {
c.homeAddress.houseNumber = atoi(pStr);
}
printf("Your details: ");
printf("%s, %d, %d, %d\n", c.name, c.age, c.phoneNo, c.homeAddress.houseNumber);
// There's no need to read more data from the file
// So break out of the loop
break;
}
}
fclose(fPtr);
}