aboutsummaryrefslogtreecommitdiffstats
path: root/demos/modules/gfile/fatfs/main.c
blob: c5b4392772c7f2ce8efea90db191f97eb0f636ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "gfx.h"
#include <string.h>

/* Function to log messages to a file. */
void LogInfo(const char* msg) {
	GFILE* logFile;
	
	logFile = gfileOpen("info.txt", "a");		// Open the file for append
	if (logfile) {
		gfileWrite(logFile, msg, strlen(msg));
		gfileClose(logFile);					// Close the file again
	}
}

int main(void) {
	GFILE* file;							// GFILE variable to store file info.
	const char msg[] = "Hello file!";		// String to write to a file.
	
	/* Call the µGFX init routine. */
	gfxInit();
	
	/* Mount the file system. */
	if (gfileMount('F', "/"))
		gfxHalt("Can't mount the FAT file system");

	/* Check if a file exists. */
	if (gfileExists("file.txt"))
		LogInfo("[Info]: File exists already!");
	else
		LogInfo("[Info]: The file does not exist yet!");	
	
	/* Write a string to the file. */
	file = gfileOpen("file.txt", "wx");
	if(!file) {
		LogInfo("[Error]: Something went wrong opening the file.");
		gfxHalt("Can't open the file file.txt");;
	}
	
	/* A normal write */		
	gfileWrite(file, msg, strlen(msg));
	
	/* Write the file size in the file using the uGFX equivalent of fprintf(). */
	fnprintg(file, 30, "The file is currently %dkB", gfileGetSize(file));
	
	/* Close the file */
	gfileClose(file);
	
	/* Rename te file. */
	gfileRename("file.txt", "renamedFile.txt");
	
	/* Unmount the file system again */
	gfileUnmount('F', "/");
	
	/* This line should not work as the file system is now unmounted */
	LogInfo("[Info]: Entering enldess while loop.");
	
	/* The program ends here. */
	while(1)
		gfxSleepMilliseconds(200);
}