2016-09-15 2 views
0
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define  ZERO   (0) 
#define  ONE   (1) 
#define  TEN   (10) 
#define TWENTY   (20) 
#define FOURTY_SEVEN  (47) 
#define FIFTY_SEVEN  (57) 

typedef struct PHONE 
{ 
char szName[20] ; 

char szPhone[20] ; 

struct PHONE *pNext ; 
}; 

struct PHONE *pFirst    = NULL ; // To denote start of the linked list 

struct PHONE *pPointer   = NULL ; // To denote current end node in linked list 

struct PHONE *pNew    = NULL ; // To denote the new node 

struct PHONE *pTemp    = NULL ; // To store pointer temporarily 

struct PHONE *pTempForDeallocation = NULL ; // To store pointer for deallocation 

struct PHONE stPhone   ; 

FILE *fPointerForOpen    ;   // To Open the file 

int FilePresentOrNot()   ;   // To Check the file is present or not 

int MemoryAllocation()   ;   // To Allocate Memory to the Linked List 

int main() 
{ 
    int iChoice  = 0 ; 

    int iRepeat  = 0 ; 

    int iLength  = 0 ; 

    do 
    { 
    fPointerForOpen = fopen("phonebook.txt","r"); 

    fflush(stdin); 

    system("cls"); 

    if(NULL == fPointerForOpen) 
    { 
     FilePresentOrNot(); 
    } 

    // stPhone.szName = (char *)malloc(100); 

    while(fscanf(fPointerForOpen,"%s",stPhone.szName)!= EOF) 
    { 
     fscanf(fPointerForOpen,"%s",stPhone.szPhone); 

     pNew = (struct PHONE *) malloc(sizeof(struct PHONE)) ; 

     strcpy(pNew -> szName , stPhone.szName) ; 

     strcpy(pNew -> szPhone , stPhone.szPhone) ; 

     pNew ->pNext = NULL ; 

     MemoryAllocation() ; 
    } 
    printf("\n\nDo you Want to continue then press 1 ? \t"); 

    scanf("%d",&iRepeat); 

    fclose(fPointerForOpen); 

    pFirst = NULL ; 

}while(ONE == iRepeat); 
return 0 ; 
} 


int MemoryAllocation() 
{ 
if(NULL == pFirst) 
    { 
    pFirst = pNew ; 

    pPointer = pNew ; 

} 
else 
    { 
    pPointer->pNext = pNew ; 

    pPointer  = pNew ; 

} 
} 

다음은 코드입니다. 여기서 배열을 포인터로 바꾸려는 배열의 이름과 전화 번호를 읽었습니다. 내가 바꿀 때 나는 단지 4 개의 문자 만 읽을 수있다.문자 배열을 사용하지 않고 파일에서 문자열을 읽는 방법은 무엇입니까?

누구나 도와 주시겠습니까? 제발 정확하지 않습니까?

+1

Em ... null 문자로 끝나는 배열 인 문자열을 저장하려는 경우 어떻게 든 배열이 필요합니다. –

+1

포인터가 뭔가를 가리 킵니다. 그것들을'char * '로 만들 수는 있지만 버퍼에 데이터를 저장하려면 배열을 어떤 방식 으로든 할당해야합니다. 이 경우에는 리소스의 의미가 전혀 없습니다. 고정 된 크기의 구조로 전체를 읽고 쓸 수 있기 때문에 모든 것을 훨씬 쉽게 할 수 있습니다. – Havenard

답변

1

구조체를 다음과 같이 바꾸십시오.

typedef struct PHONE 
    { 
     char *szName ; 
     char *szPhone ; 
     struct PHONE *pNext ; 
    }; 

그런 다음 PHONE의 새 인스턴스를 사용할 때마다 원하는 메모리를 할당하십시오.

aPhone.szName = (char *)malloc(sizeOfNameBuffers); 

또는 pNew-> szName = (숯 *)의 malloc (sizeOfNameBuffers);

1

계속 파일을 열고 닫습니다. 파일을 열 때마다 커서가 파일의 시작 부분으로 이동하고, 1 행을 반복해서 읽습니다. 이 예제를 대신 사용해보십시오 :

struct PHONE 
{ 
    char szName[20]; 
    char szPhone[20]; 
    struct PHONE *pNext; 
}; 

int main() 
{ 
    FILE *fPointerForOpen; 
    struct PHONE *pFirst = NULL; // To denote start of the linked list 
    struct PHONE *pPointer = NULL; // To denote current end node in linked list 
    struct PHONE *pNew = NULL; // To denote the new node 
    struct PHONE stPhone; 

    fPointerForOpen = fopen("phonebook.txt", "r"); 
    if (!fPointerForOpen) 
    { 
     printf("cannot read file\n"); 
     return 0; 
    } 

    while(fscanf(fPointerForOpen, "%s %s", stPhone.szName, stPhone.szPhone) == 2) 
    { 
     pNew = (struct PHONE*)malloc(sizeof(struct PHONE)); 
     strcpy(pNew->szName, stPhone.szName); 
     strcpy(pNew->szPhone, stPhone.szPhone); 
     pNew->pNext = NULL; 

     if (!pFirst) 
      pFirst = pNew; 
     else 
      pPointer->pNext = pNew; 

     pPointer = pNew; 
    } 

    fclose(fPointerForOpen); 

    //print the list: 
    pPointer = pFirst; 
    printf("Testing:\n"); 
    while (pPointer) 
    { 
     printf("%s %s\n", pPointer->szName, pPointer->szPhone); 
     pPointer = pPointer->pNext; 
    } 

    return 0; 
} 

또한 필요하지 않을 때 전역 변수를 사용하지 마십시오. 위의 예제와 같이 변수를 스택에 넣으십시오.

관련 문제