Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file open

Status
Not open for further replies.

esdelphi

Programmer
Jun 3, 2003
4
US
Hi. I have a question about file open and read, and possible need to use array too. I have to read 10 files (they are wave files), and read them 1 by 1. Should I use fmOpenRead? What is the syntax ?

Also, the files' names are like ABC100, ABC102, ABC103, ... ABC109. Is there a way to truncate the file name into 3 parts (ie. 'ABC', '1', and '00' for filename 'ABC100') ?

Thanks a lot!
 
I prefer TFileStream to the old Pascal file procedures:
Code:
with TFileStream.Create(AFileName, fmOpenRead + fmShareDenyNone) do
try
    SetLength(ThisBuffer, Size);
    Read(PChar(ThisBuffer)^, Size);
finally
    Free;
end;

ThisBuffer is a string variable holding the entire file.

You can parse your file names as strings any way you like. If they have a fixed layout, the
Code:
Copy
function will extract substrings. The
Code:
ExtractFileName
and
Code:
ChangeFileExt
functions are also useful.

Cheers
 
Thanks a lot for your reply, richardchaven! I still have a question though, what is the "^" means on the command you typed for Read(PChar(ThisBuffer)^, Size); ?? Also, could you tell me more about the copy function which will extract substrings? Why I need to truncate the fileanme is because I have to load 10 files (ie. ABC100 - ABC109) at 1 time according to the middle part of the filename. The "1" determine set #1 of wave files, and then the second set will be ABC200 - ABC209, the 3rd set will be ABC300 - ABC309 and so on.
I am new to Delphi and still trying to learn the syntax and command. Thanks again for your help!
 
The TStream.Read and .Write are still tied to Pascal. They read and write the variable contents blindly.

Code:
TStream.Write(ThisInteger, SizeOf(Integer));
writes the binary representation of an integer.

If we want to read and write strings (which we almost always do), we have to give these methods a variable pointing to the raw data. A PChar(ThisString) is a pointer to the actual text of a string variable. PChar(ThisString)^ is the equivelent of a Char variable of the first character of a string.

The second argument of the Read and Write methods is the length in bytes. When we say Write(PChar(ThisString)^, 23), it will write the first character and the next 22 bytes of memory whatever that is. We know it will be the next characters in the string, but .Write does not care.

---------------------------------------------------

Copy(ThisString, StartingPosition, NumberOfCharacters);

If StartingPosition is past the end of the string or NumberOfCharacters is zero (0), you get an empty string.

If StartingPosition + NumberOfCharacers is longer than the string, you just get all the characters to the end of the string. I use MaxInt for NumberOfCharacters when I want to get the right-hand parts of strings.

Cheers
 
Thank you for the explanation. I understand it now. I appreciated all your help! THANK YOU!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top