Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
2年前关闭。
}
它将更改名称和职位,但是对于薪水,它将显示违反权限。我想更改薪水并存储新薪水,但是它不能存储编译器执行直到行确认要修改的新薪水?然后停止执行。职员P代表修改函数中的所有P [i]
输出:
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
2年前关闭。
typedef struct staff {
int id, salary;
char name[30], position[30];
}staff;
void modifyStaff() {
char ans, cont, name[25], position[20];
int i = 0, pCount, modiCount = 0, found, id[20];
int salary[20];
staff P[20];
FILE*fp;
fp = fopen("staff.dat", "rb");
while (fread(&P[i], sizeof(staff), 1, fp))
i++;
pCount = i;
fclose(fp);
do {
printf("\nEnter ID of the Staff to be modified : ");
rewind(stdin);
scanf("%d", &id);
found = 0;
printf("\nID NAME POSITION SALARY \n");
printf("============ =========== ======= \n");
for (i = 0; i < pCount; i++) {
if (id == P[i].id == 0) {
found = 1;
printf("%-18d %-10s %-10s %-10d \n",
P[i].id, P[i].name, P[i].position, P[i].salary);
printf("\n Updated Name:");
scanf("%[^\n]", &name);
printf("\n Updated Position:");
scanf("%[^\n]", &position);
printf("\n Updated salary:");
scanf("%d", &salary);
printf("Confirm to Modify (Y=yes)? ");
scanf("%c", &ans);
if (toupper(ans) == 'Y') {
P[i].id = id;
strcpy(P[i].name, name);
strcpy(P[i].position, position);
P[i].salary=salary;
modiCount++;
}
printf("Updated Staff Records:\n");
printf("\nID NAME POSITION SALARY\n");
printf("======== ========= =========== ========\n");
printf("%-18d %-10s %-10s %-10d", P[i].id, P[i].name, P[i].position, P[i].salary);
}
}
if (!found)
printf("NO record founded with this ID");
printf("Any more record to modify?(Y=yes)?");
scanf("%c", &cont);
} while (toupper(cont) == 'Y');
fp = fopen("staff.dat", "wb");
for (i = 0; i < pCount; i++)
fwrite(&P[i], sizeof(staff), 1, fp);
fclose(fp);
printf("\n\t%d Record(s) modified.\n\n", modiCount);
}
它将更改名称和职位,但是对于薪水,它将显示违反权限。我想更改薪水并存储新薪水,但是它不能存储编译器执行直到行确认要修改的新薪水?然后停止执行。职员P代表修改函数中的所有P [i]
最佳答案
就像我在评论中说的那样:strcpy()
和strncpy()
函数返回指向目标字符串dest的指针。
如果发现s1(或其前n个字节)分别小于,匹配或大于s2,则strcmp()
和strncmp()
函数将返回小于,等于或大于零的整数。 。
以下程序应解释这两种功能的用法:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 256
struct pers{
char dest[SIZE];
};
int main ( void ){
struct pers data;
const char *src = "Hello";
if ( ( strlen( src ) + 1) < SIZE ){
strcpy( data.dest, src );
}else{
printf("The SRC is bigger then DEST\n");
exit(EXIT_FAILURE);
}
if ( strcmp( data.dest, src ) == 0 ){
printf("SRC and DEST are equal:\n\n");
printf("\tSRC = %s\n", src);
printf("\tDEST = %s\n", data.dest);
}else{
printf("SRC and DEST are NOT equal\n");
}
}
输出:
SRC and DEST are equal:
SRC = Hello
DEST = Hello
关于c - strcpy和strcmp以及printf和scanf的参数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47853934/
10-12 16:08