Save array in memory into file : Memory Save « Memory « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C / ANSI-C » Memory » Memory SaveScreenshots 
Save array in memory into file

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

  int i;
  double *p;
  FILE *fp;

  /* get memory */
  p = malloc(10 * sizeof(double));
  if(!p) {
    printf("Allocation Error");
    exit(1);
  }

  /* generate 10 random numbers */
  for(i = 0; i < 10; i++)
    p[i(doublerand();

  if((fp = fopen("my.txt""wb")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  /* write the entire array in one step */
  iffwrite(p, 10*sizeof(double)1, fp!= 1) {
    printf("Write Error.\n");
    exit(1);
  }
  fclose(fp);

  free(p)/* free memory */

  /* get memory again */
  p = malloc(10 * sizeof(double));
  if(!p) {
    printf("Allocation Error");
    exit(1);
  }

  if((fp = fopen("my.txt""rb"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }
  /* read the entire array in one step */
  if(fread(p, 10 * sizeofdouble )1, fp!= 1) {
    printf" Read Error.\n " );
    exit);
  }
  fclosefp );

  /* display the array */
  for(i = 0; i < 10; i++
      printf("%f ", p]);
  free(p);

  return 0;
}


           
       
Related examples in the same category
w__w_w.j___a__va2_s_.___c_o_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.