2015-10-15 67 views
-1

我有一个简单的方法,需要一个文件名和一个指向链接列表的指针。链表似乎没有任何问题。但是,我注意到由于某种原因,strcpy似乎无法覆盖曾经存在的字符串。每次我覆盖char数组时,副本会越来越糟糕。 为什么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); 

}

INPUT:

bill 
firstaddress 
9119398644 
1993 

tim 
birch st. 
7567115 
1980 

roger 
wood st drive 
4830382 
1909 

OUTPUT:

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); 
} 
+3

时间来学习如何使用调试器。 – juanchopanza

+3

(1)当你阅读一个换行符时,你不会终止'theString'的空字符''\ 0''。 (2)当追加数据时,你不会防止'theString'溢出。 –

+0

@juanchopanza,我认为你是对的! – Tyler

回答

2

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

变化:

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

到:

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

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

非常感谢你!我以为我曾尝试过,但我想不是。再次感谢 – Tyler

0
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]; 
    char yearofbirth[100]; 
    char telno [100]; 

    char *buff=name; // initially is the name 
    int target = 0; 
    /* 
    0 name 
    1 address 
    2 yearofbirth 
    3 telno 
    */ 



    if (!infile) 
    { 
     printf("Error: There has to be a file named: %s\n", inputArray); 
     return; 
    } 
    char lastChar=0; 
    while (characterInput != EOF) 
    { 
     characterInput = fgetc(infile); 


     if (characterInput == '\n') 
     { 
      if(lastChar==characterInput) 
       continue; /*empty line*/ 

      target++; 
      target%=4; 
      *buff=0; // null terminate the string 
      switch(target){ 
       case 0:/*name*/ 
        addRecord(temp, name, address, atoi(yearofbirth), telno); 
        buff=name; 
        break; 
       case 1: /*address*/ 
        buff=address; 
        break; 
       case 2: /*yearofbirth*/ 
        buff=yearofbirth; 
        break; 
       case 3: /*telephone number*/ 
        buff=telno; 

       } 
     } 
     else /*if the character is not a null line ie its a regular character*/ 
     { 
      *buff = characterInput; 
      buff++; 
     } 
     lastChar=characterInput; 
    } 


    fclose(infile); 

    if(buff!=name) // last record? 
     addRecord(temp, name, address,atoi(yearofbirth), telno); 
} 
相关问题