How to convert this PHP function into C?
function adx_store_data(filepath, data)
{
$fp = fopen(filepath,"ab+");
if($fp)
{
fputs($fp,data);
fclose($fp);
}
}
Call write() function on the file object, and pass the string to write() function as argument. Once all the writing is done, close the file using close() function.
Strings can easily be written to and read from a file.
Writing Data into a File. The putc() function is used to write a character to a file whereas the fputs() function is used to write a line of text into a file. The syntax for putc is: int putc(char c,FILE* fp);
#include <stdio.h>
void adx_store_data(const char *filepath, const char *data)
{
FILE *fp = fopen(filepath, "ab");
if (fp != NULL)
{
fputs(data, fp);
fclose(fp);
}
}
Something like this should do it:
#include <stdio.h>
: : :
int adxStoreData (char *filepath, char *data) {
int rc = 0;
FILE *fOut = fopen (filepath, "ab+");
if (fOut != NULL) {
if (fputs (data, fOut) != EOF) {
rc = 1;
}
fclose (fOut); // or for the paranoid: if (fclose (fOut) == EOF) rc = 0;
}
return rc;
}
It checks various error conditions such as file I/O problems and returns 1 (true) if okay, 0 (false) otherwise. This is probably something you should be doing, even in PHP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With