Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (AndrewAMD, monk12, TipmyPip, Quad, aliswee), 1,029 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
limit reading & writing to file #468227
09/26/17 13:35
09/26/17 13:35
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Howdy,

I am trying to append some text from a very big data file to an other big data file. I am using file_asc_read and file_asc_write for this. The appending goes fine however it eventually stops at line ~13982. I think it reached a limit or such. When I append the text another time it ends at line ~27963 (about twice as long). Anyway to bypass this limit? Here is the code that does the copying:

Code:
var _sizeReadFile = file_length(_handleReadFile); //get size
while (_sizeReadFile > 0) {
		var _byte = file_asc_read (_handleReadFile); //read byte
		file_asc_write (_handleSave, _byte); //save byte
		_sizeReadFile -= 1;
	}


Thanks

Re: limit reading & writing to file [Re: Reconnoiter] #468234
09/26/17 18:38
09/26/17 18:38
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
you can copy the content to a buffer and save it. Here goes an untested example:
Code:
long _size = 0;
char *_file = file_load ( "myfile.txt", NULL, &_size );
char *_fileNew = sys_malloc ( _size * 2 );
memcpy ( _fileNew, _file, _size );
memcpy ( _fileNew + _size, _file, _size );
file_save ( "mynewfile.txt", _fileNew, _size * 2 );
file_load ( NULL, _file, NULL );
sys_free ( _fileNew );



Re: limit reading & writing to file [Re: txesmi] #468260
09/27/17 11:50
09/27/17 11:50
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Hey txesmi that does copy the whole data ty, but how do I append it to the original file? (it now overwrites, something like memcat would be nice if that exists :S)

Last edited by Reconnoiter; 09/27/17 11:52.
Re: limit reading & writing to file [Re: Reconnoiter] #468262
09/27/17 12:03
09/27/17 12:03
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
use the stdio.h file API to access files. you can open a file with
Code:
FILE * f = fopen("filename", "a");


to append data to the end of the file.

also try to prevent single-byte reads and read as much data as possible in a single fread call


Visit my site: www.masterq32.de
Re: limit reading & writing to file [Re: MasterQ32] #468267
09/27/17 15:02
09/27/17 15:02
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Hi, tnx for that link & tip. I almost got it working now, there is still a small bug in the code with big data files; as in that it copies & appends everything perfectly but then where it should stop it doesn't and instead appends puts some extra crap. Also the error("reading error") triggers (see code below) so I am probably setting some size variable wrong.

Code:
FILE * _fWrite = fopen(_chr(tmpWrite_str), "a");
	if (_fWrite != NULL) {
		//		error("works");
		FILE * _fRead = fopen(_chr(tmpRead_str), "r");
		if (_fRead != NULL) {
			
			long _lSize;
			char* _buffer;
			size_t _result;
			
			// obtain file size
			fseek (_fRead, 0, SEEK_END);
			_lSize = ftell (_fRead);
			rewind (_fRead);

			// allocate memory to contain the whole file
			_buffer = (char*) malloc (sizeof(char)*_lSize);
			if (_buffer == NULL) error("memory error");

			// copy the file into the buffer
			_result = fread (_buffer, 1, _lSize, _fRead);
			if (_result != _lSize) error("reading error");
			
			// append buffer to write file
			fwrite (_buffer, 1, _lSize, _fWrite);
			
			fclose (_fRead);
			free (_buffer);
		}
		fclose (_fWrite);
	}
	//	else error("error");



Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1