数组中的Strcpy与Strncpy [英] Strcpy versus Strncpy in arrays

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

问题描述



我很欣赏我收到的所有反馈。我很享受

在我的程序上工作,我希望它能够被欣赏。我现在有我的

程序编译,我正在继续解决这些错误。我有两个我似乎无法解决的问题:


1.我希望有人能够输入他们想要的员工数量

to ..并且对于输入的每个员工,我想保存来自

的数据,该条目在程序结束时打印出来。我继续运行

来解决循环问题。我实际上并不打算使用数组

然而我似乎无法想办法解决

问题。


2.这第二个问题可能是由于在程序中使用数组

,但是据说我还想把这个

拿出来....我试图连接两个字符串在一起,连接。我

了解strncpy指的是n个字符而不是strcpy

是实际字符但是我遇到的难度必须与

一起语法。很明显,我在某种程度上没有正确处理

,将内容放入内存然后提取它们。任何

的想法??


//稍后要添加的规格


// C库

#include< stdio.h>

#include< math.h>

#include< string.h>

//全局常量

#define FULLNAME 20

#define EMPLOYEES 1000

//全球定义常数

const float OT = 1.5;


//全局变量声明

FILE * inp;


/ /全球常数

#define FULLNAME 20

#define EMPLOYEES 1000


char ln [FULLNAME];

char department [20];

char;

int count_EMP;

int number_EMP;


浮动工资;

浮动OTwage;

浮动小时;

浮动RegHr;

浮动RegHrPay;

浮动OTHrPay;

浮动OTHr;

浮动GrossPay;



int main(无效)

