zoukankan      html  css  js  c++  java
  • C语言文件操作

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE * fp, *efp;
        char filePath[]="C:\Users\zxf\Desktop\文件操作\file.txt";
       char name[50], oneLine[1024];
       int i, number;
    
       fp = fopen (filePath, "w+");//清空原有内容打开
       for(i=0;i<3;i++){
            gets(name);
            fprintf(fp, "%s
    ", name);
       }
       fclose(fp);
    
        //添加到文件末尾
       fp = fopen (filePath, "rt+");//可读写,不清空原有内容
       fseek(fp, 0, SEEK_END);//指针标记到文件最后
       fprintf(fp, "%s %s %s %d
    ", "We", "are", "in", 2014);
       fclose(fp);
    
         //
        number=0;
        fp=fopen(filePath,"rt+");
        while(!feof(fp)){
            fgets(oneLine, 1024, fp);
            number=number+1;
        }
        fclose(fp);
    
        i=0;
       fp = fopen (filePath, "rt+");
    
       while(!feof(fp)){
            fgets(oneLine, 1024, fp);
            printf("%d	%s",i, oneLine);
            i=i+1;
            if(i+1==number)break;
       }
       fclose(fp);
    //    //从文件中指定删除一行
    //   fp = fopen (filePath, "rt+");
    //   fseek(fp, 0, SEEK_END);
    //   fprintf(fp, "%s %s %s %d
    ", "We", "are", "in", 2014);
    //   fclose(fp);
    //
       return(0);
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    libiconv2.dll
    windows下安装,配置gcc编译器
    STM32之DMA
    keilkill.bat
    STM32之系统滴答定时器
    STM32库函数开发使用总结
    STM32之串口通信
    yocto系统介绍
    fork子进程僵尸问题及解决方案
    shell实现的守护进程
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3661453.html
Copyright © 2011-2022 走看看