zoukankan      html  css  js  c++  java
  • 文件操作 fopen() fclose()

    #define _CRT_SECURE_NO_DEPRECATE  /*取消scanf,printf不安全之类的错误提示*/
    /* fopen example */
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	FILE * pFile, *pFile2;
    	char buf[101];
    	char buf2[101] = "hello file test!";
    	pFile = fopen("myfile.txt", "w");
    	pFile2 = fopen("fopentest.txt", "r"); //只读方式打开,文件必须存在。
    	if (pFile != NULL)
    	{
    		fputs("fopen example!", pFile);
    		fclose(pFile);
    	}
    	if (pFile2 != NULL){
    		fgets(buf, 100, pFile2);
    		puts(buf);
    		fclose(pFile2);
    	}
    	pFile2 = fopen("fopentest.txt", "a"); //附加的方式打开,插入字符到文件末尾。如果此文件不存在,则创建它。
    	fputs(buf2, pFile2);
    	fclose(pFile2);
    	pFile2 = fopen("fopentest.txt", "r");
    	fgets(buf, 100, pFile2);
    	puts(buf);
    	fclose(pFile2);
    
    	pFile2 = fopen("fopentest.txt", "w");
    	fputs("fopen example2!", pFile2);
    	//fgets(buf, 100, pFile2); //向文件内写数据的时候,如果向要查看写入的数据,必须先关闭文件重新打开,否则会影响刚刚写入的数据。
    	fclose(pFile2);
    	pFile2 = fopen("fopentest.txt", "r");
    	fgets(buf, 100, pFile2);
    	fclose(pFile2);
    	return 0;
    }
    

      

  • 相关阅读:
    数组塌陷现象
    深浅拷贝的区别
    冒泡排序,选择排序的应用
    JavaScript双重循环的嵌套
    Css Grid网格布局
    css3动画详细介绍
    Python tkinter Label Widget relief upload image
    XXXFD
    XXX1
    Python爬取一个简单网页的HTML代码
  • 原文地址:https://www.cnblogs.com/mrethan/p/4161735.html
Copyright © 2011-2022 走看看