简体   繁体   English

C结构值出现乱码

[英]C struct values garbled

Having the problem with the sender and receiver of the checksum . checksum的发送者和接收者有问题。 where I am using the struct to define the variables to be written into the pipe. 我在哪里使用该结构来定义要写入管道的变量。

struct ANSWER
{
    int arr[BUFFER];
    int counter;
    int ans;
}a;         

The mechanism used for checksum is : 用于checksum的机制是:

int chck(int value[],int count)
{
    int i,sum=0,checksum;
    for ( i=0; i<count; i++)
    {
        sum+=value[i];
    }
    printf("SUM IS :%d \n",sum);
    checksum=checksum^sum;
    printf("CHECKSUM IS : %d",checksum);
    return checksum;
}

The main problem is in writing in the pipe as 主要问题是在管道中写为

write(pipe,a,sizeof(a));

This writes all the contains using struct. 这将使用struct写入所有包含。

While at receiver side having problem in retrieving the specific values when I retrieve the values at receiver side I get garbled values in count and ans variables. 当在接收方检索接收方的值时,在检索特定值时遇到问题,但在countans变量中却出现乱码。

read(pipe,a,sizeof(a));
printf("COUNT : \n ",a.counter);

the values turn out to be garbled. 值证明是乱码。

Main area: 主要领域:

void main(int argc, char * argv[])
{
    int pipe,i;

    pipe = open("devil",O_WRONLY);

    if ( pipe == -1 )
    {
        printf("ERROR NO PIPE FOUND \n");
    }

    printf("ENTER NUMBER OF ELEMENT : ");
    scanf("%d",a.counter);

    for ( i=0; i<a.counter; i++)
    {
        printf("ENTER NUMBER :");
        scanf("%d",&a.arr[i]);
    }

    a.ans=chck(a.arr,a.counter);

    printf("CHECKSUM IS : %d \n",a.ans);

    write(pipe,&a,sizeof(a));

}

Turning comment to answer, I suggest you add error checking to all your system and standard library calls (except printing to stdout/stderr, that's more clutter than it's worth for almost any app), something like: 转为评论,我建议您对所有系统调用和标准库调用添加错误检查(除了打印到stdout / stderr之外,这比几乎所有应用程序都要复杂),例如:

int main(int argc, char * argv[])
{
    int pipe, i;

    pipe = open("devil",O_WRONLY);
    if ( pipe == -1 )
    {
        perror("open pipe");
        return EXIT_FAILURE;
    }

    printf("ENTER NUMBER OF ELEMENT : ");
    if (scanf("%d",&a.counter) != 1) 
    {
        fprintf(stderr, "scanf count IO or parse error, or EOF\n");
        return EXIT_FAILURE;
    }

    for ( i=0; i<a.counter; i++)
    {
        printf("ENTER NUMBER :");
        if (scanf("%d",&a.arr[i]) != 1)
        {
            fprintf(stderr, "scanf number IO or parse error, or EOF\n");
            return EXIT_FAILURE;
        }
    }

    a.ans=chck(a.arr,a.counter);
    printf("CHECKSUM IS : %d \n",a.ans);
    i = write(pipe,&a,sizeof(a));
    if (i == -1)
    {
        perror("write to pipe");
        return EXIT_FAILURE;
    }
    else if (i != sizeof(a))
    {
        fprintf(stderr, "write to pipe, partial write!\n");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Notes: For pedantic code, write actually returns type ssize_t . 注意:对于pedantic代码, write实际上返回类型ssize_t I changed return type of main to int as that is required by standard. 我将main返回类型更改为int ,这是标准要求。 Please read manual page / docs of perror to understand what it does and how it prints the actual error message. 请阅读手册页/ perror文档,以了解其功能以及如何打印实际错误消息。 EXIT_FAILURE and EXIT_SUCCESS are constants for the two standard-defined return values or exit codes, usually 1 and 0 respectively. EXIT_FAILUREEXIT_SUCCESS是两个标准定义的返回值或退出代码的常量,通常分别为1和0。 And in a larger program, you would probably write helper functions or macros for error handling, and split the entire main function into multiple functions, to make the code look less cluttered. 在更大的程序中,您可能会编写帮助程序功能或宏以进行错误处理,并将整个主要功能拆分为多个功能,以使代码看起来更简洁。


Additionally, you use checksum variable uninitialized in chck function. 此外,您使用chck函数中未初始化的checksum变量。 You should probably initialize it to 0, something like: 您可能应该将其初始化为0,例如:

int chck(int value[],int count)
{
    int i = 0, sum = 0, checksum = 0;
    ....

In general, you should avoid having uninitialzied variables, it's too easy to forget when first using them, like demonstrated here... Also, turn on compiler warnings, so you will usually get a warning if you still do that accidentally. 通常,应避免使用未初始化的变量,如初次使用时那样,太容易忘记,就像这里展示的那样。此外,请打开编译器警告,因此,如果您仍然不小心这样做, 通常会得到警告。


One more bug: your first scanf was missing & to get pointer to a.counter (required for scanf , to actually modify the original variable). 另一个错误:您的第一个scanf丢失了&并获得了指向a.counter指针( scanf所需,实际上是修改了原始变量)。 A good compiler may also be smart enough to warn about passing suspicious arguments to scanf . 一个好的编译器可能也足够聪明,可以警告不要将可疑参数传递给scanf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM