字符串比较有问题吗? C
好的,我在这里遇到了一些问题。 基本上我正在读取共享内存,但这不是问题。 我有一个change.c 函数,如果我输入他们的“ID”号,它可以让我更改struct Studentdata 共享内存。
问题是,它显示它不匹配,即使它是。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/sem.h>
#include <pwd.h>
#include <stdlib.h>
#include "header.h"
//VOID CHANGE_STUDENT
//function that asks the user to enter the new data and stores the data
//into the appropriate student struct
void Change_Student(struct StudentInfo *ptr, int sema_set);
//BEGIN MAIN FUNCTION
main(int argc, char* argv[])
{
int i,id; //Id to hold shmem id
struct StudentInfo *infoptr; //Ptr to shmem
int sema_set; //ID for semaphores
char input[30]; //Buffer for user input
int found = 0; //Found 'bool'
struct StudentInfo *beginptr; //Ptr to beginning of data
int rcid; //Readcount ID
int* rcptr; //Readcount ptr
/* get the id of the shared memory segment with key "KEY" */
/* note that this is the segment where the data is stored */
id = shmget(KEY,SEGSIZE, 0);
if (id <0){
perror("change: shmget failed 1");
exit(1);
}
/* attach the already created shared memory segment to infoptr so the
* shared memory segment can be accessed through 'inforptr'
* */
infoptr=(struct StudentInfo *)shmat(id,0,0);
if (infoptr <= (struct StudentInfo *) (0))
{
perror("change: shmat failed");
exit(2);
}
//Get the shmem with readcount ID
rcid = shmget(RCKEY,READCOUNT,0);
if(rcid < 0)
{
perror("Change: shmget failed");
exit(1);
}
//Attach the memory to our program and set ptr to it
rcptr = (int*)shmat(id,0,0);
if(rcptr <= (int*)(0))
{
perror("Change: shmat failed");
exit(2);
}
//Get semaphores with our ID
sema_set = semget(SEMA_KEY,NUM_SEMAPHS,0);
if(sema_set < 0)
{
perror("Change: shmget failed");
exit(1);
}
//While the user cannot enter correct password
while(strcmp(input,"000")!=0)
{
printf("\nPlease input your password: ");
scanf("%s",input);
}
//Set the begin pointer
beginptr = infoptr;
while(1)
{
//Ask the user which ID to change
printf("Please input a student ID you would like to change ('quit' to quit): ");
scanf("%s",input);
//If the issue quit command, exit
if(strcmp(input,"quit")==0)
exit(0);
//Check for the ID in shmem
while((strcmp(infoptr->Name,"")!=0) && (found != 1))
{
printf("%s\n",infoptr->ID);
//if the name was found, print the data we are changing
if(strcmp(infoptr->ID,input)==0)
{
found = 1;
printf("\n%s\n",infoptr->Name);
printf("%s\n",infoptr->telNumber);
printf("%s\n",infoptr->Address);
printf("%s\n\n",infoptr->ID);
}
else
infoptr++;
}
//if never found, print error
if(found == 0)
printf("Record not found.\n");
//if found, run change record function
if(found == 1)
Change_Student(infoptr,sema_set);
//reset found
found = 0;
//Reset ptr
infoptr = beginptr;
}
}
void Change_Student(struct StudentInfo *ptr,int sema_set)
{
char input[50]; //Input buffer
Wait(sema_set,0); //Wait on writers
//Get the user's input to change the data
gets(input);
printf("Input Student Name: ");
gets(input);
strcpy(ptr->Name,input);
printf("Input Student Tel. Number: ");
gets(input);
strcpy(ptr->telNumber,input);
printf("Input Student Address: ");
gets(input);
strcpy(ptr->Address,input);
//Sleep for testing
sleep(10);
//Signal the writers
Signal(sema_set,0);
//Confirmation message
printf("\nRecord Updated!\n");
exit(0);
}
当我运行程序时,我得到了这个(打印输出,如果只是为了调试)
Please input your password: 000
Please input a student ID you would like to change ('quit' to quit): 111223333
111223333
111223344
111223355
111223366
111223377
111224411
111224422
111224433
111224444
111224455
111224466
Record not found.
Please input a student ID you would like to change ('quit' to quit):
上次我检查过。 111223333 和 111223333 是同一个东西吗?那么为什么它不起作用呢?因为 infoptr->ID 在循环中显示相同的内容?
Ok so im having a bit of an issue here.
basically Im reading shared memory but thats not the problem.
I have a change.c function that lets me change a struct studentdata shared memory if I enter their "ID" number.
problem is, it's showing it's not matching, EVEN THO IT IS.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/sem.h>
#include <pwd.h>
#include <stdlib.h>
#include "header.h"
//VOID CHANGE_STUDENT
//function that asks the user to enter the new data and stores the data
//into the appropriate student struct
void Change_Student(struct StudentInfo *ptr, int sema_set);
//BEGIN MAIN FUNCTION
main(int argc, char* argv[])
{
int i,id; //Id to hold shmem id
struct StudentInfo *infoptr; //Ptr to shmem
int sema_set; //ID for semaphores
char input[30]; //Buffer for user input
int found = 0; //Found 'bool'
struct StudentInfo *beginptr; //Ptr to beginning of data
int rcid; //Readcount ID
int* rcptr; //Readcount ptr
/* get the id of the shared memory segment with key "KEY" */
/* note that this is the segment where the data is stored */
id = shmget(KEY,SEGSIZE, 0);
if (id <0){
perror("change: shmget failed 1");
exit(1);
}
/* attach the already created shared memory segment to infoptr so the
* shared memory segment can be accessed through 'inforptr'
* */
infoptr=(struct StudentInfo *)shmat(id,0,0);
if (infoptr <= (struct StudentInfo *) (0))
{
perror("change: shmat failed");
exit(2);
}
//Get the shmem with readcount ID
rcid = shmget(RCKEY,READCOUNT,0);
if(rcid < 0)
{
perror("Change: shmget failed");
exit(1);
}
//Attach the memory to our program and set ptr to it
rcptr = (int*)shmat(id,0,0);
if(rcptr <= (int*)(0))
{
perror("Change: shmat failed");
exit(2);
}
//Get semaphores with our ID
sema_set = semget(SEMA_KEY,NUM_SEMAPHS,0);
if(sema_set < 0)
{
perror("Change: shmget failed");
exit(1);
}
//While the user cannot enter correct password
while(strcmp(input,"000")!=0)
{
printf("\nPlease input your password: ");
scanf("%s",input);
}
//Set the begin pointer
beginptr = infoptr;
while(1)
{
//Ask the user which ID to change
printf("Please input a student ID you would like to change ('quit' to quit): ");
scanf("%s",input);
//If the issue quit command, exit
if(strcmp(input,"quit")==0)
exit(0);
//Check for the ID in shmem
while((strcmp(infoptr->Name,"")!=0) && (found != 1))
{
printf("%s\n",infoptr->ID);
//if the name was found, print the data we are changing
if(strcmp(infoptr->ID,input)==0)
{
found = 1;
printf("\n%s\n",infoptr->Name);
printf("%s\n",infoptr->telNumber);
printf("%s\n",infoptr->Address);
printf("%s\n\n",infoptr->ID);
}
else
infoptr++;
}
//if never found, print error
if(found == 0)
printf("Record not found.\n");
//if found, run change record function
if(found == 1)
Change_Student(infoptr,sema_set);
//reset found
found = 0;
//Reset ptr
infoptr = beginptr;
}
}
void Change_Student(struct StudentInfo *ptr,int sema_set)
{
char input[50]; //Input buffer
Wait(sema_set,0); //Wait on writers
//Get the user's input to change the data
gets(input);
printf("Input Student Name: ");
gets(input);
strcpy(ptr->Name,input);
printf("Input Student Tel. Number: ");
gets(input);
strcpy(ptr->telNumber,input);
printf("Input Student Address: ");
gets(input);
strcpy(ptr->Address,input);
//Sleep for testing
sleep(10);
//Signal the writers
Signal(sema_set,0);
//Confirmation message
printf("\nRecord Updated!\n");
exit(0);
}
when I run the program I get this (the printout if just for debugging)
Please input your password: 000
Please input a student ID you would like to change ('quit' to quit): 111223333
111223333
111223344
111223355
111223366
111223377
111224411
111224422
111224433
111224444
111224455
111224466
Record not found.
Please input a student ID you would like to change ('quit' to quit):
Last time I checked. 111223333 and 111223333 were the same thing? So why isn't it working? because infoptr->ID is showing the same thing in the loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信琴弦确实不一样。尝试使用
strncmp
吗?I believe the strings really aren't the same. Try using
strncmp
?