Pages

Wednesday, February 5, 2014

fread and fwrite

Lab 2 cont. Arban Nichols

fread and fwrite

fread (void* ptr, size_t size, size_t count, FILE* stream)

header file: #include <cstdio.h>

Reads an array of count elements each with a size of size bytes from the stream and stores them in a block memory specified by ptr.

sample code:

#include <cstdio.h>
#include <cstdlib.h>

int main()
{
  FILE * p;
  char buf[100];
  size_t buffer;
  long arrSize
  p = fopen("myfile.txt" , w+);
  fseek (p , 0 , SEEK_SET);
  buf = (char*) malloc (sizeof(char)*arrSize);
  buffer = fread (buf, 1 , arrSize , p)
  fclose(p);
  free(buf);
  return 0;
}

fwrite (const void* ptr, size_t size, size_t count, FILE* stream)

header file: #include <cstdio.h>

Writes an array of count elements each with a size of size bytes from the stream and stores them in a block memory pointed to by ptr to the current position in the stream.

sample code:

#include <cstdio.h>
#include <cstdlib.h>

int main
{
  FILE * p;
  char buf[] = { 'I' , 'P' , 'C' };
  p = fopen("myfile.txt" , w+);
  fwrite (buf , sizeof(char) , sizeof(buf) , p);
  fclose(p);
  return 0;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.