C語言文件操作詳解

//FILE *fopen(char *pname,char *mode)
#include <bits/stdc++.h>

using namespace std;
FILE *fopen();

int main()
{
    FILE *fp;
    if((fp=fopen("d:/test.txt","r"))==NULL){
        printf("The file can not be opened.\n");
        exit(1);
    }
    else{
        printf("I catch U!");
    }
    return 0;
}
//FILE *fopen(char *pname,char *mode)
#include <bits/stdc++.h>

using namespace std;
FILE *fopen();

int main()
{
    FILE *fp;
    fp=fopen("d:/test.txt","r");
    fclose(fp);
    return 0;
}
//FILE *fopen(char *pname,char *mode)
#include <bits/stdc++.h>

using namespace std;
FILE *fopen();

int main(int argc, char *argv[])
{
    int ch;
    FILE *fp;
    if(argc!=2){
        printf("Error format,Usage:display filename1\n");
        return 0;
    }
    if( (fp=fopen(argv[1],"r"))==NULL ){
        printf("The file <%s> can not be opend.\n",argv[1]);
        return 0;
    }
    ch = fgetc(fp);
    while(ch != EOF){
        putchar(ch);
        ch=fgetc(fp);
    }
    fclose(fp);
}
//FILE *fopen(char *pname,char *mode)
#include <bits/stdc++.h>

using namespace std;
FILE *fopen();

int main(int argc, char *argv[])
{
    int ch;
    FILE *in, *out;
    if(argc!=3){
        printf("Error in format,Usage:copyfile filename1 filename1\n");
        return 0;
    }
    if( (in=fopen(argv[1],"r"))==NULL ){
        printf("The file <%s> can not be opend.\n",argv[1]);
        return 0;
    }

    if( (out=fopen(argv[2],"r"))==NULL ){
        printf("The file <%s> can not be opend.\n",argv[2]);
        return 0;
    }

    ch = fgetc(in);
    while(ch != EOF){
        fputc(ch,out);
        ch=fgetc(in);
    }
    fclose(in);
    fclose(out);
}
//FILE *fopen(char *pname,char *mode)
#include <bits/stdc++.h>

using namespace std;
FILE *fopen();

int main(int argc, char *argv[])
{
    char str[9];
    int ch, count, i;
    FILE *fp;
    if(argc!=2){
        printf("Error in format,Usage:copyfile filename\n");
        return 0;
    }
    if( (fp=fopen(argv[1],"r"))==NULL ){
        printf("The file <%s> can not be opend.\n",argv[1]);
        return 0;
    }

    count = 0;
    do{
      i=0;
      printf("%06o:",count*8);
      do{
          ch=fgetc(fp);
          printf("%4d",ch);
          if(ch<' ' || ch>'~') str[i]='#';
          else str[i]=ch;
          if(++i==8) break;
      }while(ch!=EOF);
      str[i]='\0';
      for(;i<8;i++) printf(" ");
      printf(" %s\n",str);
      count++;
    }while(ch!=EOF);
    fclose(fp);
}
發佈了111 篇原創文章 · 獲贊 32 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章