栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

How to operate file in C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

How to operate file in C++

The basic uses for file operation in C++
  1. create a file named test.txt and write some random number in it
  2. open this file and print all the numbers in this file
  3. set the read position to get the specific number
The codes for those
#include 
#include 

using namespace std;
int main()
{
    ofstream outfile;
    outfile.open("test.txt", ios::out | ios::app | ios::binary);
    // write some radom numbers to the file
    for (int i = 0; i < 100; i++)
    {
        int num = rand() % 100;
        outfile.write((char*)&num, sizeof(num));
    }
    outfile.close();
    // read all contents of the test.txt file
    ifstream infile;
    infile.open("test.txt", ios::in | ios::binary);
    cout << "Contents of test.txt file: " << endl;
    int num;
    while (infile.read((char*)&num, sizeof(num)))
    {
        cout << num << "  ";
    }
    cout << endl;
    infile.close();
    // read the content from 2nd byte to 5th byte
    ifstream infile2;
    infile2.open("test.txt", ios::in | ios::binary);
    cout << "a number in Contents of test.txt file from 0th: " << endl;
    infile2.seekg(0);
    infile2.read((char*)&num, sizeof(num));
    cout << num << endl;
    infile2.seekg(4);
    infile2.read((char*)&num, sizeof(num));
    cout << "a number in Contents of test.txt file from 1st: " << endl;
    cout << num << endl;
    infile2.close();

    return 0;
}

The corresponding results
Contents of test.txt file:
83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39  83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39  83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39  83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39  83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39  83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39  83  86  77  15  93  35  86  92  49  21  62  27  90  59  63  26  40  26  72  36  11  68  67  29  82  30  62  23  67  35  29  2  22  58  69  67  93  56  11  42  29  73  21  19  84  37  98  24  15  70  13  26  91  80  56  73  62  70  96  81  5  25  84  27  36  5  46  29  13  57  24  95  82  45  14  67  34  64  43  50  87  8  76  78  88  84  3  51  54  99  32  60  76  68  39  12  26  86  94  39
a number in Contents of test.txt file from 0th:
83
a number in Contents of test.txt file from 1st:
86
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835664.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号