내 strcpy ()가 전체 문자열을 덮어 쓰지 않고 마지막 문자 []의 문자를 유지하지 않는 이유는 무엇입니까?

타일러

파일 이름과 연결 목록에 대한 포인터를받는 간단한 방법이 있습니다. 연결 목록에 문제가없는 것 같습니다. 그러나 어떤 이유로 strcpy가 거기에 있던 문자열을 덮어 쓰지 못하는 것 같습니다. char 배열을 덮어 쓸 때마다 복사본이 점점 더 나빠집니다. 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);
}
폴 R

입력 문자열은 '\0'.

변화:

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

에:

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

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관