为什么strcpy';d在C中
源代码:为什么strcpy';d在C中,c,file,character-encoding,C,File,Character Encoding,源代码: char CUSTOMERS_FILE[50] = "customers.txt"; typedef struct Customer { char name[50]; char password[50]; char billing_address[100]; char phone_number[15]; double amount_paid; double amount_due; char date[20]; } Custome
char CUSTOMERS_FILE[50] = "customers.txt";
typedef struct Customer {
char name[50];
char password[50];
char billing_address[100];
char phone_number[15];
double amount_paid;
double amount_due;
char date[20];
} Customer;
char* read_string(int length) {
char data[length];
rewind(stdin);
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
return data;
}
void handle_modify_customer(Customer customer) {
Customer edited_details;
printf("\nMODIFYING DETAILS\n==============\n\n");
printf("CREATE A CUSTOMER PROFILE\n=========================\n");
printf("Name (%s): ", customer.name);
strcpy(edited_details.name, read_string(50));
printf("Password (%s): ", customer.password);
strcpy(edited_details.password, read_string(50));
printf("Billing Address (%s): ", customer.billing_address);
strcpy(edited_details.billing_address, read_string(100));
printf("Phone Number (%s): ", customer.phone_number);
strcpy(edited_details.phone_number, read_string(15));
printf("Amount Paid (%10.2lf): ", customer.amount_paid);
scanf("%lf", &edited_details.amount_paid);
printf("Amount Due (%10.2lf): ", customer.amount_due);
scanf("%lf", &edited_details.amount_due);
printf("Payment Date (%s): ", customer.date);
strcpy(edited_details.date, read_string(20));
/*
if (strlen(edited_details.name) == '\0' || strlen(edited_details.billing_address) == '\0' || strlen(edited_details.password) == '\0' || strlen(edited_details.phone_number) == '\0' || strlen(edited_details.date) == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}*/
if (edited_details.name[0] == '\0' || edited_details.billing_address[0] == '\0' || edited_details.password[0] == '\0' || edited_details.phone_number[0] == '\0' || edited_details.date[0] == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}
FILE *file = fopen(CUSTOMERS_FILE, "r");
FILE *new_file = fopen("customers_new.txt", "ab+");
Customer record;
while (fscanf(file, "[%[^]]], [%[^]]], [%[^]]], [%[^]]], [%lf], [%lf], [%[^]]]\n",
record.name, record.password, record.billing_address, record.phone_number,
&record.amount_paid, &record.amount_due, record.date) == 7)
{
if (strcmp(customer.name, record.name) == 0) {
printf("P: %s\nD: %s", edited_details.phone_number, edited_details.date);
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", edited_details.name, edited_details.password, edited_details.billing_address, edited_details.phone_number, edited_details.amount_paid, edited_details.amount_due, edited_details.date);
} else {
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", record.name, record.password, record.billing_address, record.phone_number, record.amount_paid, record.amount_due, record.date);
}
}
fclose(file);
fclose(new_file);
remove(CUSTOMERS_FILE);
rename("customers_new.txt", CUSTOMERS_FILE);
printf("\nThe customer details have been successfully modified!\n");
key_to_continue();
}
MODIFYING DETAILS
==============
CREATE A CUSTOMER PROFILE
=========================
Name (dumbfk): test
DATA: testPassword (abc123): lol
DATA: lolBilling Address (pukima jalan): lol
DATA: lolPhone Number (6969696969): 499449
DATA: 499449Amount Paid ( 6969.00): 499449
Amount Due (6969699.00): 499494
Payment Date (6/9/1969): 22/2/2000
DATA: 22/2/2000P: �O���
D: �O���
The customer details have been successfully modified!
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[dumbfk], [abc123], [pukima jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[test], [lol], [lol], [�O���], [499449.000000], [499494.000000], [�O���]
执行示例:
char CUSTOMERS_FILE[50] = "customers.txt";
typedef struct Customer {
char name[50];
char password[50];
char billing_address[100];
char phone_number[15];
double amount_paid;
double amount_due;
char date[20];
} Customer;
char* read_string(int length) {
char data[length];
rewind(stdin);
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
return data;
}
void handle_modify_customer(Customer customer) {
Customer edited_details;
printf("\nMODIFYING DETAILS\n==============\n\n");
printf("CREATE A CUSTOMER PROFILE\n=========================\n");
printf("Name (%s): ", customer.name);
strcpy(edited_details.name, read_string(50));
printf("Password (%s): ", customer.password);
strcpy(edited_details.password, read_string(50));
printf("Billing Address (%s): ", customer.billing_address);
strcpy(edited_details.billing_address, read_string(100));
printf("Phone Number (%s): ", customer.phone_number);
strcpy(edited_details.phone_number, read_string(15));
printf("Amount Paid (%10.2lf): ", customer.amount_paid);
scanf("%lf", &edited_details.amount_paid);
printf("Amount Due (%10.2lf): ", customer.amount_due);
scanf("%lf", &edited_details.amount_due);
printf("Payment Date (%s): ", customer.date);
strcpy(edited_details.date, read_string(20));
/*
if (strlen(edited_details.name) == '\0' || strlen(edited_details.billing_address) == '\0' || strlen(edited_details.password) == '\0' || strlen(edited_details.phone_number) == '\0' || strlen(edited_details.date) == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}*/
if (edited_details.name[0] == '\0' || edited_details.billing_address[0] == '\0' || edited_details.password[0] == '\0' || edited_details.phone_number[0] == '\0' || edited_details.date[0] == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}
FILE *file = fopen(CUSTOMERS_FILE, "r");
FILE *new_file = fopen("customers_new.txt", "ab+");
Customer record;
while (fscanf(file, "[%[^]]], [%[^]]], [%[^]]], [%[^]]], [%lf], [%lf], [%[^]]]\n",
record.name, record.password, record.billing_address, record.phone_number,
&record.amount_paid, &record.amount_due, record.date) == 7)
{
if (strcmp(customer.name, record.name) == 0) {
printf("P: %s\nD: %s", edited_details.phone_number, edited_details.date);
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", edited_details.name, edited_details.password, edited_details.billing_address, edited_details.phone_number, edited_details.amount_paid, edited_details.amount_due, edited_details.date);
} else {
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", record.name, record.password, record.billing_address, record.phone_number, record.amount_paid, record.amount_due, record.date);
}
}
fclose(file);
fclose(new_file);
remove(CUSTOMERS_FILE);
rename("customers_new.txt", CUSTOMERS_FILE);
printf("\nThe customer details have been successfully modified!\n");
key_to_continue();
}
MODIFYING DETAILS
==============
CREATE A CUSTOMER PROFILE
=========================
Name (dumbfk): test
DATA: testPassword (abc123): lol
DATA: lolBilling Address (pukima jalan): lol
DATA: lolPhone Number (6969696969): 499449
DATA: 499449Amount Paid ( 6969.00): 499449
Amount Due (6969699.00): 499494
Payment Date (6/9/1969): 22/2/2000
DATA: 22/2/2000P: �O���
D: �O���
The customer details have been successfully modified!
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[dumbfk], [abc123], [pukima jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[test], [lol], [lol], [�O���], [499449.000000], [499494.000000], [�O���]
数据文件(之前):
char CUSTOMERS_FILE[50] = "customers.txt";
typedef struct Customer {
char name[50];
char password[50];
char billing_address[100];
char phone_number[15];
double amount_paid;
double amount_due;
char date[20];
} Customer;
char* read_string(int length) {
char data[length];
rewind(stdin);
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
return data;
}
void handle_modify_customer(Customer customer) {
Customer edited_details;
printf("\nMODIFYING DETAILS\n==============\n\n");
printf("CREATE A CUSTOMER PROFILE\n=========================\n");
printf("Name (%s): ", customer.name);
strcpy(edited_details.name, read_string(50));
printf("Password (%s): ", customer.password);
strcpy(edited_details.password, read_string(50));
printf("Billing Address (%s): ", customer.billing_address);
strcpy(edited_details.billing_address, read_string(100));
printf("Phone Number (%s): ", customer.phone_number);
strcpy(edited_details.phone_number, read_string(15));
printf("Amount Paid (%10.2lf): ", customer.amount_paid);
scanf("%lf", &edited_details.amount_paid);
printf("Amount Due (%10.2lf): ", customer.amount_due);
scanf("%lf", &edited_details.amount_due);
printf("Payment Date (%s): ", customer.date);
strcpy(edited_details.date, read_string(20));
/*
if (strlen(edited_details.name) == '\0' || strlen(edited_details.billing_address) == '\0' || strlen(edited_details.password) == '\0' || strlen(edited_details.phone_number) == '\0' || strlen(edited_details.date) == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}*/
if (edited_details.name[0] == '\0' || edited_details.billing_address[0] == '\0' || edited_details.password[0] == '\0' || edited_details.phone_number[0] == '\0' || edited_details.date[0] == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}
FILE *file = fopen(CUSTOMERS_FILE, "r");
FILE *new_file = fopen("customers_new.txt", "ab+");
Customer record;
while (fscanf(file, "[%[^]]], [%[^]]], [%[^]]], [%[^]]], [%lf], [%lf], [%[^]]]\n",
record.name, record.password, record.billing_address, record.phone_number,
&record.amount_paid, &record.amount_due, record.date) == 7)
{
if (strcmp(customer.name, record.name) == 0) {
printf("P: %s\nD: %s", edited_details.phone_number, edited_details.date);
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", edited_details.name, edited_details.password, edited_details.billing_address, edited_details.phone_number, edited_details.amount_paid, edited_details.amount_due, edited_details.date);
} else {
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", record.name, record.password, record.billing_address, record.phone_number, record.amount_paid, record.amount_due, record.date);
}
}
fclose(file);
fclose(new_file);
remove(CUSTOMERS_FILE);
rename("customers_new.txt", CUSTOMERS_FILE);
printf("\nThe customer details have been successfully modified!\n");
key_to_continue();
}
MODIFYING DETAILS
==============
CREATE A CUSTOMER PROFILE
=========================
Name (dumbfk): test
DATA: testPassword (abc123): lol
DATA: lolBilling Address (pukima jalan): lol
DATA: lolPhone Number (6969696969): 499449
DATA: 499449Amount Paid ( 6969.00): 499449
Amount Due (6969699.00): 499494
Payment Date (6/9/1969): 22/2/2000
DATA: 22/2/2000P: �O���
D: �O���
The customer details have been successfully modified!
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[dumbfk], [abc123], [pukima jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[test], [lol], [lol], [�O���], [499449.000000], [499494.000000], [�O���]
数据文件(之后):
char CUSTOMERS_FILE[50] = "customers.txt";
typedef struct Customer {
char name[50];
char password[50];
char billing_address[100];
char phone_number[15];
double amount_paid;
double amount_due;
char date[20];
} Customer;
char* read_string(int length) {
char data[length];
rewind(stdin);
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
return data;
}
void handle_modify_customer(Customer customer) {
Customer edited_details;
printf("\nMODIFYING DETAILS\n==============\n\n");
printf("CREATE A CUSTOMER PROFILE\n=========================\n");
printf("Name (%s): ", customer.name);
strcpy(edited_details.name, read_string(50));
printf("Password (%s): ", customer.password);
strcpy(edited_details.password, read_string(50));
printf("Billing Address (%s): ", customer.billing_address);
strcpy(edited_details.billing_address, read_string(100));
printf("Phone Number (%s): ", customer.phone_number);
strcpy(edited_details.phone_number, read_string(15));
printf("Amount Paid (%10.2lf): ", customer.amount_paid);
scanf("%lf", &edited_details.amount_paid);
printf("Amount Due (%10.2lf): ", customer.amount_due);
scanf("%lf", &edited_details.amount_due);
printf("Payment Date (%s): ", customer.date);
strcpy(edited_details.date, read_string(20));
/*
if (strlen(edited_details.name) == '\0' || strlen(edited_details.billing_address) == '\0' || strlen(edited_details.password) == '\0' || strlen(edited_details.phone_number) == '\0' || strlen(edited_details.date) == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}*/
if (edited_details.name[0] == '\0' || edited_details.billing_address[0] == '\0' || edited_details.password[0] == '\0' || edited_details.phone_number[0] == '\0' || edited_details.date[0] == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}
FILE *file = fopen(CUSTOMERS_FILE, "r");
FILE *new_file = fopen("customers_new.txt", "ab+");
Customer record;
while (fscanf(file, "[%[^]]], [%[^]]], [%[^]]], [%[^]]], [%lf], [%lf], [%[^]]]\n",
record.name, record.password, record.billing_address, record.phone_number,
&record.amount_paid, &record.amount_due, record.date) == 7)
{
if (strcmp(customer.name, record.name) == 0) {
printf("P: %s\nD: %s", edited_details.phone_number, edited_details.date);
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", edited_details.name, edited_details.password, edited_details.billing_address, edited_details.phone_number, edited_details.amount_paid, edited_details.amount_due, edited_details.date);
} else {
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", record.name, record.password, record.billing_address, record.phone_number, record.amount_paid, record.amount_due, record.date);
}
}
fclose(file);
fclose(new_file);
remove(CUSTOMERS_FILE);
rename("customers_new.txt", CUSTOMERS_FILE);
printf("\nThe customer details have been successfully modified!\n");
key_to_continue();
}
MODIFYING DETAILS
==============
CREATE A CUSTOMER PROFILE
=========================
Name (dumbfk): test
DATA: testPassword (abc123): lol
DATA: lolBilling Address (pukima jalan): lol
DATA: lolPhone Number (6969696969): 499449
DATA: 499449Amount Paid ( 6969.00): 499449
Amount Due (6969699.00): 499494
Payment Date (6/9/1969): 22/2/2000
DATA: 22/2/2000P: �O���
D: �O���
The customer details have been successfully modified!
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[dumbfk], [abc123], [pukima jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[test], [lol], [lol], [�O���], [499449.000000], [499494.000000], [�O���]
问题:
char CUSTOMERS_FILE[50] = "customers.txt";
typedef struct Customer {
char name[50];
char password[50];
char billing_address[100];
char phone_number[15];
double amount_paid;
double amount_due;
char date[20];
} Customer;
char* read_string(int length) {
char data[length];
rewind(stdin);
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
return data;
}
void handle_modify_customer(Customer customer) {
Customer edited_details;
printf("\nMODIFYING DETAILS\n==============\n\n");
printf("CREATE A CUSTOMER PROFILE\n=========================\n");
printf("Name (%s): ", customer.name);
strcpy(edited_details.name, read_string(50));
printf("Password (%s): ", customer.password);
strcpy(edited_details.password, read_string(50));
printf("Billing Address (%s): ", customer.billing_address);
strcpy(edited_details.billing_address, read_string(100));
printf("Phone Number (%s): ", customer.phone_number);
strcpy(edited_details.phone_number, read_string(15));
printf("Amount Paid (%10.2lf): ", customer.amount_paid);
scanf("%lf", &edited_details.amount_paid);
printf("Amount Due (%10.2lf): ", customer.amount_due);
scanf("%lf", &edited_details.amount_due);
printf("Payment Date (%s): ", customer.date);
strcpy(edited_details.date, read_string(20));
/*
if (strlen(edited_details.name) == '\0' || strlen(edited_details.billing_address) == '\0' || strlen(edited_details.password) == '\0' || strlen(edited_details.phone_number) == '\0' || strlen(edited_details.date) == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}*/
if (edited_details.name[0] == '\0' || edited_details.billing_address[0] == '\0' || edited_details.password[0] == '\0' || edited_details.phone_number[0] == '\0' || edited_details.date[0] == '\0') {
printf("All fields must be filled in!");
handle_modify_customer(customer);
}
FILE *file = fopen(CUSTOMERS_FILE, "r");
FILE *new_file = fopen("customers_new.txt", "ab+");
Customer record;
while (fscanf(file, "[%[^]]], [%[^]]], [%[^]]], [%[^]]], [%lf], [%lf], [%[^]]]\n",
record.name, record.password, record.billing_address, record.phone_number,
&record.amount_paid, &record.amount_due, record.date) == 7)
{
if (strcmp(customer.name, record.name) == 0) {
printf("P: %s\nD: %s", edited_details.phone_number, edited_details.date);
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", edited_details.name, edited_details.password, edited_details.billing_address, edited_details.phone_number, edited_details.amount_paid, edited_details.amount_due, edited_details.date);
} else {
fprintf(new_file, "[%s], [%s], [%s], [%s], [%lf], [%lf], [%s]\n", record.name, record.password, record.billing_address, record.phone_number, record.amount_paid, record.amount_due, record.date);
}
}
fclose(file);
fclose(new_file);
remove(CUSTOMERS_FILE);
rename("customers_new.txt", CUSTOMERS_FILE);
printf("\nThe customer details have been successfully modified!\n");
key_to_continue();
}
MODIFYING DETAILS
==============
CREATE A CUSTOMER PROFILE
=========================
Name (dumbfk): test
DATA: testPassword (abc123): lol
DATA: lolBilling Address (pukima jalan): lol
DATA: lolPhone Number (6969696969): 499449
DATA: 499449Amount Paid ( 6969.00): 499449
Amount Due (6969699.00): 499494
Payment Date (6/9/1969): 22/2/2000
DATA: 22/2/2000P: �O���
D: �O���
The customer details have been successfully modified!
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[dumbfk], [abc123], [pukima jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[well lol], [abc123], [wtf bro? 24], [0183188383], [3000.000000], [4000.000000], [12/12/2012]
[chow hai], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[lol head], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[stupid face], [abc123], [lol jalan], [6969696969], [6969.000000], [6969699.000000], [6/9/1969]
[test], [lol], [lol], [�O���], [499449.000000], [499494.000000], [�O���]
正如您可能看到的,问题在于付款日期和电话号码字段变得混乱。它发生在我使用strcpy之后。我调试了read\u string(..)
函数,它看起来很好。我不明白为什么会这样。如能帮助解决此问题,我们将不胜感激
有趣的是:只有
日期
和电话号码
受到影响<代码>名称,密码
,账单地址
没有问题。将read\u string函数中的本地字符数据更改为静态变量,以便不会丢失内存
#define MAX_STR_LENGTH 100
char* read_string(int length)
{
static char data[MAX_STR_LENGTH]; // change to static -- it will not change
rewind(stdin);
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
return data;
}
这是一个非常好的例子,说明指针和数组不是一回事:
char* read_string(int length) {
char data[length];
// code
return data;
}
将不起作用,因为data
是在堆栈上分配的本地数组,当函数返回时将不再存在
如果您将char data[length]
更改为static char data[length]
,它将起作用。但是请注意,之前的读取将被覆盖,因此这将无法按预期工作:
char *s1, *s2;
s1 = read_string(10);
s2 = read_string(10);
printf("First string: %s\n", s1);
printf("Second string: %s\n", s2);
解决这个问题的一种方法是使用char data*=malloc(length*sizeof*data)
。这样,您将能够使用以前的读取。但一般来说,您希望避免像这样隐藏的malloc
s,因为您需要free
它们。如果您想采用该解决方案,请执行以下操作:
char * read_string(char * dest, int length) {
char data[length];
// code
return strncpy(dest, data, length);
}
char * str = malloc(length);
if(! read_string(str, length)) {
fprintf(stderr, "Error reading string\n");
exit(EXIT_FAILURE);
}
// Code
free(str);
然后这样称呼它:
char * read_string(char * dest, int length) {
char data[length];
// code
return strncpy(dest, data, length);
}
char * str = malloc(length);
if(! read_string(str, length)) {
fprintf(stderr, "Error reading string\n");
exit(EXIT_FAILURE);
}
// Code
free(str);
另一个选项是将指针与长度一起传递
void read_string(char *data, int length) {
fgets(data, length, stdin);
if (data[0] == '\n') {
data[0] = '\0';
}
strtok(data, "\n");
printf("DATA: %s", data);
}
打电话给
read_string(edited_details.name, 50);
那么就不需要使用strcpy()在C语言中没有对字符串的直接支持。大多数情况下,您必须处理(我应该说是“内存块”而不是“数组”?)包含字符的内存块以及最后的
NUL
在您的情况下,它是这样的:
char * read_string(char * dest, int length) {
char data[length];
// code
return strncpy(dest, data, length);
}
char * str = malloc(length);
if(! read_string(str, length)) {
fprintf(stderr, "Error reading string\n");
exit(EXIT_FAILURE);
}
// Code
free(str);
char* read_string(int length) {
/* "data" is your chunk of memory.
* It is an array.
* This array is local to containing function.
* This array is automatically allocated on the stack.
* This array has its address in memory. Like &data[0].
* The address may change from one invocation of the function to another.
*/
char data[length];
/* some code here... */
return data; /* true, this returns the address &data[0] */
}
/* The function is done. Local variables (array by the name of "data") are gone.
* The memory on the stack that was once allocated for local variables is
* considered "free-to-use" by any other function you will invoke next. This memory
* is not valid for use outside the function any more, but you may and you do
* return a pointer to that memory, which is dangerous to use and harmful
*/
基本上有三种方法可以解决此问题:
您可以声明一个全局内存块,例如一个全局数组:
char data[MAX_POSSIBLE_LENGTH]; /* Static storage. Global scope. */
char* read_string() {
/* some code here... */
return data;
}
。。。或者使用固定长度使阵列保持静态:
char* read_string() {
static char data[MAX_POSSIBLE_LENGTH]; /* Static storage. Function scope. */
/* some code here... */
return data;
}
。。。或者从外部将阵列与其长度一起传入:
char* read_string(char *data, int length) {
/* some code here... */
return data;
}
。。。或者使用动态分配
char* read_string(int length) {
/* some code here... */
char *data;
data = malloc(length); /* Dynamic storage. Must be freed somewhere./*
/* This is not an "array", now this really is a "chunk of memory" :) */
return data;
}
void handle_modify_customer(Customer customer)
{
Customer edited_details;
char * input_str_ptr;
printf("\nMODIFYING DETAILS\n==============\n\n");
printf("CREATE A CUSTOMER PROFILE\n=========================\n");
printf("Name (%s): ", customer.name);
strcpy(edited_details.name, input_str_ptr = read_string(50));
free(input_str_ptr); /* Don't allow mem leakage */
/* ... */
}
@user3121023我使用退货吗?如何解决这个问题?
倒带(stdin)代码>非常不可靠<代码>cat数据文件|您的程序
将破坏您的代码。您无法控制其他人如何调用您的程序。关于:if(data[0]='\n'){data[0]='\0';}
这不是消除尾随换行符的方法。建议:data[strcspn(data,“\n”)]='\0'代码>请注意,这样使用static
会使代码不可重入且多线程不安全。