简体   繁体   English

应用程序在不同机器上的行为不同

[英]Application behaves differently on different machines

I am learning about streams and copied the following application from the text book.我正在学习流,并从教科书中复制了以下应用程序。 When my friend compiles and runs on his Windows machine, it works fine.当我的朋友在他的 Windows 机器上编译并运行时,它工作正常。 When I run the application on my Ubuntu 18.04 machine, the input works fine, but the values don't seem to have any effect on the application, ie entering 0 does not cause the program to exit.当我在我的 Ubuntu 18.04 机器上运行应用程序时,输入工作正常,但这些值似乎对应用程序没有任何影响,即输入0不会导致程序退出。 My output is below the code.我的 output 在代码下方。

What would cause different behavior when compiling on different machines, and why is this not working on my machine?在不同的机器上编译时会导致不同的行为,为什么这在我的机器上不起作用?

int main(int argc, char* argv[])
{
    FILE        *fpt;
    char        byte;
    long int    where,move;

    if(argc != 2)
    {
        printf("Usage: fileseek filename\n");
        return(0);
    }

    fpt = fopen(argv[1], "r");

    if(fpt == NULL)
    {
        printf("Unable to open file %s for reading\n", argv[1]);
        return(0);
    }

    while(1)
    {
        where = ftell(fpt);

        fread(&byte,1,1,fpt);
        fseek(fpt,-1,SEEK_CUR);

        printf("Byte %d: %d (%c)\n", where, byte, byte);
        printf("Enter #bytes (+ or -) to move, or 0 to quit: ");

        scanf("%d", &move);

        printf("move: %d\n", move);

        if(move == 0)
            break;

        fseek(fpt,move,SEEK_CUR);
    }
    fclose(fpt);
}

Output Output

jonathon@dev1:~/hoover/ch5/build$ ./fileseek  text.txt 
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: 0
move: 0
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: 1
move: 1
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: 2
move: 2
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: 3
move: 3
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: 4
move: 4
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: 5
move: 5
Byte 0: 84 (T)
Enter #bytes (+ or -) to move, or 0 to quit: ^C     

The textbook is wrong if it that's exactly what it says: %d is a conversion specifier for an int , but move is a long int .如果这正是它所说的教科书是错误的: %dint的转换说明符,但movelong int The correct call would be:正确的调用是:

scanf("%ld", &move)

, with similar corrections to several printf calls. ,对几个printf调用进行了类似的更正。

It can work by coincidence, especially when long and int happen to be the same size (as they are in 64-bit Windows, but not in 64-bit Linux).它可以巧合地工作,尤其是当longint恰好大小相同时(因为它们在 64 位 Windows 中,但在 64 位 Linux 中不一样)。 With the mismatches, however, no particular behaviour is defined for the entire program: the compiler is allowed by the language standard to assume that the kind of illegal actions they represent never happen, and has no obligation whatever as to what a program that performs such an action may do.然而,对于不匹配,没有为整个程序定义特定的行为:语言标准允许编译器假设它们所代表的非法行为类型永远不会发生,并且对于执行此类行为的程序没有任何义务一个动作可以做。

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

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