Home > Back-end >  Questions about c builder operating TXT!!!!!!
Questions about c builder operating TXT!!!!!!

Time:10-09

Dear all:
Today tried to use c + + Builder TXT operation, the test system of each test data stored in a TXT file, there was no success! Because the last saved data will be the next data cover! Q. is there any way to solve this problem? The following is my example program, please feel free to comment!!!

Void __fastcall TForm1: : Button1Click (TObject * Sender)
{
Fp=fopen (" C: \ \ t.t xt ", "w +");
The fputs (" hello TXT \ n ", fp);
The fclose (fp);
}

CodePudding user response:

There is no roof, oneself the top!

CodePudding user response:

After open the file, move the offset to the end-of-file using fseek function, or fopen with a + b (append mode) to open the file,

CodePudding user response:

Reply gzliudaan: it according to your method I try it on!

CodePudding user response:

Change the W + to A or A +, it is helpful to have A good look at the development of the MSDN documentation,

CodePudding user response:

Can also use a function of the VCL, FileOpen FileRead, FileWrite FileSeek...

Also can TStringList object
AnsiString fName="c \ \ 1. TXT";
TStringList * li=new TStringList ();
Li - & gt; LoadFromFile (fName) :
Li - & gt; Text=li - & gt; The Text + "appended to the new content";
Li - & gt; SaveToFile (fName);
The delete li;

CodePudding user response:

Thank you enthusiastic reply, I tried, is to change the W + a +, ha ha, thank you!!!!!!
Thanks for sololie provide method, educated!!

CodePudding user response:

Reply sololie: the following function, use the VCL FileOpen, FileRead, FileWrite FileSeek... Write a string to the TXT, covering the last data but still happen! But I use to read and write mode!!!!! What's the matter??????
Void __fastcall TForm1: : Button2Click (TObject * Sender)
{
String f;
Int hand1;
Char buf [14]="hello world! \n";
Hand1=FileCreate (" c: \ \ test TXT ");
Edit1 - & gt; Text=FileOpen (" c: \ \ test TXT ", 32).
FileWrite (hand1, & amp; Buf, strlen (buf));
FileClose (hand1);
}

CodePudding user response:

32 what is, mode parameters have the following constant values

The static const Word fmCreate=0 XFFFF;

The static const Shortint fmOpenRead=0 x0;
The static const Shortint fmOpenWrite=0 x1;
The static const Shortint fmOpenReadWrite=0 x2;
The static const Shortint fmShareCompat=0 x0;
The static const Shortint fmShareExclusive=0 x10;
The static const Shortint fmShareDenyWrite=0 x20;
The static const Shortint fmShareDenyRead=0 x30;
The static const Shortint fmShareDenyNone=0 x40;
The static const Shortint fmOpenRead=0 x0;
The static const Shortint fmOpenWrite=0 x1;

The static const Shortint fmOpenReadWrite=0 x2;
The static const Shortint fmShareCompat=0 x0;
The static const Shortint fmShareExclusive=0 x10;
The static const Shortint fmShareDenyWrite=0 x20;
The static const Shortint fmShareDenyRead=0 x30;
The static const Shortint fmShareDenyNone=0 x40;

The Description

The file open mode constants are informs The when a file or stream is The opened to control how it can be Shared.

The TFileStream constructor has a Mode parameter that 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 the 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".

//create a file, and write some content
Void __fastcall TForm1: : btn1Click (TObject * Sender)
{
Char buf []="hello world! \n";
Int hand1=FileCreate (fileName);
FileWrite (hand1, & amp; Buf, strlen (buf));
FileClose (hand1);
}

//open an existing file, and in the end some additional content
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 meaning parameter values, the file pointer to, and at the end of beginning 0 and 1 at the moment, 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. The Length ());
FileClose (hand1);
}





CodePudding user response:

Good professional ah,,,

CodePudding user response:

With w is covered with a is additional,

CodePudding user response:

C + + builder less and less popular, only I am a rookie still sticking to it
  • Related