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

时间:2016-09-15 16:09:22

标签: c

#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 ;

 }
}

这是代码。在这里,我使用数组来读取名称和电话号码,我想将该数组替换为指针。当我更换时,我只能阅读四个字符。

请有人帮我做,请告诉我它是否正确?

2 个答案:

答案 0 :(得分:1)

用这个替换你的结构;

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

然后每次使用新的PHONE实例时,分配你想要的内存;

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

或       pNew-&gt; szName =(char *)malloc(sizeOfNameBuffers);

答案 1 :(得分: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;
}

当您不需要时,也尽量避免使用全局变量。将变量放在堆栈中,如上例所示。