#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 개의 문자 만 읽을 수있다.문자 배열을 사용하지 않고 파일에서 문자열을 읽는 방법은 무엇입니까?
누구나 도와 주시겠습니까? 제발 정확하지 않습니까?
Em ... null 문자로 끝나는 배열 인 문자열을 저장하려는 경우 어떻게 든 배열이 필요합니다. –
포인터가 뭔가를 가리 킵니다. 그것들을'char * '로 만들 수는 있지만 버퍼에 데이터를 저장하려면 배열을 어떤 방식 으로든 할당해야합니다. 이 경우에는 리소스의 의미가 전혀 없습니다. 고정 된 크기의 구조로 전체를 읽고 쓸 수 있기 때문에 모든 것을 훨씬 쉽게 할 수 있습니다. – Havenard