显示问题 [英] Showing problem

查看:81
本文介绍了显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我怎么能改变这段代码,显示我保存到文件中的人数?

它打印的垃圾很多。问题出在哪里?


#include< stdio.h>

int main()

{

int i;

FILE * data_file;

char list [] =" list.txt";


struct person {

char firstname [20];

char lastname [25];

int phonenumber;


};


struct person person_list [50];


for(i = 0; i< 1; i ++) {


printf("给你的名字:");

scanf("%s",person_list [i] .firstname);


printf("给你的姓氏:");

scanf("%s",person_list [i] .lastname);


printf(给你的电话号码:);

scanf("%d",& person_list [i] .phonenumber);


}


if((data_file = fopen(list," w"))== NULL){

printf(打开文件时出错);

返回0;

}否则{


对于(I = 0; I< = 2; i ++)

{

fprintf(data_file,"%s%s%d \ n",person_list [i] .firstname,

person_list [i] .lastname,person_list [i] .phonenumber);

}

}

fclose(data_file);

printf(数据保存成功!);

返回0;

}


How can i changhe this code showing how many persons i saved to the file?
And it prints pretty much rubbish. Where is the problem?

#include <stdio.h>
int main()
{
int i;
FILE *data_file;
char list[] = "list.txt";

struct person {
char firstname[20];
char lastname[25];
int phonenumber;

};

struct person person_list[50];

for(i=0; i<1; i++) {

printf("Give your firstname:");
scanf("%s", person_list[i].firstname);

printf("Give your lastname:");
scanf("%s", person_list[i].lastname);

printf("Give your phone number:");
scanf("%d", &person_list[i].phonenumber);

}

if ((data_file = fopen(list, "w")) == NULL) {
printf("Error opening file");
return 0;
} else {

for(i=0; i<=2; i++)
{
fprintf(data_file, "%s %s %d\n", person_list[i].firstname,
person_list[i].lastname, person_list[i].phonenumber);
}
}
fclose(data_file);
printf("Data saved succesfully!");
return 0;
}

推荐答案

问题在于您的设计本身。你声明了一个50

的数组,如
The problem is with your design itself. You declared an array of 50
like
struct person person_list [50];
但是在下面的陈述中你只循环一个人的

输入:

for(i = 0; i< 1; i ++){

printf(" Give your firstname:");
scanf("%s",person_list [i] .firstname);

printf( 给你的姓氏:;;
scanf("%s",person_list [i] .lastname);

printf("给你的电话号码:") ;
scanf("%d",& person_list [i] .phonenumber);

}


打印到文件你正在打印两个人喜欢(i = 0; i <= 2; i ++)
{f / f>(data_file,"%s%s%d \ n",person_list [i] .firstname ,
person_list [i] .lastname,person_list [i] .phonenumber);
}
}
struct person person_list[50]; but in the following statement you are looping for only one person''s
input:
for(i=0; i<1; i++) {

printf("Give your firstname:");
scanf("%s", person_list[i].firstname);

printf("Give your lastname:");
scanf("%s", person_list[i].lastname);

printf("Give your phone number:");
scanf("%d", &person_list[i].phonenumber);

}
For printing to the file you are printing for two persons likefor(i=0; i<=2; i++)
{
fprintf(data_file, "%s %s %d\n", person_list[i].firstname,
person_list[i].lastname, person_list[i].phonenumber);
}
}




解决方案是

1)获取所需数量的人的输入但是小于数组

长度。

2)将数据写入fil e,。

3)对于上述两个步骤,你必须将for循环更改为

适应数组长度。

4)阅读文件返回并检查你的元数之前

遇到EOF。


-Shan



The solution is
1) Get input for as many persons you want but lesser than the array
length.
2) write the data to the file,.
3) For the above two steps you have to change the for loop to
accomodate the array length.
4) Read the file back and check for the number of lines before you
encounter EOF.

-Shan


beginner10< pe ************ @ yahoo.com>写道:
beginner10 <pe************@yahoo.com> wrote:
我怎么能改变这段代码,显示我保存到文件中的人数?
它打印的垃圾很多。问题出在哪儿?
#include< stdio.h>
int main()


