Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ONE FOR THE EXPERTS - Appending lines with different for
Message
From
14/05/1998 00:53:16
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00098397
Message ID:
00099309
Views:
56
>>>>I have an SDF file that I must append into a set of tables. Every line in the file can have a different layout and length depending on what kind of record it is. The kind of record is determined by the first character of the line.
>>>
>>>Then it's not SDF, according to my reading of the docs.
>>>SDF is supposed to have fixed length fields, and not variant records.
>>>
>>>>Here is the basic situation:
>>>>
>>>>INCOMING.SDF can have different layouts on a line by line basis, so I need to be able to look at the first character of an entire line, then append the entire line into a set of tables, like so:
>>>>
>>>>Line Layout 1 --> Table1.dbf (which just holds Layout 1 records)
>>>>Line Layout 2 --> Table2.dbf (etc.)
>>>>Line Layout 3 --> Table3.dbf
>>>>Line Layout 4 --> Table4.dbf
>>>>Line Layout 5 --> Table5.dbf
>>>>Line Layout 6 --> Table6.dbf
>>>>
>>>>I thought about appending the entire file, line by line, into 1 MEMO field (since these lines have over 254 character lengths) in a temporary database, then APPENDing from the temp table into my other DBFs based on left(MEMOFIELD,1).
>>>
>>>Nice idea, but APPEND needs a MEMO clause to append memo fields, and doesn't translate memo to character.
>>>
>>>
>>>I'd either read the file using low level file functions (FREAD, FOPEN, FGETS, FCLOSE) and process it as a string of bytes that I can do anything with.
>>>
>>>OR read the lines and then split out the lines into the 6 different structures in 6 different .SDF files, using FPUTS or FWRITE - so that I could import each of the 6 different formats separately.
>>
>>If each line could be identified with the first char then what about this one :
function LayoutAppend
>>lparameters cTableName, cIdentifier, cASCIIFileName
>>select 0
>>use (cTableName)
>>=afields(aTmpStruc)
>>create cursor tcTemp from array aTmpStruc
>>append from (cASCIIFileName) type sdf
>>select (cTableName)
>>append from tcTemp for eval(field(1)) = cIdentifier
>>use in (cTableName)
>>use in "tcTemp"
Cetin
>
>
>That wouldn't work, because it's not really an SDF file. Each row has a different format. Appending it to a single table, no matter what you name it, will break the fields exactly the same, so that data from one line may have actual fields broken in the middle. The low level file read is the best answer in my opinion. Based on the first character, a routine can be called that does SUBSTR() using fixed, known, starting positions and field lengths for each field, storing the results into variables (or an array, his preference). Then he can do an INSERT INTO the proper target with the values acquired.
>
>Dana
Dana,
Despite it's longer than Dragan's solution, sure it would work. An SDF file doesn't mean it should have a fix format for all lines. Read the original question again. Every table would be appended based on their first character. That means if a group of lines begin with ie: "a" then all those lines written to a separate file would constitute a *suitable* SDF for a given table, right ? This is what you would do with lowlevel. If a field would be broken in the middle, how lowlevel approach would prevent it ? ie:
* Column widths 1,6,10
* SDF containing a line as
amichellegrand
* How would lowlevel understand if this is 
* a, michel, legrand 
* or
* a, michelle, grand ?
You would rely on original column widths ? Yes, and that is what I do too in the above routine. What is the difference other than less coding ? With lowlevel substr() usage is another problem because some lines might well contain no more than just identifier and it doesn't mean it's not an SDF file. An SDF file for a given tables perfectly might have different length lines.
Cetin    Basoz   cetin@neptune.imst.deu.edu.tr
John     Smith
Perfectly consist two records SDF file.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform