加载链接列表文件指针(Loading a linklist file pointers)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
加载链接列表文件指针(Loading a linklist file pointers)

我创建了一个链表。 将链接列表发送到具有写入和读取选项的文件。 我坚持使用加载功能。 我也是2个月新来的C和这个网站的菜鸟。

我的代码:

void load( char filename[10], struct node *np){ // creating a temporary variable for holding a record char tmpfirstName[30]; char tmplastName[30]; char tmpPhoneNo[15]; char tmpeMail[55]; char tmpAddress[255]; int counter; // declare a file pointer FILE *input= fopen(filename, "r+"); //check if the file opened successfully if (input==NULL) perror ("Error opening file"); else{ counter=0; // continue in reading the file till the EOF while(!feof(input)){ strcpy(tmpfirstName," "); fscanf(input, "%s %s %s %s %s -=", tmpfirstName, tmplastName, tmpPhoneNo, tmpeMail, tmpAddress); if(strcmp(tmpfirstName," ")!=0){ strcpy(np->[counter].firstName, tmpfirstName); strcpy(np->[counter].lastName, tmplastName); strcpy(np->[counter].phoneNo, tmpPhoneNo); strcpy(np->[counter].eMail, tmpeMail); strcpy(np->[counter].address, tmpAddress); counter++; } } fclose(input); }

我得到的错误:

C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(134): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(135): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(136): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(137): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(138): error #2047: Expected a field name.

I have created a linkedlist. Sent the linkedlist to a file with a write and read option. I am stuck with with load function. I am also 2 months new to C and a noob to this site.

My Code:

void load( char filename[10], struct node *np){ // creating a temporary variable for holding a record char tmpfirstName[30]; char tmplastName[30]; char tmpPhoneNo[15]; char tmpeMail[55]; char tmpAddress[255]; int counter; // declare a file pointer FILE *input= fopen(filename, "r+"); //check if the file opened successfully if (input==NULL) perror ("Error opening file"); else{ counter=0; // continue in reading the file till the EOF while(!feof(input)){ strcpy(tmpfirstName," "); fscanf(input, "%s %s %s %s %s -=", tmpfirstName, tmplastName, tmpPhoneNo, tmpeMail, tmpAddress); if(strcmp(tmpfirstName," ")!=0){ strcpy(np->[counter].firstName, tmpfirstName); strcpy(np->[counter].lastName, tmplastName); strcpy(np->[counter].phoneNo, tmpPhoneNo); strcpy(np->[counter].eMail, tmpeMail); strcpy(np->[counter].address, tmpAddress); counter++; } } fclose(input); }

The errors I am getting:

C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(134): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(135): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(136): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(137): error #2047: Expected a field name. C:\Users\User\Documents\Pelles C Projects\FinalProject\PhoneBook.c(138): error #2047: Expected a field name.

最满意答案

你没有确定134-138行,但可能是范围包括行

strcpy(np->[counter].firstName, tmpfirstName);

如第134行。符号np->[counter].firstName是错误的。 给定函数参数列表中的声明,您需要np[counter].firstName 。

请注意, while (!feof(file))总是错误的 。

您应该直接测试I / O操作,例如使用:

while (fscanf(input, "%29s %29s %14s %54s %254s -=", tmpfirstName, tmplastName, tmpPhoneNo, tmpeMail, tmpAddress) == 5) { strcpy(np[counter].firstName, tmpfirstName); strcpy(np[counter].lastName, tmplastName); strcpy(np[counter].phoneNo, tmpPhoneNo); strcpy(np[counter].eMail, tmpeMail); strcpy(np[counter].address, tmpAddress); counter++; }

这些数字可以防止数据溢出字符数组。

请注意,大多数地址都有空白,扫描带有%s的字符串(有或没有大小限制)将跳过前导空格,收集变量中的非空白,但停在第一个空白处。 您可能需要使用扫描集: %254[^=] 。 您永远不会知道原始代码或修订后的代码是否匹配格式字符串末尾的-= 。

我注意到你最终需要升级函数,以便告诉它有多大的数组,这样它就不会溢出数组的边界。 在将数据复制到np数组之前,您需要添加一个检查。

您可能还应该升级该函数,以便它返回成功/失败指示,并且可能是一种机制,指示有多少记录被读入数组。 一种常见的方法可能是让函数返回一个整数,使用EOF(负数,通常为-1)表示问题,使用零或正数表示成功读取了多少条记录。

You've not identified lines 134-138, but presumably the range includes the line

strcpy(np->[counter].firstName, tmpfirstName);

as line 134. The notation np->[counter].firstName is erroneous. Given the declaration in the function parameter list, you need np[counter].firstName.

Please note that while (!feof(file)) is always wrong.

You should be testing the I/O operation directly, for example using:

while (fscanf(input, "%29s %29s %14s %54s %254s -=", tmpfirstName, tmplastName, tmpPhoneNo, tmpeMail, tmpAddress) == 5) { strcpy(np[counter].firstName, tmpfirstName); strcpy(np[counter].lastName, tmplastName); strcpy(np[counter].phoneNo, tmpPhoneNo); strcpy(np[counter].eMail, tmpeMail); strcpy(np[counter].address, tmpAddress); counter++; }

The numbers prevent the data overflowing your character arrays.

Note that most addresses have blanks in them and scanning a string with %s (with or without the size limit) will skip leading blanks, collect non-blanks in the variable, but stop at the first blank. You might need to use a scan-set: %254[^=] for example. You will never know with either the original code or the revised code whether the -= at the end of the format string was matched or not.

I note that you will eventually need to upgrade the function so that you tell it how big the array is, so that it does not overflow the bounds of the array. You would add a check before copying the data into the np array.

You should probably also upgrade the function so that it returns a success/failure indication, and probably a mechanism that indicates how many records were read into the array. One common way to do that might be to have the function return an integer, using EOF (a negative number, normally -1) to indicate problems, and a zero or a positive number to indicate how many records were read successfully.

更多推荐

本文发布于:2023-04-21 18:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/53f5002624c6b5af95c2c6e9ed8192a5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   加载   链接   文件   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!