Compare Left & Right Boxes
Stretch to fit Box
Stretch to fit Box Define Syntax Define.
[
[=
],
[=
], ...] Description Assign the same data type to a series of variables. Without this keyword, variables are created with the default type of PureBasic which is the type INTEGER. As a reminder, the type INTEGER is: 4 bytes (with a 32-bit compiler) ranging from -2147483648 to + 2147483647 8 bytes (with a 64-bit compiler) ranging from -9223372036854775808 to +9223372036854775807 Example Define.b a, b = 10, c = b*2, d ; these 4 variables will be byte typed (.b) Define is very flexible because it also allows you to assign a type to a particular variable within a series. Example Define.q a, b.w, c, d ; a, c, and d are Quad (.q) whereas b is a Word (.w) Debug SizeOf(a) ; will print 8 Debug SizeOf(b) ; will print 2, because it doesn't have the default type Debug SizeOf(c) ; will print 8 Debug SizeOf(d) ; will print 8 Define can also be used with arrays, the lists and the maps. Syntax Define
.
[=
] [,
.
[=
], ...] Description Alternative possibility for the variable declaration using Define. Example Define MyChar.c Define MyLong.l Define MyWord.w Debug SizeOf(MyChar) ; will print 2 Debug SizeOf(MyLong) ; will print 4 Debug SizeOf(MyWord) ; will print 2
Stretch to fit Box CreateFile() Syntax Result = CreateFile(#File, Filename$ [, Flags]) Description Create an empty file. Parameters #File The number to identify the new file. #PB_Any can be used to auto-generate this number. Filename$ The filename and path to the new file. If the filename does not include a full path, it is interpreted relative to the current directory. Flags (optional) It can be a combination (using the '| operand) of the following values: #PB_File_SharedRead : the opened file can be read by another process (Windows only). #PB_File_SharedWrite: the opened file can be written by another process (Windows only). #PB_File_NoBuffering: the internal PureBasic file buffering system will be disabled for this file. FileBuffersSize() can not be used on this file. combined with one of the following values (the following flags affect the WriteString()(), WriteStringN(), ReadString(), ReadCharacter() and WriteCharacter() behaviour): #PB_Ascii : all read/write string operation will use ASCII if not specified otherwise. #PB_UTF8 : all read/write string operation will use UTF-8 if not specified otherwise (default). #PB_Unicode: all read/write string operation will use Unicode if not specified otherwise. Return value Returns nonzero if the file was created successfully and zero if there was an error. If #PB_Any was used as the #File parameter then the new generated number is returned on success. Remarks If the file already exists, it will be overwritten by the new empty file. The FileSize() function can be used to determine whether a file exists so the user can be prompted before overwriting a file. To open an existing file for reading/writing, use the OpenFile() function. To open a file for reading only, use ReadFile(). Example If CreateFile(0, "Text.txt") ; we create a new text file... For a=1 To 10 WriteStringN(0, "Line "+Str(a)) ; we write 10 lines (each with 'end of line' character) Next For a=1 To 10 WriteString(0, "String"+Str(a)) ; and now we add 10 more strings on the same line (because there is no 'end of line' character) Next CloseFile(0) ; close the previously opened file and store the written data this way Else MessageRequester("Information","may not create the file!") EndIf See Also OpenFile(), ReadFile(), CloseFile() Supported OS All <- CloseFile() - File Index - Eof() ->
Stretch to fit Box EnableExplicit Procedure CheckForDuplicates() Define inputFile.s, outputFile.s, line.s, prevLine.s, fn = 0 inputFile = OpenFileRequester("Select Input File", "*.*", "Text (*.txt;*.bat)|*.txt;*.bat|All files (*.*)|*.*", 0) If inputFile <> "" fn = CreateFile(1, "output.txt") If fn If ReadFile(0, inputFile) prevLine = "" While Not Eof(0) line = ReadString(0) Debug line If line <> prevLine WriteStringN(1, line) prevLine = line EndIf Wend CloseFile(0) CloseFile(1) Else Debug "Failed to read input file!" EndIf Else Debug "Failed to create output file!" EndIf Else Debug "No input file selected!" EndIf EndProcedure ; Usage example: CheckForDuplicates()
Stretch to fit Box Procedure ScanFile(file.s) Protected fn,line.s,ct,*chr.Unicode,*st,len,key.s fn = OpenFile(#PB_Any,file) If fn Repeat line.s = ReadString(fn,#PB_UTF8) ct+1 *chr=@line If line <> "" *st=*chr Repeat While (*chr\u > 0 And *chr\u <= 32) *chr+2 *st=*chr Wend While ((*chr\u >= '0' And *chr\u <= '9') Or (*chr\u >= 'A' And *chr\u <= 'Z') Or (*chr\u >= 'a' And *chr\u <= 'z') Or *chr\u=39) ;' len+1 *chr+2 Wend If len key = LCase(PeekS(*st,len)) ct = sq\Get(0,@key) + 1 ;look up the word and increment its count sq\Set(0,@key,ct) ;set the word in the dictionary len = 0 *st=*chr+2 EndIf If *chr\u = 0 Break EndIf *chr+2 *st=*chr Until *chr\u = 0 EndIf Until Eof(fn) EndIf ProcedureReturn sq\size() EndProcedure
Stretch to fit Box
Stretch to fit Box