#include <stdio.h> #include <fstream> #include <iostream>//http://www.cnblogs.com/yogurshine/p/ 3677201.htmlusing namespace std; #define ERROR 1#define OK 0int writefilewithcstyle () {float f[6]={3.2f,-4.34f,25.04f,0. 1f,50.56f,80.5f}; FILE *fp;fp=fopen ("1.txt", "w"), if (NULL==FP) {return ERROR;} Fwrite (f,sizeof (float), sizeof (f)/sizeof (float), FP);//binary for (int i=0;i<6;i++) {fprintf (FP, "%f", F[i]);//asic} Fclose (FP); return OK;} int Readfilewithcstyle () {float f[6]={0.0}; FILE *fp;fp=fopen ("1.txt", "R"), if (NULL = = fp) {return ERROR;} /*fread (f,sizeof (float), 6,FP);//binary fclose (FP); for (int i=0;i<6;i++) {printf ("f[%d] value[%2f]\r\n", I,f[i]);} */for (int i=0;i<6;i++) {fscanf (FP, "%f", &f[i]); printf ("f[%d] value[%2f]\r\n", I,f[i]);} return OK; int Writefilewithcppstyle () {fstream file1;file1.open ("2.txt"), if (!file1) {cout<< "OEPN error" <<ENDL;} Char A[10];char b[10];//write file for (int i=0;i<10;i++) {cin>>a[i];file1<<a[i]<< ""; File1.close (); FStream file2;file2. Open ("2.txt"); if (!file2) {cout<< "OEPN error" <<ENDL;} Reads the for (int i=0;i<10;i++) {file2>>b[i];cout<<b[i]<< "" from the file; File2.close (); return OK;} int main () {Writefilewithcstyle (); Readfilewithcstyle (); Writefilewithcppstyle (); return OK;}
C C + + basic input and output