阅读背景:

如何在不使用字符数组的情况下从文件中读取字符串?

来源:互联网 
#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 ;

 }
}
#include <stdio.h>
#include <stdlib.h>
#include



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: