• C++中关于文本内容的实用操作集合(新)(添加一些关于文件流的介绍)


    首先先给大家一个链接:http://baike.baidu.com/view/1679747.htm

    主要是关于ios的使用,头文件要include<ios>,然后就可以调用下面的一些操作了。

    ios::app: 以追加的方式打开文件
    ios::ate: 文件打开后定位到文件尾,ios:app就包含有此属性
    ios::binary: 以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文
    ios::in: 文件以输入方式打开
    ios::out: 文件以输出方式打开
    ios::nocreate: 不建立文件,所以文件不存在时打开失败
    ios::noreplace:不覆盖文件,所以保存文件时如果文件存在失败
    ios::trunc: 如果文件存在,把文件长度设为0

    今天写程序的时候涉及到了关于文本内容的操作,本来只是解决一个简单的问题,但是自己下午偷了个懒,翻了翻书,看了看那博客,收集了一些关于文本操作的内容,跟大家分享一下。

    批量读写文本内容

    示例代码:

    #include<iostream>
    #include<fstream>
    #include<string>
    #include<stdio.h>
    using namespace std;
    int main()
    {
            int i, natom;
            char filename;
            //写文件
            for (i=0; i<3; i++){
                    sprintf(filename,"t%d.txt",i);
                    cout << filename <<endl;
                    ofstream myfile(filename);
                    myfile << 0 <<endl;
                    myfile.close();
            }
            //读文件
            for (i=0; i<3; i++){
                    sprintf(filename,"t%d.txt",i);
                    cout << filename <<endl;
                    ifstream myfile(filename);
                    myfile >> natom;
                    cout << natom <<endl;
                    myfile.close();
            }
            return 0;
    }
    重点是sprintf的使用,它是一个字符串格式化指令,在这里可以用来对文件名进行逐一读取来控制读取的进度。

    C++读取一个目录下所有文件名称

    示例代码:

    #include   <stdio.h> 
    #include   <dirent.h> 
    #include   <string.h> 
     
     typedef   struct   FileList 
        { 
            char   filename[64]; 
            struct   FileList   *next; 
        }FILENODE; 
     
    FILENODE*  getFiles(char *dir/**//*文目录*/)
    {
       DIR   *directory_pointer; 
       struct   dirent   *entry; 
       directory_pointer=opendir(dir);
       struct FileList start;
       struct FileList *filesNode;
        start.next=NULL; 
            filesNode=&start; 
            while   ((entry=readdir(directory_pointer))!=NULL) 
            { 
                filesNode-> next=(struct   FileList   *)malloc(sizeof(struct   FileList)); 
                filesNode=filesNode-> next; 
                strcpy(filesNode-> filename,entry-> d_name); 
                filesNode-> next=NULL; 
            } 
            closedir(directory_pointer); 
            filesNode=start.next;
            return filesNode;         
    }
     
    int   main() 
    { 
        struct FileList *filesNode;
        char dir[100]="D:\down";  
        filesNode=getFiles(dir);
        
         
        if (filesNode==NULL)
        {
        printf("没有成功");
        return 0;
        }
        while(filesNode) 
        { 
        printf( "%s
     ",filesNode-> filename); 
        filesNode=filesNode-> next; 
        }  
       
        system("pause");
        return 0;
    }

    在文本操作中vector的妙用

    这个是我在CSDN上看到的,觉得总结的挺好的,大家可以看看。

    http://blog.csdn.net/isbnhao/article/details/8052409

    http://blog.csdn.net/isbnhao/article/details/8055359

    其他的还有关于顺序文件的创建和读取,以及其他的一些操作,我会在后面陆续给大家更新,欢迎关注,哈哈。

  • 相关阅读:
    流畅的python,Fluent Python 第四章笔记
    Python中的eval()、exec()及其相关函数(转)
    给自己与初学者关于decode,encode的建议(啥utf-8,GBK)。
    流畅的python,Fluent Python 第三章笔记
    流畅的python,Fluent Python 第二章笔记
    python数组array.array(转帖)
    流畅的python,Fluent Python 第一章笔记
    流畅的Python第五章,一等函数笔记
    python中的__slots__使用极其定义(转)
    load
  • 原文地址:https://www.cnblogs.com/sexybear/p/Cppfile.html
Copyright © 2020-2023  润新知