int main(无效)

{
int i;
FILE * data_file;
char list [] =" list.txt" ;;

struct person {
char firstname [20];


使用像这样的魔术数字是有问题的。更好地使用定义

程序的开头如



并坚持使用所以,你只需要在一个地方改变它

当你后来意识到19个字符不足以存储所有

名字时。

char lastname [25];
int phonenumber;
};

struct person person_list [50];


再次,我建议使用


#define MAX_PERSONS 50


而不是固定号码。

for(i = 0; i< 1; i ++){


为什么你会在这里停一次''我'是1?如果''我'是50或者用户输入的东西

你可以从中推断出来的话,那么它是否更有意义

来摆脱困境( s)他不想输入更多数据

(就像名字的空行一样)?

printf("给你的名字:") ;
scanf("%s",person_list [i] .firstname);


让我们希望名字中没有空格,否则使用

scanf()就好了work(提示:scanf()在白色空格处停止

字符在读取带有%s的字符串时)。而且,更好的

希望没有一个名字超过19

字符的人或你写过数组的末尾。作为

纠正这两个问题的方法,查找fgets()函数。

printf(" Give your lastname:");
scanf(" ;%s",person_list [i] .lastname);
printf("给你的电话号码:");
scanf("%d",& person_list [i] .phonenumber);


如果用户输入

错误并输入非数字(可能只是空格),则使用scanf()会出现问题。再次,查看

fgets()并查看你从中得到的缓冲区,以获得可以将
解释为电话号码的内容。

if((data_file = fopen(list," w"))== NULL){
printf(" Error opening file");
返回0;
}其他{


实际上,这里不需要''其他' - 当打开文件失败时

已经退出程序。

for(i = 0; i< = 2; i ++)


因为当''i'为1时你停在上面你永远不会超过列表中有一个单独的
人,你为什么要尝试写出3套

数据记录?更好地统计

用户输入的人数,并写出多少记录。

{
fprintf(data_file,"%s%s %d \ n",person_list [i] .firstname,
person_list [i] .lastname,person_list [i] .phonenumber);
}
}
fclose(data_file );
printf(数据保存成功!);
返回0;
}
How can i changhe this code showing how many persons i saved to the file?
And it prints pretty much rubbish. Where is the problem? #include <stdio.h>
int main()
int main( void )
{
int i;
FILE *data_file;
char list[] = "list.txt";

struct person {
char firstname[20];
Using ''magic'' numbers like this is problematic. Better use a define at
the start of the program like

#define MAX_FIRST_NAME_LENGTH 20

and stick with that, so you only need to change it in a single place
when you later realize that 19 characters isn''t enough to store all
first names.
char lastname[25];
int phonenumber; };

struct person person_list[50];
Again, I would recommend to use

#define MAX_PERSONS 50

instead of a fixed number.

for(i=0; i<1; i++) {
Why would you stop here once ''i'' is 1? Wouldn''t it make more sense
to bail out of the loop if ''i'' is 50 or the user enters something
from which you can deduce that (s)he doesn''t want to enter more data
(like an empty line for the first name)?
printf("Give your firstname:");
scanf("%s", person_list[i].firstname);
Let''s hope there''s no-one with a space in the name, otherwise using
scanf() like that won''t work (hint: scanf() stops at white-space
characters when reading in strings with "%s"). And moreover, better
hope that there''s no-one with a first name that''s longer than 19
characters or you write past the end of the array. As a way to
correct both problems look up the fgets() function.
printf("Give your lastname:");
scanf("%s", person_list[i].lastname); printf("Give your phone number:");
scanf("%d", &person_list[i].phonenumber);
Using scanf() here is is going to be a problem if the user makes a
typo and enters an non-digit (maybe just a space). Again, look up
fgets() and check the buffer you get from it for something that can
be interpreted as a phone number.
}

if ((data_file = fopen(list, "w")) == NULL) {
printf("Error opening file");
return 0;
} else {
Actually, no ''else'' is needed here - when opening the file failed you
already exited the program.
for(i=0; i<=2; i++)
Since you stop above when ''i'' is 1 you never have more than a single
person in your list, Why would you try to write out now 3 sets of
data records? Better keep count how many people got entered by the
user and than write out as many records.
{
fprintf(data_file, "%s %s %d\n", person_list[i].firstname,
person_list[i].lastname, person_list[i].phonenumber);
}
}
fclose(data_file);
printf("Data saved succesfully!");
return 0;
}



问候,Jens

-

\ Jens Thoms Toerring ___ Je * **********@physik.fu-berlin.de

\ __________________________ http://www.toerring.de




" beginner10" < PE ************ @ yahoo.com>在消息中写道

新闻:d9 ****************************** @ localhost.ta lkaboutprogramming。 com ...

"beginner10" <pe************@yahoo.com> wrote in message
news:d9******************************@localhost.ta lkaboutprogramming.com...

我怎么能改变这段代码,显示我保存到文件中的人数?
它打印的垃圾很多。问题出在哪里?

#include< stdio.h>
int main()
{
int i;
FILE * data_file;
char list [] =" list.txt";

struct person {
char firstname [20];
char lastname [25];
int phonenumber;


更好地使用char数组用于phonenumber字段....

让用户输入类似于:9899325025

then它会溢出并存储错误的信息。

使用char数组你有更精确的控制

你要在多少位数存储在phonenumber中

field。


};

struct person person_list [50];


哎呀!使用#defines而不是幻数....

喜欢#define MAX_RECORDS 50

for(i = 0; i< 1; i ++){

printf("给你的名字:");
scanf("%s",person_list [i] .firstname);

printf("给你的姓氏:");
scanf("%s",person_list [i] .lastname);

printf("提供您的电话号码:");
scanf("%d",& person_list [i] .phonenumber);

}
if((data_file = fopen(list," w")) == NULL){
printf(" Error opening file");
return 0;
} else {

for(i = 0; i< = 2; i ++)
{


你只在第一条记录中有有效数据,但在这里你要访问

并存储第二和第三行...包含垃圾。

要么在程序开始时正确初始化整个数组

如果你想存储更多的记录然后从用户输入。

或更好保持这个循环计数器与上面相同。


希望这将有助于U. ...


-Neo

fprintf(data_file,"%s%s%d \ n",person_list [i] .firstname,
person_list [i] .lastname,person_list [i] .phonenumber);
}
} fclose(data_file);
printf(数据已成功保存! ;);
返回0;
}

How can i changhe this code showing how many persons i saved to the file?
And it prints pretty much rubbish. Where is the problem?

#include <stdio.h>
int main()
{
int i;
FILE *data_file;
char list[] = "list.txt";

struct person {
char firstname[20];
char lastname[25];
int phonenumber;
better to use char array for phonenumber field too....
lets say user input something like : 9899325025
then it will overflow and store wrong information.
by using char array you have more precise control
over how many digits you want to store in phonenumber
field.


};

struct person person_list[50];
Oops! use #defines instead of magic numbers....
like #define MAX_RECORDS 50

for(i=0; i<1; i++) {

printf("Give your firstname:");
scanf("%s", person_list[i].firstname);

printf("Give your lastname:");
scanf("%s", person_list[i].lastname);

printf("Give your phone number:");
scanf("%d", &person_list[i].phonenumber);

}

if ((data_file = fopen(list, "w")) == NULL) {
printf("Error opening file");
return 0;
} else {

for(i=0; i<=2; i++)
{
you have valid data only in first record, but here you are accessing
and storing second and third row also... that contains garbage.
either properly initialize the whole array in the begining of the program
if you want to store more records then input from the user.
or better keep this loop counter same as above.

Hope this will help U....

-Neo
fprintf(data_file, "%s %s %d\n", person_list[i].firstname,
person_list[i].lastname, person_list[i].phonenumber);
}
}
fclose(data_file);
printf("Data saved succesfully!");
return 0;
}



这篇关于显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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