开发者

program running on max osx normally and in linux abnormally

开发者 https://www.devze.com 2023-01-24 01:11 出处:网络
i have been really busy with a project on operating systems recently .. i am working on my macs and everything is going great..when i tried to run the same programm at a pc running linux the programm

i have been really busy with a project on operating systems recently .. i am working on my macs and everything is going great..when i tried to run the same programm at a pc running linux the programm went terribly wrong...for example fgets and fscanf work fluently at mac but when i try to fgets 2 times in a row from stdin(rewinding the stdin firstly) i dont get what i want on linux even though at my mac evertyhting is running normally as expexted..please help me if i am missing out any information about linux and problems..

void add_new_account(int sd)
{
    int my_id, next_id;
    struct drivers instance; 

    read(sd, &next_id, sizeof(int));
    if (next_id >= 1000) {
        printf("system is full .. we are sorry for this inconvenience\n");
        exit(0);
    }

    read(sd, buf, sizeof(buf));
    printf("%s\n", buf);

    rewind(stdin);
    fgets(instance.driver_name, sizeof(instance.driver_name), stdin);

    rewind(stdin);
    fgets(instance.password, sizeof(instance.password)+1, stdin);

    write(sd, &instance, sizeof(instance));
    read(sd, &my_id, sizeof(int));
    printf("y开发者_Go百科our unique id is %d! please save it in order to login with it\n", my_id);
}


You should not rewind stdin, since it's not guaranteed to work. It's impossible to say exactly what's happening without seeing any code, but I'm guessing you're trying to rewind stdin with fseek (or one of its kin such as fsetpos or rewind) and that call is failing.

Regular file objects are seekable, but other types of file-like objects such as sockets and FIFOs are usually not seekable. In your limited case, you're probably observing that Mac OS X is buffering enough data for you to seek it to your liking, but Linux is not doing so.

You should rewrite your program so that you don't need to rewind stdin. The way you'd do this is by doing your own buffering—read data into a buffer in your application, and then read data out of that buffer (possibly multiple times, if necessary). Then you don't need to depend on the input stream being rewindable.

0

精彩评论

暂无评论...
验证码 换一张
取 消