Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to create delimited file with non-standard deli
Message
From
13/11/2001 12:16:00
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00580703
Message ID:
00580949
Views:
18
This message has been marked as the solution to the initial question of the thread.
>>>Hi everyone,
>>>
>>>VFP6.0 SP5
>>>
>>>I have a requirement to create a text file from our database, where delimiters would be |^ (two symbols) and char data would not have quotes. Files have ~250 fields and may be huge.
>>>
>>>I'm thinking about two possible approaches:
>>>1) Scan the file and process it manually (here is a subquestion: do it through textmerge or create chunk string and put this string into file)
>>>
>>>2) Use copy to command and process the file afterward (Need some fll to make this replacement).
>>>
>>>What do you think would be better? Does somebody have a similar experience and some benchmarking?
>>>
>>>Thanks a lot in advance.
>>
>>Nadya,
>>Would you elaborate this more. I think you don't mean :
>>
>>COPY TO myfile.txt TYPE DELIMITED with '|' WITH CHARACTER "^"
>>
>>Cetin
>
>No, it inserts additional chars instead. We need Field1|^Field2|^Field3|^Field4

* VFP sample
use employee
copy to testfile delimited
set library to expandchar.fll
*Char2String(cInFileName,cOutFileName,cFieldDelimiter,cReplacementStr[,cTextDelimiter])
Char2String("testfile.txt","testfile.out",",","|^",'"')
* Or
Char2String("testfile.txt","testfile.out",",","|^")
* C source
#include <pro_ext.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char FAR *NullTerminate(Value FAR *cVal)
{
	char *RetVal;
	if (!_SetHandSize(cVal->ev_handle, cVal->ev_length + 1))
	{
		_Error(182); // "Insufficient memory"
	}
	
	((char FAR *) _HandToPtr(cVal->ev_handle))[cVal->ev_length] = '\0';
	RetVal = (char FAR *) _HandToPtr(cVal->ev_handle);
	return RetVal; 
}

void FAR ExpandChars(ParamBlk FAR *parm)
{
	/*
	Parameters :
	cInFile  : Input filename
	cOutFile : Output filename
	cStuff   : Char to seek for replacement
	cStuff2  : String to replace char to
	cChars   : Delimiter Char to filter out, optional - Default doublequote
	*/
	const BUF_SIZE = 4096;
	FILE *fchanin, *fchanout ;
	int numRead,i,j,p,sLen;
    BYTE buffer[BUF_SIZE], outbuffer[BUF_SIZE];
	
	// Prepare parameters
	char *filein  = NullTerminate(&parm->p[0].val);  // Input file
	char *fileout = NullTerminate(&parm->p[1].val);  // Output file
	char *cStuffStr = NullTerminate(&parm->p[2].val);
	char cStuff = cStuffStr[0] ;	// Char to seek for replacement
	char *cStuff2 = NullTerminate(&parm->p[3].val);  // String to replace char to
	sLen = strlen(cStuff2);

	char *cChars = "\"" ;  // Delimiter Char to filter- Default dblquotes;
	if ( parm->pCount == 5 )
		cChars = NullTerminate(&parm->p[4].val);
	char ch = cChars[0];
	// Prepare parameters
	
	_HLock(parm->p[0].val.ev_handle);	    // Lock handle 
	if ((fchanin = fopen(filein,"r")) < 0)	    // Open input file
	{
		_UserError("Could not open file.");	// Couldn't open file 
	}
	_HUnLock(parm->p[0].val.ev_handle);		// UnLock handle 

	
	_HLock(parm->p[1].val.ev_handle);		// Lock handle 
	if ((fchanout = fopen(fileout,"w")) < 0)	// Open output file
	{
		_UserError("Could not open file.");	// Couldn't open file 
	}
	_HUnLock(parm->p[1].val.ev_handle);		// UnLock handle 
	
	

      // Read in byte blocks,
      // remember how many bytes were actually read,
      // and try to write that many out. This loop ends
      // when there are no more bytes to read.

      do
      {
         numRead = fread( buffer, sizeof( char ), BUF_SIZE, fchanin );
		 for(i=0, j=0;i<numRead;i++)
		 {
			 if ( buffer[i] != ch )
			 {
				 if ( buffer[i] != cStuff )
					 outbuffer[j++] = buffer[i];
				 else
				 {
					 for(p=0;p < sLen;)
						 outbuffer[j++] = cStuff2[p++];
				 }
			 }
		 }
         int numwritten = fwrite( outbuffer, sizeof( char ), j, fchanout );
      }
      while (numRead > 0);

	fclose(fchanout);
	fclose(fchanin);
}


FoxInfo myFoxInfo[] =
{
	{"Char2String", (FPFI) ExpandChars, 5, "CCCC.C"},
};


extern "C" {
FoxTable _FoxTable =
{
	(FoxTable *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};
}
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