简体   繁体   English

fwrite写入不同的数据(C编程)

[英]fwrite to write different data (C Programming)

Introduction 介绍

Hello! 你好! This is a small C project that saves the user's first name, last name and prints it out. 这是一个小型C项目,它将保存用户的名字,姓氏并打印出来。

It would work without problems when you enter the first user. 当您输入第一个用户时,它将正常工作。

However, when you add another user, the program crashes. 但是,当您添加另一个用户时,程序崩溃。


Problem 问题

  • crashes when you add a second user 添加第二个用户时崩溃

Questions 问题

  • does fwrite save data as two different elements when I add a second user? 添加第二个用户时, fwrite将数据保存为两个不同的元素吗?

    For example: 例如:
    First User: 第一用户:

    First Name: John 名:约翰

    Last Name: Lemon 姓氏:柠檬

    Second User: 第二用户:

    First Name: James 名:詹姆斯

    Last Name: Bond 姓氏:Bond

  • Else if it doesn't, how do I separate the data's from each other? 否则,如何将数据彼此分开?


Any help would be greatly appreciated! 任何帮助将不胜感激! Thank you! 谢谢!

 #include<stdio.h> char user[]={"user.txt"}; struct user { char firstname[10]; char lastname[10]; }; void viewdata () { char input[20]; FILE *fp; struct user u1; fp = fopen(user,"rb"); fread(&u1,sizeof(u1),1,fp); printf("Please enter user first name you wish to search: "); scanf("%s",&input); if ((strcmp(input,u1.firstname)==0)) { printf("\\nUser First Name: %s",u1.firstname); printf("\\nUser Last Name: %s\\n\\n",u1.lastname); system("pause"); system("cls"); main(); } else printf("\\nNot found!\\n\\n"); system("pause"); system("cls"); main(); }; void enterdata () { char choice; FILE *fp; struct user u1; printf("\\nPlease enter first name: "); scanf("%s",&u1.firstname); fflush(stdin); printf("\\nPlease enter last name: "); scanf("%s",&u1.lastname); fflush(stdin); printf("\\nConfirm? (Y/N): "); scanf("%c",&choice); fflush(stdin); if (choice == 'y' || choice == 'Y') { fp=fopen(user,"a"); fwrite(&u1,sizeof(u1),1,fp); fclose(fp); system("cls"); main(); } else printf("\\nData has not been saved!"); system("pause"); main(); }; int main () { int choice; printf("\\n1. Enter data\\n"); printf("\\n2. View data\\n"); printf("\\n\\nPlease select an option: "); scanf("%d",&choice); fflush(stdin); if (choice == 1) { system("cls"); enterdata(); } else if (choice == 2); { system("cls"); viewdata(); } } 

If the file is successfully opened, the function returns a pointer to a FILE object that can be used to identify the stream on future operations. 如果文件成功打开,该函数将返回一个指向FILE对象的指针,该对象可用于在以后的操作中标识流。 Otherwise, a null pointer is returned. 否则,将返回空指针。

You should take care to check whether you were able to open the file successfully once you call fopen() by checking fp == NULL and erroring out if so. 您应该注意检查fp == NULL并错误地检查调用fopen()是否能够成功打开文件。 This will prevent the crash. 这样可以防止崩溃。

You're also reading it the wrong way. 您也在错误地阅读它。 If you use fwrite , make sure to use fread . 如果使用fwrite ,请确保使用fread

fscanf(fp,"%s %s",&u1.firstname,&u1.lastname); // wrong

Although I strongly advise on not storing binary data and retrieving them from a file, you should be doing something like this instead. 尽管我强烈建议不要存储二进制数据并从文件中检索它们,但是您应该这样做。

fread(&u1, sizeof(u1), 1, fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM