如何读取和删除TXT文件中的一行,在线·

hnldlily 2003-09-18 03:49:22
小黑豆在吗?
...全文
2737 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohedou 2003-09-18
  • 打赏
  • 举报
回复
这个是MSDN 的:

Microsoft® JScript™
ReadLine Method Language Reference
Version 2

Description
Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.

Syntax
object.ReadLine( )
The object argument is always the name of a TextStream object.

Remarks
The following example illustrates the use of the Line property:

function GetLine()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
f.WriteLine("Hello world!");
f.WriteLine("JScript is fun");
f.Close();
f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
r = f.ReadLine();
return(r);
}
xiaohedou 2003-09-18
  • 打赏
  • 举报
回复
抱歉,才看到!大家已经说得很多了!读学一行CStdioFile方便些!我写了一个CFile的较繁。
struct InfoStr {
char sNo; //序列号,便于查找
char s1;
char s2[16];
} ;

struct {
};
void WriteMsg(char szMsg[64])
{
CFile f;
CFileException e;
char* pFileName =_T("abc.txt");

if( !f.Open( pFileName,CFile::modeCreate | CFile::modeNoTruncate
| CFile::modeWrite| CFile::shareExclusive , &e ) )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
}
InfoStr info;
info.sNo= '1';
info.s1 = '2';
info.s2 ="same string";

f.SeekToEnd();

f.Write(&info, strlen((char *) &info)); //把序号也写入

f.Flush();
f.Close();


//读文件:
UINT nActual = 0;
f.Seek( 0, CFile::begin );
do {
nActual = f.Read( &info, sizeof( info ) );
if (info.sNo == '你的特定行') {
...dosomething...
或用memset(&info,0,sizeof(info));
...f.Write(&info, strlen((char *) &info)); //把空行写入
}

}while (nActual < sizeof(info) ); //文件EOF

}


CFile::Read
virtual UINT Read( void* lpBuf, UINT nCount );
throw( CFileException );

Return Value

The number of bytes transferred to the buffer.
Note that for all CFile classes, the return value may be less than nCount if the end of file was reached.


Richuen22 2003-09-18
  • 打赏
  • 举报
回复
CStdioFile file;
if( !file.Open( "D:\\Test.txt", CFile::modeReadWrite | CFile::typeText ) )
{
AfxMessageBox("打开文件出错!");
return;
}
CString strEnter;
file.ReadString(strEnter); //读取一行
file.Close();

而删除一行的,由于没有相应的函数,比较麻烦,最好是用CStringList,将Test.txt的每一行读入CStringList中,删除时用CStringList::RemoveAt(当前行),最后结束时将CStringList的内容写入原文件中。
Viconia 2003-09-18
  • 打赏
  • 举报
回复
FlyYang(++X++) :

CStdioFile用ReadString是读所有的内容吧
FlyYang 2003-09-18
  • 打赏
  • 举报
回复

如果你用MFC

用 CStdioFile 类向单简单

她可以一行一行的读文本文件文件

---------
++C++
---------
真糨糊 2003-09-18
  • 打赏
  • 举报
回复
要考虑同步。
真糨糊 2003-09-18
  • 打赏
  • 举报
回复
FILE *stream;
TCHAR csText[255];
if(!(stream = _tfopen(_T("c:\\xx.txt"),_T("r"))))
return FALSE;
while( !feof( stream ) )
{
_fgetts(csText,255,stream);//读一行
}
fclose(stream);
hnldlily 2003-09-18
  • 打赏
  • 举报
回复
现在的问题是:这个文件是动态的,经常有数据插入这样有可能会有数据丢失呀。
benz600 2003-09-18
  • 打赏
  • 举报
回复
好象没有简单方法啊
vcforever 2003-09-18
  • 打赏
  • 举报
回复
先把文件的内容都读出来!然后在分析要读的或是要删除的!在内存中将他们分离或删除
然后在写回文件中!
hnldlily 2003-09-18
  • 打赏
  • 举报
回复
总有些区别吧,TXT没SETION,一行就是一个记录,用VC一次读取一行读后再删掉,怎么实现。
能不能详细点。
bluestar 2003-09-18
  • 打赏
  • 举报
回复
一样,和ini没什么区别
hnldlily 2003-09-18
  • 打赏
  • 举报
回复
这是INI文件的,我要TXT文件的
rockersz 2003-09-18
  • 打赏
  • 举报
回复
www.vckbase.com有例子
http://www.vckbase.com/code/listcode.asp?mclsid=15&sclsid=1509
hnldlily 2003-09-18
  • 打赏
  • 举报
回复
up
hnldlily 2003-09-18
  • 打赏
  • 举报
回复
能不能详细一点呀?
  • 打赏
  • 举报
回复
将文件内容读取到缓冲中,然后分析缓冲,将需要删除的内容从缓冲中去掉,然后将缓冲的内容重新写入到文件中去.

好像没有简单的办法
edrftgyh 2003-09-18
  • 打赏
  • 举报
回复
读取回车健!
lzwcom 2003-09-18
  • 打赏
  • 举报
回复
将文件内容读取到缓冲中,然后分析缓冲,将需要删除的内容从缓冲中去掉,然后将缓冲的内容重新写入到文件中去.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