Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by M_D. 04/26/24 20:22
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (PeroPero), 788 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 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