gpt4 book ai didi

c - 为什么我的 strcpy() 不覆盖整个字符串并保留最后一个 char [] 中的字符?

转载 作者:行者123 更新时间:2023-11-30 18:53:19 24 4
gpt4 key购买 nike

我有一个简单的方法,它接受文件名和指向链接列表的指针。看起来链表没有什么问题。但是,我注意到由于某种原因, strcpy 似乎无法覆盖以前存在的字符串。每次我覆盖字符数组时,副本都会变得越来越糟。 为什么 strcpy 保留以前的数据?

void readFile(struct record ** recordArray, char inputArray [])
{

struct record ** temp = recordArray;
char theString [100];
char characterInput;
int counter = 0;
counter = 0;
FILE * infile = fopen(inputArray, "r");

char name [100];
char address [100];
int yearofbirth;
char telno [20];

int target = 0;
/*
0 name
1 address
2 yearofbirth
3 telno
*/

/*If the file exists*/
if (infile != NULL)
{
while (characterInput != EOF)
{
characterInput = fgetc(infile);

if (characterInput == '\n')
{
theString[counter] = '\0';

if (target == 0)/*name*/
{
strcpy(name, theString);
counter = 0;
target++;
}
else if (target == 1) /*address*/
{
strcpy(address, theString);
counter = 0;
target++;
}
else if (target == 2) /*yearofbirth*/
{
yearofbirth = atoi(theString);
counter = 0;
target++;
}
else if (target == 3) /*telephone number*/
{
strcpy(telno, theString);
counter = 0;
target = 0;
addRecord(temp, name, address, yearofbirth, telno);
}
}
else /*if the character is not a null line ie its a regular character*/
{
theString[counter] = characterInput;
counter++;
}
}
}
else
{
printf("Error: There has to be a file named: %s\n", inputArray);
}
fclose(infile);

}

输入:

bill
firstaddress
9119398644
1993

tim
birch st.
7567115
1980

roger
wood st drive
4830382
1909

输出:

Name: bill
Address: firstaddress
Birthyear: 529464052
Telephone Number: 1993398644ss

Name: tim3398644ss
Address: birch st.4ss
Birthyear: 7567115
Telephone Number: 1980115t.4ss

Name: timmy15t.4ss
Address: wood st drive
Birthyear: 4830382
Telephone Number: 1909382 drive

编辑:

这是最终运行的代码。谢谢大家。

void readFile(struct record ** recordArray, char inputArray [])
{

struct record ** temp = recordArray;
char theString [100];
char characterInput;
int counter = 0;
counter = 0;
FILE * infile = fopen(inputArray, "r");

char name [100];
char address [100];
int yearofbirth;
char telno [20];

int target = 0;
/*
0 name
1 address
2 yearofbirth
3 telno
*/

/*If the file exists*/
if (infile != NULL)
{
while (characterInput != EOF)
{
characterInput = fgetc(infile);

if (characterInput == '\n')
{
theString[counter] = '\0';

if (target == 0)/*name*/
{
strcpy(name, theString);
counter = 0;
target++;
}
else if (target == 1) /*address*/
{
strcpy(address, theString);
counter = 0;
target++;
}
else if (target == 2) /*yearofbirth*/
{
yearofbirth = atoi(theString);
counter = 0;
target++;
}
else if (target == 3) /*telephone number*/
{
strcpy(telno, theString);
counter = 0;
target = 0;
addRecord(temp, name, address, yearofbirth, telno);
}
}
else /*if the character is not a null line ie its a regular character*/
{
theString[counter] = characterInput;
counter++;
}
}
}
else
{
printf("Error: There has to be a file named: %s\n", inputArray);
}
fclose(infile);
}

最佳答案

您需要使用 '\0' 终止输入字符串。

更改:

    if (characterInput == '\n')
{
if (target == 0)/*name*/
{
...

至:

    if (characterInput == '\n')
{
theString[counter] = '\0'; // terminate input string

if (target == 0)/*name*/
{
...

关于c - 为什么我的 strcpy() 不覆盖整个字符串并保留最后一个 char [] 中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33142785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com