获取工人的详细信息并将其写入文件 [英] Taking details of a worker and writing them to a file

查看:74
本文介绍了获取工人的详细信息并将其写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习C.我正在尝试编写一个程序,该程序询问工作人员数量并使用malloc函数分配内存。我创建了一个存储工人详细信息的结构。当我从用户那里获取详细信息时,一切正常,直到程序询问工人手机号码,程序停止工作。



#包括< stdio.h> 
#include< stdlib.h>

typedef struct
{
int eid;
char name [50];
unsigned int mobilenumber;
}工人;

int main()
{
int worker;

printf(输入工人数量:);
scanf(%d,& worker);

工人* ptr;
ptr =(工人*)malloc(工人* sizeof(工人));

printf(输入工人的移动数字:);
scanf(%u,ptr-> mobilenumber);
printf(输入工人的ID:);
scanf(%d,ptr-> eid);
printf(输入工人姓名:);
scanf(%[^ \ n],ptr-> name);

printf(%d,ptr-> eid);

free(ptr);
返回0;
}





我的尝试:



我认为问题是因为s​​canf函数而存在,但我无法找到错误。

解决方案

这里有几件事:

1)手机号码不是传统意义上的号码:它是一个字符串,而不是一个整数。不相信我?给你最好的伴侣而不使用领先的零...

如果你不能用它做数学,它不是一个数字,不应该这样存储。

(而且,移动电话号码可以包含其他字符,如括号和加号。)

这忽略了整数的大小因系统而异:有些可能只有16比特,这太短了,无论如何都不能打开手机号码。



2)当你这样做时:

 ptr-> mobilenumber 

取消引用 指针并返回整数中包含的值,而不是整数的地址。所以当你这样做时:

 scanf(%u,ptr-> mobilenumber); 

你将变量的内容传递给 scanf - 取决于你的编译器和选项,它是随机的零 - 它会把它当作一个地址。此时你的应用程序很可能崩溃或做一些非常奇怪的事情!

你需要传递整数的地址,而不是它的内容:

 scanf(%u ,&(ptr-> mobilenumber)); 

它适用于字符数组,因为数组的名称是指向数组本身第一个元素的指针。



我要做的是将用户输入读入一个字符数组,然后将其处理为你想要的变量,在我做之前检查它是否适合。


您应该使用并存储手机号码作为字符串。请查看此电话号码示例



比较电话号码时,需要进行一些规范化,例如删除()或处理+。比较总是右边



处理电话号码是一门艺术。 ; - )

I am still learning C. I am trying to write a program which asks for number of workers and assigns memory using malloc function. I created a structure which stores the details of the workers. When I am taking the details from the user everything works fine until the program asks for the workers mobile number the program stops to work.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
	int eid;
	char name[50];
	unsigned int mobilenumber;
} workers;

int main()
{
	int worker;
	
	printf("Enter the number of workers: ");
	scanf("%d", &worker);
	
	workers *ptr;
	ptr = (workers *)malloc( worker * sizeof(workers));
	
	printf("Enter the mobilenumber of the worker: ");
	scanf("%u", ptr->mobilenumber);
	printf("Enter the ID of the worker: ");
	scanf("%d", ptr->eid);
	printf("Enter the name of the worker: ");
	scanf(" %[^\n]", ptr->name);
	
	printf("%d", ptr->eid);
	
	free(ptr);
	return 0;
}



What I have tried:

I think the problem exists because of the scanf function but I am not able to find the fault.

解决方案

Couple of things here:
1) A mobile number is not a number in the traditional sense: it is a string, not an integer. Don't believe me? Phone your best mate without using a leading zero ...
If you can't do maths with it, it isn't a number, and should not be stored as such.
(And also, a mobile number can contain other characters such as brackets, and plus.)
This ignores that the size of integer varies from system to system: on some it may only be 16 bits, which is too short to holed a mobile number anyway.

2) When you do this:

ptr->mobilenumber

you dereference the pointer and return the value that is contained in the integer, not the address of the integer. So when you do this:

scanf("%u", ptr->mobilenumber);

you pass the content of the variable to scanf - which is either random of zero depending on your compiler and options - and it will treat it as an address. At that point your app is very likely to crash or do very strange things!
You need to pass the address of the integer, not it's content:

scanf("%u", &(ptr->mobilenumber));

It works for arrays of characters because the name of an array is a pointer to the first element of the array itself.

What I would do is read the user input into an array of characters, and then process it to the variable you want, checking that it fits before I did it.


You should use and store a mobile phone number as string. Take a look at this examples of phone numbers.

When comparing phone numbers you need some normalization like removing the "()" or dealing with "+". And comparision is always right sided.

Dealing with phone numbers is an art. ;-)


这篇关于获取工人的详细信息并将其写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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