注册 登录
编程论坛 C++教室

怎么对txt文件同时进行读写操作(是边读边写,不是只读只写)

for37 发布于 2007-10-14 11:44, 5622 次点击
只读只写我会,大家就不用说了
为了便于大家描述,我就举几个例子吧(只能在原文件操作,不能生成一个副本)
假设文件名为a.txt
1.删除文件中所有长度大于10个字节的字符串
2.在长度小于5个字节的字符串后面加一个分号(;)
3.把所有大于10的数用方括号([])括起来

为了验证正确性,请大家贴上可编译的源代码(最好用标准C++的fstream)
6 回复
#2
HJin2007-10-14 11:54
回复:(for37)怎么对txt文件同时进行读写操作(是边...

1.删除文件中所有长度大于10个字节的字符串

Does the "字符串" only contain a..z and A..Z?

Does a "字符串" mean a line (not including the '\n')?

2.在长度小于5个字节的字符串后面加一个分号(;)


3.把所有大于10的数用方括号([])括起来

Do you allow hex numbers such as 0xFF?

#3
xskowscut2007-10-14 12:39

楼主的问题很不清晰哦。你是用什么方式写文件的呢?是二进制吗?而且数据格式也很模糊,操作起来好像有点困难

#4
for372007-10-14 13:18

比如原文件为:
========================
adf 201 684 asd12
asdfdsafsdfasd asdfa1
16 2 as
========================
1的结果:
=====================
adf 201 684 asd12
asdfa1
16 2 as
=====================
2的结果:
======================
adf; 201; 684; asd12
asdfdsafsdfasd asdfa1
16; 2; as;
======================
3的结果:
======================
adf [201] [684] asd12
asdfdsafsdfasd asdfa1
[16] 2 as
======================

不清楚吗?

既然是.txt文件,怎么可能是二进制?
(反正我就是新键一个记事本,在其中写了几个字,是二进制吗?如果有区别,你把两个结果都写出来吧

如果可以的话,请留下QQ号,我的QQ是544125362

[此贴子已经被作者于2007-10-14 13:22:15编辑过]

#5
for372007-10-15 06:23
哪位帮帮忙
#6
HJin2007-10-15 08:42
回复:(for37)怎么对txt文件同时进行读写操作(是边...

(只能在原文件操作,不能生成一个副本)
This is what made your problem hard. I assume this also means
that we can only open the file once.

Without using any API or system depedent functionality, the only
possible way is to use a bidirectional IO --- jumping around
with seekg, seekp, tellg, tellp, etc.

However, there is a fundamental question here: how do you change
the size of a file using only C++ streams?

I tried 1. And the following code is as close to the requirement
as I can get.


程序代码:

/*---------------------------------------------------------------------------
File name:      bccn-对txt文件同时进行读写操作.cpp
Author:         HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on:     10/13/2007 22:32:21
Environment:    WinXPSP2 En Pro + VS2005 v8.0.50727.762



Modification history:
===========================================================================



Problem statement:
---------------------------------------------------------------------------
https://bbs.bc-cn.net/viewthread.php?tid=177484


怎么对txt文件同时进行读写操作(是边读边写,不是只读只写)


只读只写我会,大家就不用说了
为了便于大家描述,我就举几个例子吧(只能在原文件操作,不能生成一个副本)
假设文件名为a.txt
1.删除文件中所有长度大于10个字节的字符串
2.在长度小于5个字节的字符串后面加一个分号(;)
3.把所有大于10的数用方括号([])括起来


为了验证正确性,请大家贴上可编译的源代码(最好用标准C++的fstream)


比如原文件为:
========================
adf  201 684 asd12
asdfdsafsdfasd asdfa1
16 2 as
========================
1的结果:
=====================
adf  201 684 asd12
asdfa1
16 2 as
=====================
2的结果:
======================
adf;  201; 684; asd12
asdfdsafsdfasd asdfa1
16; 2; as;
======================
3的结果:
======================
adf  [201] [684] asd12
asdfdsafsdfasd asdfa1
[16] 2 as
======================


Analysis:
---------------------------------------------------------------------------



Sample output:
---------------------------------------------------------------------------



*/


#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <iterator>
#include <utility>
#include <memory> // auto_ptr
#include <algorithm>
#include <functional>
#include <numeric>
#include <new> // bad_alloc
#include <stdexcept>
using namespace std;



void f1(const string& filename)
{
    fstream fs(filename.c_str());
    if(!fs)
    {
        cout<<\"cannot open file.\n\";
        exit(1);
    }
    vector<string> vs;
    string word;


    while(fs)
    {
        if(fs.peek()=='\n')
        {
            vs.push_back(\"\n\");
            fs.get();
        }
        
        getline(fs, word, ' ');


        if(word.size()<=10)
        {
            vs.push_back(word);
        }
    }
    fs.clear();


    fs.seekp(0, ios::beg);


    for(vector<string>::const_iterator it=vs.begin(); it!=vs.end(); ++it)
    {
        fs<<*it<<\" \";
    }


    fs.close();
}



int main()
{
    string filename = \"a.txt\";
   
    f1(filename);



    return 0;
}


只有本站会员才能查看附件,请 登录

#7
for372007-10-15 16:40
有简单点的吗?
1