字符串比较有问题吗? C

发布于 2024-09-30 03:36:40 字数 4756 浏览 3 评论 0原文

好的,我在这里遇到了一些问题。 基本上我正在读取共享内存,但这不是问题。 我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-10-07 03:36:40

我相信琴弦确实不一样。尝试使用 strncmp 吗?

I believe the strings really aren't the same. Try using strncmp ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文