欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C語言文件操作函數(shù)freopen詳細解析

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

今天做USACO  用到了文件的操作。 之前做USACO只是格式化的些 寫  freopen("xxx.in","r",stdin)  和"freopen("xxx.out","w",stdout)" 

百度百科上是這么介紹的:

函數(shù)名: freopen

功 能: 替換一個流,或者說重新分配文件指針,實現(xiàn)重定向。如果stream流已經(jīng)打開,則先關閉該流。如果該流已經(jīng)定向,則freopen將會清除該定向。此函數(shù)一般用于將一個指定的文件打開一個預定義的流:標準輸入、標準輸出或者標準出錯。

用 法: FILE *freopen(const char *filename,const char *type, FILE *stream);

頭文件:stdio.h

例1:

復制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    if(freopen("file.txt","w",stdout)==NULL)
        fprintf(stderr,"error\n");
    printf("This is in the file\n");      //這句話會在file.txt中顯示。
    fclose(stdout);               //使用fclose()函數(shù)就可以把<A target=_blank>緩沖區(qū)</A>內(nèi)最后剩余的數(shù)據(jù)輸出到磁盤文件中,并釋放文件指針和有關的緩沖區(qū)。
    return 0;
}

例2:
復制代碼 代碼如下:

//首先在同路徑下創(chuàng)建一個in.txt文本文檔寫入若干數(shù)字
#include <stdio.h>
#include <stdlib.h>

int main()
{
    freopen("in.txt","r",stdin);     //從in.txt 中讀入數(shù)據(jù)
    freopen("out.txt","w",stdout);  // 將最后數(shù)據(jù)寫入out.txt中
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)     //數(shù)據(jù)是從in.txt中輸入的
        printf("%d\n",a+b);             //寫入out.txt中
    fclose(stdin);
    fclose(stdout);
    return 0;
}

freopen("CON","w",stdout)  表示在控制臺窗口上寫入數(shù)據(jù);

例3:

復制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   // FILE *stream;
    freopen("file1.txt","w",stdout);
    printf("this is in file1.txt");      // 這句話在file1.txt中顯示
    freopen("CON","w",stdout);
    printf("And this is in command.\n");    //這句話在控制臺上顯示
    return 0;
}

例5:  關于fread   可以通過下面的程序,一看就知道什么意思了
復制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *stream
    char s[102400]="";
    if((stream=freopen("file.txt","r",stdin))==null)
        exit(-1);
    fread(s,1,1024,stdin);    // 讀取file.txt中1到1024位,放入s中 ,我是這么理解的
    printf("%s\n",s);
    return 0;
}

上一篇:探討register關鍵字在c語言和c++中的差異

欄    目:C語言

下一篇:冒泡排序的三種實現(xiàn)方法

本文標題:C語言文件操作函數(shù)freopen詳細解析

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3995.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有