| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5604 人关注过本帖, 1 人收藏
标题:怎么对txt文件同时进行读写操作(是边读边写,不是只读只写)
只看楼主 加入收藏
for37
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-12-19
收藏(1)
 问题点数:0 回复次数:6 
怎么对txt文件同时进行读写操作(是边读边写,不是只读只写)
只读只写我会,大家就不用说了
为了便于大家描述,我就举几个例子吧(只能在原文件操作,不能生成一个副本)
假设文件名为a.txt
1.删除文件中所有长度大于10个字节的字符串
2.在长度小于5个字节的字符串后面加一个分号(;)
3.把所有大于10的数用方括号([])括起来

为了验证正确性,请大家贴上可编译的源代码(最好用标准C++的fstream)
搜索更多相关主题的帖子: txt 文件 
2007-10-14 11:44
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(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?


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-10-14 11:54
xskowscut
Rank: 1
等 级:新手上路
帖 子:81
专家分:0
注 册:2007-10-13
收藏
得分:0 

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


希望能够跟大家学习!交流编程经验!
2007-10-14 12:39
for37
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-12-19
收藏
得分:0 

比如原文件为:
========================
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编辑过]

2007-10-14 13:18
for37
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-12-19
收藏
得分:0 
哪位帮帮忙
2007-10-15 06:23
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(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:
---------------------------------------------------------------------------
http://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;
}


图片附件: 游客没有浏览图片的权限,请 登录注册


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-10-15 08:42
for37
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-12-19
收藏
得分:0 
有简单点的吗?
2007-10-15 16:40
快速回复:怎么对txt文件同时进行读写操作(是边读边写,不是只读只写)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012241 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved