鱼C论坛

 找回密码
 立即注册
查看: 2630|回复: 2

同时读写文件

[复制链接]
发表于 2017-8-12 00:34:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 luckstudent 于 2017-8-12 00:36 编辑

int main()
{
    fstream inOut("C:\\Users\\lucks\\Desktop\\C++\\text.txt", fstream::ate | fstream::in | fstream::out);
    size_t cnt = 0;
    string line;
    auto endMark = inOut.tellg();
    inOut.seekg(0, fstream::beg);
    while(getline(inOut, line) && inOut && inOut.tellg() != endMark)
    {
        cnt = line.size() + 1;
        auto tempmark = inOut.tellg();
        inOut.seekp(0, fstream::end);
        inOut << cnt;
        if(tempmark != endMark)
            inOut << " ";
        inOut.seekg(tempmark);
    }
    inOut.seekp(0, fstream::end);
    inOut << "\n";
    return 0;
}
txt文档末输出每行字符数,但结果错误。求指点 QQ图片20170812003029.png QQ图片20170812003201.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-12 10:02:04 | 显示全部楼层
在我的试验中是没有问题的:
  1. #include <iostream>
  2. #include <fstream>

  3. using namespace std;

  4. int main() {
  5.     fstream inOut("inout.txt", fstream::ate | fstream::in | fstream::out);
  6.     size_t cnt = 0;
  7.     string line;
  8.     auto endMark = inOut.tellg();
  9.     inOut.seekg(0, fstream::beg);
  10.     while (getline(inOut, line) && inOut && inOut.tellg() != endMark) {
  11.         cnt = line.size() + 1;
  12.         auto tempmark = inOut.tellg();
  13.         inOut.seekp(0, fstream::end);
  14.         inOut << cnt;
  15.         if (tempmark != endMark)
  16.             inOut << " ";
  17.         inOut.seekg(tempmark);
  18.     }
  19.     inOut.seekp(0, fstream::end);
  20.     inOut << "\n";
  21.     return 0;
  22. }
复制代码

其中的 inout.txt 文件内容如下:
  1. line1: hello
  2. line2: world!!!
  3. line3: 012314
  4. line4: 01231484
  5. line5: 012314484
复制代码

注意最后留了个空行,输出结果如下:
  1. line1: hello
  2. line2: world!!!
  3. line3: 012314
  4. line4: 01231484
  5. line5: 012314484
  6. 13 16 14 16
复制代码

当然我把 inout.txt 的换行存为 unix-style,也就是 LF 而不是 CRLF,所以第一行统计的数目为13。

你的统计结果有问题我估计是修改后没有即时保存。我用的比较简单的vscode编辑器,即时观察输出效果:
20170812100112.png

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-12 10:28:29 | 显示全部楼层
我原来是Code::Blocks写的 换成VS2013也没问题。感觉是Code::Blocks自身的问题。看下面代码
  1. int main()
  2. {
  3.     fstream inOut("C:\\Users\\lucks\\Desktop\\C++\\text.txt", fstream::ate | fstream::in | fstream::out);
  4.     fstream out("C:\\Users\\lucks\\Desktop\\new.txt", fstream::out);
  5.     int cnt = 0;
  6.     string line;
  7.     auto endMark = inOut.tellg();
  8.     inOut.seekg(0, fstream::beg);
  9.     while(getline(inOut, line) && inOut && inOut.tellg() != endMark)
  10.     {
  11.         /*auto tempMark = inOut.tellg();
  12.         inOut.seekp(0, fstream::end);
  13.         inOut.seekg(tempMark);*/
  14.         cnt = line.size() + 1;
  15.         out << cnt << line << endl;
  16.     }
  17.     return 0;
  18. }
复制代码


此程序的就是把源文档的每行字符个数加字符输出到新的文档中去。如果没有注释部分,能出来正确结果。但是如果把注释部分加上去,那么Code::Blocks就出问题了,VS2013还是正确的。有大神能输出具体原因么。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 15:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表