{

/ * De结构很好。* /

struct EMP_WeeklyPay

{

char first_name [FULLNAME];

char last_name [ FULLNAME];

浮动RegHr;

浮动工资;

浮动OTHr;

浮动OTHrPay;

浮动GrossPay;

};

/ *重命名结构语法。* /

typedef struct EMP_WeeklyPay EWP;

/ *创建一个结构数组。* /

EWP emp [EMPLOYEES];


/ * Counters * /

int n,numemp;

int count = 0;


printf(" \ n\\\
Mountain Pacific Corporation \ n" ;);

printf(部门薪资计划\ n \\ nnn);

printf(请输入部门名称:) ;

scanf("%s",department);

/ *循环读取员工工资数据* /

count_EMP = 0 ;

count_EMP ++;


for(n = 0; n<雇员; ++ n){

do {

printf(&\\;输入员工#%d:",count_EMP);

scanf ("%s%s"& fn,& ln);


printf(&\\; \ n请输入员工的小时工资:

");

scanf("%f"& wage);


printf(" \\\
请输入数字工作时间为

周:");

scanf("%f",& hours);


printf(" \谢谢你。处理另一名员工?);

scanf("%s",& again);


} while(再次==''Y''||再次==''y'');


/ *读入输入* /


numemp = scanf("%11s%11s%f%f%f%f%f",& fn,& ln,& RegHr,

& wage,& OTHr,& OTHrPay,& GrossPay);


/ *检查用户是否完成* /


if(再次!=''Y''&&再次!=''y'') ;

printf(处理结束\ nn \ n \\ nnn);

/ *处理输入* /

if(numemp == 6)

{


if(RegHr> 40)

{

OTHr =小时 - 40;

OTHrPay = OT * OTHr *工资;

RegHrPay = 40.0 *工资;

}


其他

{

RegHrPay =小时*工资;

OTHrPay = 0.0;


}


GrossPay = RegHrPay + OTHrPay;

strncpy(emp [n] .first_name,fn,FULLNAME);

emp [n] .first_name [FULLNAME] =''\ 0'';


strncpy(emp [n] .last_name,ln,FULLNAME-1);

emp [n] .last_name [FULLNAME-1] =''\''' ;


emp [n] .RegHr = RegHr;

emp [n] .wage =工资;

emp [n ] .OTHr = OTHr;

emp [n] .OTHrPay = OTHrPay;

emp [n] .GrossPay = GrossPay;

++数量;

}


/ *打印表格* /


printf(" \ n \ nMountain Pacific Corporation \ nn);

printf(部门薪资计划\ n \\ nnn);


printf(" Employee)Reg Hrs"

" Overtime Hrs Gross \ nn");


printf(" ----------- ------------------------------"

" -------- ----------------- \ n \ n");


for(n = 0; n<计数; ++ n){

printf("% - 35s%-17s%12f%10f%12f%10f %% 5f",

\\ temp [n] .first_name ,

emp [n] .last_name,emp [n] .RegHr,

emp [n] .wage,emp [n] .OTHr,emp [n]。 OTHrPay,

emp [n] .GrossPay);

}

}

}

Hi
I appreciate all of the feedback I have recieved. I am enjoying
working on my program and I hope that it can be appreciated. I have my
program compiling now and I am continuing to work out the bugs. I have
two problems that I cannot seem to resolve:

1. I want to have someone enter in as many employees as they want
to..and for each employee entered I would like to save the data from
that entry to print out at the end of the program. I continue to run
into a problem with the loop. I actually did not intend to use arrays
however I cannot seem to figure outany other way to approach the
problem.

2.This second problem is probably due to using arrays
intheprogram,however that being said I still want to figure this
out....I am trying to connect two strings together, concatenation. I
understandthat strncpy refers to n number of characters versus strcpy
is the actual character but the difficulty I am running into has to do
with the syntax. It is evident that I am somehow not correctly dealing
with putting things into memory and then extracting them. Any
thoughts??

//Specs to be added later

//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>
//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000
//Global Defined Constant
const float OT = 1.5;

//Global Variable Declaratives
FILE*inp;

//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000

char fn[FULLNAME];
char ln[FULLNAME];
char department[20];
char again;
int count_EMP;
int number_EMP;

float wage;
float OTwage;
float hours;
float RegHr;
float RegHrPay;
float OTHrPay;
float OTHr;
float GrossPay;


int main(void)
{
/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
};
/*Rename the structure syntax.*/
typedef struct EMP_WeeklyPay EWP;
/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", department);
/*Loop to read in employee wage data*/
count_EMP = 0;
count_EMP++;

for (n = 0; n < EMPLOYEES; ++n){
do{
printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);

printf("\nPlease enter the hourly wage for the employee:
");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);

printf("\nThank you. Process another employee?");
scanf("%s", &again);

}while(again == ''Y'' || again == ''y'');

/*Read in the input*/

numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);

/*Check if user is done*/


if(again != ''Y'' && again !=''y'');
printf("End of processing\n\n\n");
/*Process the input*/
if(numemp == 6)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;

strncpy(emp[n].first_name, fn, FULLNAME);
emp[n].first_name[FULLNAME] = ''\0'';

strncpy(emp[n].last_name, ln, FULLNAME-1);
emp[n].last_name[FULLNAME-1] = ''\0'';

emp[n].RegHr = RegHr;
emp[n].wage = wage;
emp[n].OTHr = OTHr;
emp[n].OTHrPay = OTHrPay;
emp[n].GrossPay = GrossPay;
++count;
}

/*Print Table*/

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");

printf("Employee Reg Hrs "
"Overtime Hrs Gross\n");

printf("-----------------------------------------"
"-------------------------\n\n");


for(n=0; n < count; ++n) {
printf("%-35s%-17s%12f%10f%12f%10f%%5f",
emp[n].first_name,
emp[n].last_name, emp[n].RegHr,
emp[n].wage, emp[n].OTHr, emp[n].OTHrPay,
emp[n].GrossPay);
}
}
}

推荐答案

SK写道:
2.第二个问题可能是由于在程序中使用数组
正在说我还是想把这个
拿出来....我试图将两个字符串连接在一起,连接。我理解strncpy指的是n个字符而不是strcpy
是实际的字符,但我遇到的困难必须用语法来完成
。很明显,我在某种程度上没有正确处理
将内容放入内存然后提取它们。任何
想法??
2.This second problem is probably due to using arrays
intheprogram,however that being said I still want to figure this
out....I am trying to connect two strings together, concatenation. I
understandthat strncpy refers to n number of characters versus strcpy
is the actual character but the difficulty I am running into has to do
with the syntax. It is evident that I am somehow not correctly dealing
with putting things into memory and then extracting them. Any
thoughts??




在这里停止处理您的程序并启动另一个仅用于测试

如何strcpy和strncpy工作。另外,阅读

函数的文档。如果您遇到一些非常具体的问题,请发布一个

最小的例子 - 您不断增长的主要项目并不符合最低限度

的例子。


最后,为了处理任意字符串,你需要

malloc / realloc / free用于内存处理和strlen,strcpy和strcat

for字符串操作。 * n *变体通常只有在你使用固定大小的数组运行时才需要
,对于动态字符串,你最好先调整它们的大小。


Uli



Stop working on your program here and start another one just for testing
out how strcpy and strncpy work. Also, read the documentation of both
functions. If you have problems with some very specific thing, post a
minimal example - your growing main project doesn''t qualify as minimal
example.

Lastly, in order to handle arbitrary strings, you will need
malloc/realloc/free for the memory handling and strlen, strcpy and strcat
for the string operations. The *n* variants are usually only necessary if
you operate on fixed size arrays, for dynamic strings you better resize
them beforehand.

Uli


2005年11月13日12:04:08 -0800,在comp.lang.c,SK

< ba ****** @ aol.com>写道:
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"
<ba******@aol.com> wrote:

//全局定义常数
const浮动OT = 1.5;


你真的不想使用浮动 - 使用双倍。

scanf("%s",department);


正如我之前所说,scanf是一个很糟糕的功能。考虑

如果有人输入22个字符会发生什么。

for(n = 0; n< EMPLOYEES; ++ n){


通常做n ++

做{
printf(&\\;输入员工#%d:",count_EMP);
scanf("%s%) s",& fn,& ln);


fn和ln未定义。假设它们应该是字符串,

你不需要&符号。

scanf("%s",& again);

} while(再次==''Y''||再次==''y'');


又是一个字符串,而不是一个字符,所以你需要Y。和y。或者

更好,再次声明为角色并使用%c格式。

/ *读入输入* /


你从未要求用户输入任何...

numemp = scanf("%11s%11s%f%f%f%f%f",& fn,& ln ,& RegHr,
& wage,& OTHr,& OTHrPay,& GrossPay);

//Global Defined Constant
const float OT = 1.5;
You really don''t want to use float - use double.
scanf("%s", department);
As I said before, scanf is a bad function to use for this. Consider
what happens if someone enters 22 characters.
for (n = 0; n < EMPLOYEES; ++n){
usual to do n++
do{
printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);
fn and ln are not defined. Assuming they''re supposed to be strings,
you don''t need the ampersands.
scanf("%s", &again);

}while(again == ''Y'' || again == ''y'');
again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.

/*Read in the input*/
you never asked the user to enter any...
numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);




再次,fn和ln don''似乎是被定义的。

你似乎也有一些关于循环的大问题。


后退一步。写下_in english_(或您的母语),

描述程序必须做什么。仔细考虑每个数据需要获取和存储的方式。


然后,一旦你有了设计,就开始编写代码。

-

Mark McIntyre

CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt>


---- ==发表于Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet新闻== ----
http:/ /www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- = East和West-Coast服务器场 - 通过加密实现全隐私= ----



again, fn and ln don''t seem to be defined.
You also seem to have some big problems with loops.

Take a step back. Write down _in english_ (or your native language), a
description of what the programme has to do. Consider carefully how
each piece of data needs to be obtained and stored.

Then, once you have a design, start writing code.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


Mark McIntyre写道:
Mark McIntyre wrote:
fn和ln未定义。




他有一堆全局变量,这些都是其中两个。



He''s got a bunch of globals, and those are two of them.

scanf("%s",& ;;);

} while(再次= =''Y''||再次==''y'');
scanf("%s", &again);

}while(again == ''Y'' || again == ''y'');



再次是一个字符串,而不是一个字符,所以你需要Y。和y。或者更好的是,再次声明为角色并使用%c格式。



again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.




''再次''被声明为char。另一个全球


-

pete



''again'' was declared as a char. another global

--
pete


这篇关于数组中的Strcpy与Strncpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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