为了便于大家描述,我就举几个例子吧(只能在原文件操作,不能生成一个副本)
假设文件名为a.txt
1.删除文件中所有长度大于10个字节的字符串
2.在长度小于5个字节的字符串后面加一个分号(;)
3.把所有大于10的数用方括号([])括起来
为了验证正确性,请大家贴上可编译的源代码(最好用标准C++的fstream)
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?
[此贴子已经被作者于2007-10-14 13:22:15编辑过]
(只能在原文件操作,不能生成一个副本)
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;
}