Strange fseek behaviour in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
markcrosbie
Posts: 34
Joined: 30 Sep 2010, 09:56
Location: Ireland
Contact:

Strange fseek behaviour in NXC

Post by markcrosbie »

Hi John,

I've been experimenting with the fseek and ftell functions. Now according to the NXC documentation guide ftell will always return -1. However when I test the fseek() function after creating a file to move the file cursor to the end of the file I get an error 0x9300 - LDR_ILLEGALHANDLE. Does the current NXC release support moving the file cursor on a file that is open for writing? For example can I open a file for append and then move the file cursor back to the start of the file to write at the beginning of the file?

Here is my sample code:

Code: Select all

//
// File position testing
// Use the rewind, fseek, ftell functions to position the 
// file read cursor

#define FILENAME "seeker.txt"
#define FILESIZE 100

task main() {
	
	unsigned int r;
	int f;
	int cursor;
	
	DeleteFile(FILENAME);
	r = CreateFile(FILENAME, FILESIZE, f);

	// where is the file point at?
	cursor = ftell(f);
	TextOut(0, LCD_LINE1, FormatNum("ftell=%d", cursor));
	
	// move to the end of the file
	r = fseek(f, 0, SEEK_END);
	if(r != LDR_SUCCESS) {
		TextOut(0, LCD_LINE2, "fseek failed");
		TextOut(0, LCD_LINE3, FormatNum("Err= 0x%x", r));
		Wait(5000);
		StopAllTasks();
	}
	cursor = ftell(f);
	TextOut(0, LCD_LINE1, FormatNum("ftell=%d", cursor));

	Wait(5000);
		
}
Any ideas?

Thanks,
Mark
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests