使用指针从struct读取文件时出错 [英] Error reading a file from struct using pointers

查看:69
本文介绍了使用指针从struct读取文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法向我的结构添加联系人,但我收到一条错误,指出变量''curr''正在使用而未初始化。我已经查看了我的代码,但我可以我发现指针出错了。我的readfile方法如下:



I''m trying to add a contact to my struct using the following method but I''m getting an error stating that variable ''curr'' is being used without being initialised.I have reviewed my code but I can''t spot where I have gone gone wrong with the pointers.My readfile method is as follows:

struct contact *readFile(char * FName,struct contact *ptrList)
{

struct contact *head, *newContact;
FILE *fptr;
char oneLine[60];
char *sname, *fname, *phone,*company, *email;

head = ptrList;

fptr = fopen(FName,"r");

if(fptr == NULL)
{
    printf("\nCant open file!");
    return(ptrList);

}

fgets(oneLine, 55, fptr);
while(!feof(fptr))
{
    fgets(oneLine, 55, fptr);
    if(oneLine[strlen(oneLine)-1] == '\n')
    {
        oneLine[strlen(oneLine)-1] = '\0';

    }

    sname = strtok(oneLine,",");
    fname = strtok(NULL,",");
    phone = strtok(NULL,",");
    company = strtok(NULL,",");
    email = strtok(NULL,",");

    if(head == NULL)
    {
        head = (struct contact *)malloc(sizeof(struct contact));
        ptrList = head;
        strcpy(head->sname,sname);
        strcpy(head->fname,fname);
        strcpy(head->phone,phone);
        strcpy(head->company,company);
        strcpy(head->email, email);

        head->prev = NULL;
        head->next = NULL;


    }
    else
    {

        newContact = (struct contact *)malloc(sizeof(struct contact));
        head->next = newContact;
        newContact->prev = head;
        newContact->next = NULL;
        //copy the data to the new one
        strcpy(head->sname,sname);
        strcpy(head->fname,fname);
        strcpy(head->phone,phone);
        strcpy(head->company,company);
        strcpy(head->email,email);

        //move down the list so that the head variable
        //points to the last contact
        head = newContact;

    }

  }//end while

  fclose(fptr);
  return(ptrList);
}





这是我调用列表的方法,我在其中添加了一个memset来检查null:< br $> b $ b



This is the method where I call the list in which I have added a memset to check for null:

int addContact(struct contact *theList)
{
    struct contact *newContact, *curr;
    char fn[15],sn[15],ph[15],cmpy[15],eml[15];

    //create the new structure
    newContact = (struct contact *)malloc(sizeof(struct contact));
I have added a null check here:


    memset(newContact,0,sizeof(struct contact));
    if(newContact == NULL)
    {

        return(0);

    }
    //find the end of list
Here is where the error is being thrown:

    while(curr->next != NULL)
    {
    curr = theList;
    }
    //scroll through the list
    while(curr->next != NULL)
    {
        curr = curr->next;

    }
    //now have the last contact and the new one here
    printf("\nEnter a surname: ");
    gets(newContact->sname);

    printf("\nEnter a first name: ");
    gets(newContact->fname);

    printf("\nEnter a phone: ");
    gets(newContact->phone);

    printf("\nEnter a company: ");
    gets(newContact->company);

    printf("\nEnter an email: ");
    gets(newContact->email);

    //add the new contact to the end of the list

    curr->next = newContact;
    newContact->prev = curr;
    newContact->next = NULL;
    return(0);


}//end addContact





任何人都知道我搞砸指针的地方还是读取功能本身的问题?在此先感谢。



Anyone have any idea where I have messed up the pointer or is it a problem with read function itself? Thanks in advance.

推荐答案

Quote:

这里是错误的地方抛出:



while(curr-> next!= NULL)

Here is where the error is being thrown:

while(curr->next != NULL)

这是一个错误: curr 未在那里初始化。因此它可能指向垃圾。









顺便说一下,找到你应该写的列表的结尾

That''s a mistake: curr is not initialized there. Hence it probably points to garbage.




By the way, to find the end of the list you should write

curr = theList;
while(curr->next != NULL)
{
  curr = curr->next;
}
//now have the last contact and the new one here
//...



注意 theList 输入参数必须是一个有效指针(它必须包含已分配的的地址> struct contact 变量)。这可能是你职能的前提条件。您还可以在函数的第一行添加以下保护行:


Note that theList input parameter must be a valid pointer (it must contain the address of an allocated struct contact variable). That can be a precondition of your function. You may also add the following guard line at the very first line of you function:

if ( !theList) return -1;


这篇关于使用指针从struct读取文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