c - 字符串比较有问题吗? C

标签 c string memory compare shared

好的,我在这里遇到了一些问题。 基本上我正在读取共享内存,但这不是问题。 我有一个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 在循环中显示相同的内容?

最佳答案

我相信字符串确实不一样。尝试使用 strncmp

关于c - 字符串比较有问题吗? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4062523/

相关文章:

c - 如何在 C 中使用 baskspace 从字符串中删除一个字符?

c - C 编译器能够跨目标文件进行优化吗?

Golang根据进程句柄和偏移量计算另一个进程内存地址

iphone - PNG 优化的最佳实践?

c - 错误 : could not insert module. 模块中的未知符号

c - Frama-c,非确定性浮点值

javascript - 在javascript中,如何在不覆盖以前的粗体字符串的情况下用粗体文本替换多个字符串?

java - 是否有任何内置实用程序可以检查 "null"字符串而不是 null?

Java字符串内字符串到字符串

java - Java中如何确定对象的大小