Have a great day,
Look at simple program example below for reading/writing using /dev/sda device.
//#include <fcntl.h>
//#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define SIZE 1
#define NUMELEM 5
int main(int argc, char* argv[])
{
FILE* fd = NULL;
char buff[100];
memset(buff,0,sizeof(buff)); // write 0 to buff
fd = fopen("/dev/sda","rw+"); // r - Open text file for reading; ‘w+’ : Open for reading and writing. The file is created if it does not exist, otherwise it is truncated.
if(NULL == fd)
{
printf("\n fopen() Error!!!\n");
return 1;
}
printf("\n File opened successfully through fopen()\n");
// buff - Pointer to a block of memory with a size of at least (size*count) bytes, converted to a void*.
// SIZE- size, in bytes, of each element to be read.
// NUMELM - Number of elements, each one with a size of size bytes.
// fd - Pointer to a FILE object that specifies an input stream.
if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fd)) //
{
printf("\n fread() failed\n");
return 1;
}
printf("\n Some bytes successfully read through fread()\n");
printf("\n The bytes read are [%s]\n",buff);
if(0 != fseek(fd,11,SEEK_CUR))
{
printf("\n fseek() failed\n");
return 1;
}
printf("\n fseek() successful\n");
if(SIZE*NUMELEM != fwrite(buff,SIZE,strlen(buff),fd))
{
printf("\n fwrite() failed\n");
return 1;
}
printf("\n fwrite() successful, data written to /dev/sda \n");
fclose(fd);
printf("\n File stream closed through fclose()\n");
return 0;
}
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------