Dear all:
今天試了一下用C++Builder操作txt,即將測驗系統每一次測驗的data存盤在一個txt檔案中,居然沒成功!因為前一次保存的資料會被下一次的資料覆寫掉!是否有方法解決這個問題?以下是我示例程式,請各位不吝賜教啊!!!
void __fastcall TForm1::Button1Click(TObject *Sender)
{
fp=fopen("C:\\t.txt","w+");
fputs("hello txt\n",fp);
fclose(fp);
}
uj5u.com熱心網友回復:
沒人頂,自己頂一下哈!uj5u.com熱心網友回復:
打開檔案后,用fseek函式將偏移量移動到檔案尾。或者fopen用a+b(追加模式)打開檔案。uj5u.com熱心網友回復:
回復gzliudaan:那我按照你的方法試一下哈!uj5u.com熱心網友回復:
把W+換成A或a+,好好看看MSDN的開發檔案是有幫助的。uj5u.com熱心網友回復:
也可以用VCL的函式,FileOpen、FileRead、FileWrite、FileSeek....另外還可以 TStringList 物件
AnsiString fName = "c\\1.txt";
TStringList* li = new TStringList();
li->LoadFromFile(fName ):
li->Text = li->Text + "末尾追加的新內容";
li->SaveToFile(fName);
delete li;
uj5u.com熱心網友回復:
多謝各位的熱情回復,我試過了,就是把W+換成a+,呵呵,多謝各位哈!!!多謝sololie提供的方法,受教了!!
uj5u.com熱心網友回復:
回復sololie:以下用用VCL的函式,FileOpen、FileRead、FileWrite、FileSeek.... 寫字串到txt中,但是還是出現覆寫上一次資料的情況!我可是用的讀寫模式啊!!!怎么回事呢????void __fastcall TForm1::Button2Click(TObject *Sender)
{
String f;
int hand1;
char buf[14]="hello world!\n";
hand1=FileCreate("c:\\test.txt");
Edit1->Text=FileOpen("c:\\test.txt",32);
FileWrite(hand1,&buf,strlen(buf));
FileClose(hand1);
}
uj5u.com熱心網友回復:
32 是啥,mode引數有以下常量定義static const Word fmCreate = 0xffff;
static const Shortint fmOpenRead = 0x0;
static const Shortint fmOpenWrite = 0x1;
static const Shortint fmOpenReadWrite = 0x2;
static const Shortint fmShareCompat = 0x0;
static const Shortint fmShareExclusive = 0x10;
static const Shortint fmShareDenyWrite = 0x20;
static const Shortint fmShareDenyRead = 0x30;
static const Shortint fmShareDenyNone = 0x40;
static const Shortint fmOpenRead = 0x0;
static const Shortint fmOpenWrite = 0x1;
static const Shortint fmOpenReadWrite = 0x2;
static const Shortint fmShareCompat = 0x0;
static const Shortint fmShareExclusive = 0x10;
static const Shortint fmShareDenyWrite = 0x20;
static const Shortint fmShareDenyRead = 0x30;
static const Shortint fmShareDenyNone = 0x40;
Description
The file open mode constants are used when a file or stream is opened to control how it can be shared.
The TFileStream constructor has a Mode parameter that you can set to one of these constants:
Constant Definition
fmCreate If the file exists, open for write access, otherwise, create a new file. Unlike the other constants, which are declared in the SysUtils unit, this constant is declared in classes.h.
fmOpenRead Open for read access only.
fmOpenWrite Open for write access only.
fmOpenReadWrite Open for read and write access.
fmShareCompat Compatible with the way FCBs are opened.
fmShareExclusive Read and write access is denied.
fmShareDenyWrite Write access is denied.
fmShareDenyRead Read access is denied.
fmShareDenyNone Allows full access for others.
const AnsiString fileName = "c:\\test.txt";
// 創建一個檔案,并寫入一些內容
void __fastcall TForm1::btn1Click(TObject *Sender)
{
char buf[]="hello world!\n";
int hand1=FileCreate(fileName);
FileWrite(hand1,&buf,strlen(buf));
FileClose(hand1);
}
// 打開已存在的檔案,并在末尾追加一些內容
void __fastcall TForm1::btn2Click(TObject *Sender)
{
AnsiString buf = "goddamn world!\n";
int hand1=FileOpen(fileName,fmOpenReadWrite|fmShareDenyRead);
if (hand1 == -1)
{
ShowMessage("error: -1");
return;
}
/* Origin 引數值含義,檔案指標指向,0起始,1當前,2末尾
0 The file pointer is positioned Offset bytes from the beginning of the file.
1 The file pointer is positioned Offset bytes from its current position.
2 The file pointer is positioned Offset bytes from the end of the file.
*/
FileSeek(hand1,0,2)
FileWrite(hand1,buf.c_str(),buf.Length());
FileClose(hand1);
}
uj5u.com熱心網友回復:
好專業呀。。。uj5u.com熱心網友回復:
用w是覆寫用a是追加。uj5u.com熱心網友回復:
c++builder人氣越來越不旺了,只有我等菜鳥還在堅持轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/123334.html
標籤:網絡及通訊開發
上一篇:fmpeg編譯出錯
下一篇:彩色醫學影像邊緣提取
