| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 631 人关注过本帖
标题:bidirectional io: how do you shorten file length?
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:6 
bidirectional io: how do you shorten file length?

I was asked a question about fstream a few days:

How do you shorten the file length of a bidirectional io? A requirement is that we can only open the stream once.

I tried to write the following code, but it failed.
======================================

#include <iostream>
#include <string>
#include <fstream>

/** Original content of a.txt is
aba

After running the program, I want the modified content to be
aa

This means that I have to shorten the file size by 1 byte.
*/


using namespace std;

int main()
{
fstream fs("a.txt", ios::in | ios::out);
string s;

fs>>s;
cout<<s<<endl; // s is "aba"
s.replace(s.find("b"), 1, "");
cout<<s<<endl; // s is "aa" now

fs.seekp(ios::beg);
fs.clear();

fs<<s; // this gives "aaa" since we don't shorten the file length

fs.close();

return 0;
}

搜索更多相关主题的帖子: file length shorten 
2007-08-27 11:57
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
能打开两次还好,一次似乎很困难呢。

mark。

Fight  to win  or  die...
2007-08-27 13:34
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
以下是引用HJin在2007-8-27 11:57:37的发言:

I was asked a question about fstream a few days:

How do you shorten the file length of a bidirectional io? A requirement is that we can only open the stream once.

I tried to write the following code, but it failed.
======================================

#include <iostream>
#include <string>
#include <fstream>

/** Original content of a.txt is
aba

After running the program, I want the modified content to be
aa

This means that I have to shorten the file size by 1 byte.
*/


using namespace std;

int main()
{
HANDLE hFile=CreateFile("F:\\1.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0, &hFile);
char buf[MAX_PATH]={0};
DWORD readLen=0;
int size=ReadFile(hFile,buf,MAX_PATH,&readLen,NULL);
string s=string(buf);
cout<<s<<endl; // s is "aba"
s.replace(s.find("b"), 1, "");
cout<<s<<endl; // s is "aa" now
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
WriteFile(hFile,s.c_str(),s.length(),&readLen,NULL);
CloseHandle(hFile);

return 0;
}

API有设置结尾的函数

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-08-28 14:28
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
to wfpb:

thanks for your soln. Don't know if this also works for Linux system.


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-08-28 21:57
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
wfpb
那时vc吗?

Fight  to win  or  die...
2007-08-29 00:33
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
Win32 API

I guess it won't work for standard c++.

However, it is a platform-dependent soln.

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-08-29 04:11
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

sorry,i can't help you !


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-08-29 16:30
快速回复:bidirectional io: how do you shorten file length?
数据加载中...
 
   



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

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