Does a file exist

const char *MyFilePath = "/usb/myfile.txt";

	//----- DOES FILE EXIST -----
	struct stat sb;
	int Result = stat(MyFilePath, &sb);
	if ((Result != 0) || (sb.st_mode & S_IFDIR))
	{
		//FILE DOES NOT EXIST

	}

fopen()

const char *MyFilePath = "/usb/myfile.txt";
FILE *File1;

	File1 = fopen(MyFilePath , "r");
	if (File1 == NULL)
	{
		//Failed to open file

	}

//access_mode
//	"r"  Open a file for reading. The file must exist.
//	"r+" Open a file for reading and writing. The file must exist.
//	"w"  Create an empty file for writing. If a file with the same name already exists its content is erased.
//	"w+" Create an empty file for writing and reading. If a file with the same name already exists its content is erased before it is opened.
//	"a"  Append to a file. Write operations append data at the end of the file. The file is created if it doesn’t exist.
//	"a+" Open a file for reading and appending. All writing operations are done at the end of the file protecting the previous content from being overwritten. You can
//		 reposition (fseek) the pointer to anywhere in the file for reading, but writing operations will move back to the end of file. The file is created if it doesn’t exist.

Write text file

	const char *DOWNLOAD_LOG_FILE_PATH = "/usb/esp/test.txt";

	//----- CREATE FILE -----
	ESP_LOGI(TAG, "Creating file");
	FILE *File1 = fopen(DOWNLOAD_LOG_FILE_PATH, "w");	//"w"  Create an empty file for writing. If a file with the same name already exists its content is erased.
	if (File1 == NULL)
	{
		printf("Failed to open file for writing\n");
		return;
	}
	fprintf(File1, "Hello World!\n");

	int MyValue = 123;
	fprintf(File1, "MyValue:%d %s\n", MyValue, "some text");

	fclose(File1);

Read text file


	const char *DOWNLOAD_LOG_FILE_PATH file_path = "/usb/esp/test.txt";

	//----- READ FILE -----
	FILE *File1;
	ESP_LOGI(TAG, "Reading file");
	File1 = fopen(DOWNLOAD_LOG_FILE_PATH, "r");
	if (File1 == NULL)
	{
		printf("Failed to open file for reading\n");
		return;
	}

	char LineBuffer[64];
	fgets(LineBuffer, sizeof(LineBuffer), File1);
	//fgets_our_version (&LineBuffer[0], sizeof(LineBuffer), File1)		//<Or use our better version of fgets()
	fclose(File1);

Get file length

//*************************************
//*************************************
//********** GET FILE LENGTH **********
//*************************************
//*************************************
long int GetFileLength (char *FileName)
{

	//Open the file
	FILE *File1 = fopen(FileName, "r"); 
	if (File1 == NULL)
		return 0;
	
	//Seek to the end
	fseek(File1, 0L, SEEK_END); 
	long int FileLength = ftell(File1); 

	//Close the file
	fclose(File1); 

	return(FileLength); 
}

Delete file

	remove(FilePath);

printf the contents of a text file

//***************************************
//***************************************
//********** DEBUG OUTPUT FILE **********
//***************************************
//***************************************
void DebugOutputFile (char* FilePath)
{
	FILE *File1;
	char LineBuffer[512];

	printf("Output of file %s:\n", FilePath);

	File1 = fopen(FilePath, "r");
	if (File1 == NULL)
	{
		printf("Failed to open file for reading\n");
		return;
	}

	while (fgets_our_version (&LineBuffer[0], sizeof(LineBuffer), File1) != 0x00)
		printf("%s\n", LineBuffer);

	fclose(File1);
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *