这只虫子是关于什么的?结构,指针,动态内存分配,C
我正在用c编写一个简单的银行应用程序 它将信息保存在一个文件中。 我想在每次应用程序运行时加载文件,并将文件中的信息添加到结构中,为此,我编写了两个函数,分别称为“loadfile”和“allocate” 在函数“loadfile”中,如果我取消对注释行的注释,操作系统会在我面前抛出一个“停止工作”:| 你能帮我吗? 当我在“加载文件”中使用(acc+I)时,会显示错误。 有语法问题吗o 谢谢这只虫子是关于什么的?结构,指针,动态内存分配,C,c,file,struct,allocation,scanf,C,File,Struct,Allocation,Scanf,我正在用c编写一个简单的银行应用程序 它将信息保存在一个文件中。 我想在每次应用程序运行时加载文件,并将文件中的信息添加到结构中,为此,我编写了两个函数,分别称为“loadfile”和“allocate” 在函数“loadfile”中,如果我取消对注释行的注释,操作系统会在我面前抛出一个“停止工作”:| 你能帮我吗? 当我在“加载文件”中使用(acc+I)时,会显示错误。 有语法问题吗o 谢谢 typedef结构{ 字符名[20]; int-id; 国际收支平衡; 炭枝[10]; }账户; 账户
typedef结构{
字符名[20];
int-id;
国际收支平衡;
炭枝[10];
}账户;
账户*acc;
内部分配(账户*acc){
int num=0;
字符tempname[20],tempbranch[10];
int tempid=-1,tempbalance;
文件*文件;
file=fopen(“D://bank.txt”,“r”);
而(!feof(文件)){
fscanf(文件“%s%d%d%s”、tempname、&tempid、&tempbalance、tempbranch);
如果(tempid!=-1)
num++;
}
acc=(账户*)realloc(账户,数量*大小);
fclose(文件);
printf(“分配函数中的num:%d”,num);
返回num;
}
int loadfile(帐户*acc){
int num=分配(acc);
字符tempname[20],tempbranch[10];
int tempid,tempbalance;
如果(num!=0){
int i=0;
文件*文件;
file=fopen(“D:\\bank.txt”、“r+”);
对于(i=0;!feof(文件)&&i名称),tempname);
(acc+i)->id=临时id;
(acc+i)->天平=临时天平;
strcpy(((acc+i)->分支机构),tempbrance*/
}
fclose(文件);
}
返回num;
}
我不能解释你所有的问题。这要花我几个小时。我希望这段代码是自我描述的。如果你需要帮助,请给我留言
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[20];
int id;
int balance;
char branch[10];
} account_t;
static account_t *parse_account_file(FILE *file, size_t *size) {
if (file == NULL || size == NULL) {
return NULL;
}
size_t i = 0;
account_t *account = malloc(sizeof *account);
if (account == NULL) {
return NULL;
}
int ret;
while (
(ret = fscanf(file, "%19s %d %d %9s\n", account[i].name, &account[i].id,
&account[i].balance, account[i].branch)) == 4) {
account_t *old = account;
account = realloc(account, sizeof *account * (++i + 1));
if (account == NULL) {
free(old);
return NULL;
}
}
if (ret == EOF) {
if (ferror(file)) {
perror("parse_account_file()");
} else {
*size = i;
account_t *old = account;
account = realloc(account, sizeof *account * i);
if (account == NULL) {
return old;
}
return account;
}
} else {
fprintf(stderr, "error parsing\n");
}
free(account);
return NULL;
}
int main(void) {
char const *name = "D:\\bank.txt";
FILE *file = stdin;
size_t size;
account_t *account = parse_account_file(file, &size);
fclose(file);
if (account == NULL) {
return 1;
}
for (size_t i = 0; i < size; i++) {
printf("%s %d %d %s\n", account[i].name, account[i].id, account[i].balance,
account[i].branch);
}
free(account);
}
#包括
#包括
类型定义结构{
字符名[20];
int-id;
国际收支平衡;
炭枝[10];
}账户;
静态帐户解析帐户文件(文件*文件,大小*size){
如果(文件==NULL | |大小==NULL){
返回NULL;
}
尺寸i=0;
账户=malloc(sizeof*账户);
如果(帐户==NULL){
返回NULL;
}
int ret;
当(
(ret=fscanf(文件,“%19s%d%d%9s\n”,帐户[i]。名称,&帐户[i]。id,
&科目[i]。余额,科目[i]。分行))==4){
账户_t*old=账户;
account=realloc(account,sizeof*account*(++i+1));
如果(帐户==NULL){
免费(旧);
返回NULL;
}
}
如果(ret==EOF){
如果(ferror(文件)){
perror(“parse_account_file()”);
}否则{
*尺寸=i;
账户_t*old=账户;
account=realloc(account,sizeof*account*i);
如果(帐户==NULL){
返老还童;
}
返回帐户;
}
}否则{
fprintf(stderr,“错误解析\n”);
}
免费(账户);
返回NULL;
}
内部主(空){
char const*name=“D:\\bank.txt”;
FILE*FILE=stdin;
大小;
account\t*account=解析\u account\u文件(文件和大小);
fclose(文件);
如果(帐户==NULL){
返回1;
}
对于(大小i=0;i
发布的代码有很多问题。尚不清楚单独的分配函数有何帮助,不建议使用文件范围变量acc
。我认为在这里使用realloc()
没有任何意义,因为分配只完成一次。如果确实使用了realloc()
,则应将结果存储在临时指针中,因为如果存在分配错误,函数可以返回NULL
指针。如果直接分配给要从中重新分配的指针,则会导致内存泄漏。我已经重写了代码来说明一些修复,试图保持原始代码的一般结构
您应该检查所调用函数的返回值realloc()
和malloc()
返回指向已分配内存的指针,或在发生分配错误时返回NULL
指针。您应该检查此值并处理结果。scanf()
函数返回成功分配的数量。您应该检查此值以验证输入是否符合预期。它是用feof()
来控制循环的,因为此函数依赖于正在设置的文件结束指示符,并且此指示符仅在I/O操作失败时设置
在下面的程序中,fgets()
用于将文件中的一行输入读取到缓冲区,而sscanf()
用于从缓冲区提取输入数据。在分配阶段,EOF
或空行表示数据结束。这里不进行解析,只进行行数。对于每一行,都为帐户
分配了空间。请注意,代码检查打开和关闭文件时的错误以及分配错误
loadfile()
函数再次使用fgets()
将一行输入读取到缓冲区中,然后使用sscanf()
扫描缓冲区。请注意字符串的宽度说明符的使用。它们比它们读入的数组的大小小一个,以便为sscanf()
放置在字符串末尾的'\0'
留出空间。还要注意的是,如果分配的任务少于4个,则程序退出时会显示一条错误消息。如果对临时变量的赋值成功,则帐户将使用数据更新
这段代码可以通过多种方式实现
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[20];
int id;
int balance;
char branch[10];
} account_t;
static account_t *parse_account_file(FILE *file, size_t *size) {
if (file == NULL || size == NULL) {
return NULL;
}
size_t i = 0;
account_t *account = malloc(sizeof *account);
if (account == NULL) {
return NULL;
}
int ret;
while (
(ret = fscanf(file, "%19s %d %d %9s\n", account[i].name, &account[i].id,
&account[i].balance, account[i].branch)) == 4) {
account_t *old = account;
account = realloc(account, sizeof *account * (++i + 1));
if (account == NULL) {
free(old);
return NULL;
}
}
if (ret == EOF) {
if (ferror(file)) {
perror("parse_account_file()");
} else {
*size = i;
account_t *old = account;
account = realloc(account, sizeof *account * i);
if (account == NULL) {
return old;
}
return account;
}
} else {
fprintf(stderr, "error parsing\n");
}
free(account);
return NULL;
}
int main(void) {
char const *name = "D:\\bank.txt";
FILE *file = stdin;
size_t size;
account_t *account = parse_account_file(file, &size);
fclose(file);
if (account == NULL) {
return 1;
}
for (size_t i = 0; i < size; i++) {
printf("%s %d %d %s\n", account[i].name, account[i].id, account[i].balance,
account[i].branch);
}
free(account);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int id;
int balance;
char branch[10];
} account;
account *acc = NULL;
int allocate(void)
{
int num = 0 ;
char buffer[1000];
FILE *file;
file = fopen("D://bank.txt","r");
if (file == NULL) {
fprintf(stderr, "Unable to open file in allocate()\n");
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), file) != NULL &&
buffer[0] != '\n') {
num++;
}
acc = malloc(num * sizeof(*acc));
if (acc == NULL) {
fprintf(stderr, "Allocation error in allocate()\n");
exit(EXIT_FAILURE);
}
if (fclose(file) != 0) {
fprintf(stderr, "Unable to close file in allocate()\n");
exit(EXIT_FAILURE);
}
printf(" num in allocate function : %d\n",num);
return num;
}
int loadfile(void)
{
int num = allocate();
char buffer[1000], tempname[20],tempbranch[10];
int tempid ,tempbalance;
if ( num != 0 ) {
int i = 0 ;
FILE *file;
file = fopen("D://bank.txt","r+");
if (file == NULL) {
fprintf(stderr, "Unable to open file in loadfile()\n");
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), file) != NULL
&& buffer[0] != '\n') {
if (sscanf(buffer, "%19s %d %d %9s",
tempname, &tempid, &tempbalance, tempbranch) != 4) {
fprintf(stderr, "%d: Malformed input data\n", i);
exit(EXIT_FAILURE);
}
strcpy(acc[i].name, tempname);
acc[i].id = tempid;
acc[i].balance = tempbalance;
strcpy(acc[i].branch, tempbranch);
++i;
}
if (fclose(file) != 0) {
fprintf(stderr, "Unable to open file in loadfile()\n");
exit(EXIT_FAILURE);
}
}
return num;
}
int main(void)
{
int num = loadfile();
for (int i = 0; i < num; i++) {
printf("%s %d %d %s\n",
acc[i].name, acc[i].id, acc[i].balance, acc[i].branch);
}
return 0;
}