简体   繁体   English

C程序输出

[英]c-program output

#include<stdio.h>
int main()
{

        FILE *fp,*fs;
        fs=fopen("c.dat","w");
        fp=fopen("c.dat","w");
        putc('a',fp);
        putc('1',fs);
        close(fs);
        close(fp);
        return 0;
}

Irrespective of the order execution 与订单执行无关

 putc('a',fp);putc('1',fs);   


 putc('1',fs);putc('a',fp);

contents of c.dat is '1' only. c.dat的内容仅为“ 1”。 Can somebody explain the reason ? 有人可以解释原因吗?

For a start, you don't close() a file that you opened with fopen() , you have to fclose() it. 一开始,你不要close()你有打开的文件fopen() ,你必须fclose()它。 You're mixing two levels of abstraction here, file handles and file pointers. 您在这里混合了两个抽象级别,即文件句柄和文件指针。

On top of that, I think you'll find that the two file pointers are maintaining independent file offsets and buffers. 最重要的是,我认为您会发现两个文件指针保持着独立的文件偏移量和缓冲区。 Whichever one closes the file last will write their data last, overwriting the other. 最后一个关闭文件的文件将最后写入其数据,而另一个覆盖文件。

Change the order of your fclose() statements and you should see what I mean: 更改fclose()语句的顺序,您应该明白我的意思:

#include<stdio.h>
int main (void) {
    FILE *fp,*fs;
    fs=fopen("c.dat","w");
    fp=fopen("c.dat","w");
    fputc('a',fp);
    fputc('1',fs);
    fclose(fp);    // Try changing these
    fclose(fs);    //    two around.
    return 0;
}

I did that and consistently got the contents based on the last file closed. 我这样做了,并且始终根据关闭的最后一个文件获取内容。

And, before you say that you should have gotten a because fp was the last one you closed, you did not close fp (with an fclose() ). 而且,你说在这之前你应该得到a ,因为fp是最后一个你关闭,你fp (与fclose()

What you did do was try to close() a file handle that was almost certainly invalid, because file handles are generally relatively small values (eg, 0 , 1 , 2 ) while file pointers are generally relatively large values (eg, 0xdeadbeef , 0xcaf3f00d ). 所做的事是设法close()这是几乎可以肯定是无效的文件句柄,因为文件句柄一般都是比较小的值(例如, 012 ),而文件指针一般都是比较大的值(例如, 0xdeadbeef0xcaf3f00d )。

So the closure of both those files was left up to the C runtime environment when you exited (and that would most likely have done it in a deterministic way, which is why you always got the same value in the file). 因此,当您退出时,这两个文件的关闭将留给C运行时环境进行(这很可能以确定性的方式完成,这就是为什么您在文件中始终获得相同的值)。

You are opening the same file for writing twice. 您正在打开相同的文件进行两次写入。 All bets are off at that point. 那时所有赌注都没有了。

You didnt specify the unerlying OS, but on win and *nix systems file buffers are shared between processes, so : 您没有指定操作系统,但是在win和* nix系统上,进程之间共享文件缓冲区,因此:

    putc('a',fp); 
    putc('1',fs);

are writing to exactly the same address in the same buffer if you reversed the order of the putc statements you will end up with an 'a' in the file. 如果您颠倒putc语句的顺序,则会在同一缓冲区中写入完全相同的地址,最后在文件中将以'a'结尾。 * *

NOT

Just goes to show how tricky this all is. 只是说明这一切有多么棘手。 I have tried this and what affects the output is the order in which the files are opened -- the first open always takes precedence. 我已经尝试过了,影响输出的是文件打开的顺序-第一次打开总是优先的。

我认为这是因为您首先通过fs = fopen打开了c.dat,然后某些锁不允许写入“ a”,因为这将要求再次打开文件。

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

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