File IO (Basic Knowledge)

Posted jasperzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了File IO (Basic Knowledge)相关的知识,希望对你有一定的参考价值。

 2 (1)
 3     #include<stdio.h>
 4     FILE * myFile = NULL;
 5     myFile = fopen(“input_file.dat” , “r”);
 6     /* r-> open for reading    w-> writing     a-> appending */
 7     If (myFile == NULL)
 8     {
 9         Printf(“error msg
”);
10         Exit(1);
11   }
12   char Buffer[500];
13   int x;
14   fgets(Buffer,100,myFile);
15   sscanf(Buffer,”%d”,&x);
16 
17   char x2[100];
18   myFile = fopen(“output_file.dat”,”w”);
19   /*  output_file.dat doesn’t exist: create it
20   Exist: overwrite  */
21   fprintf(myFile,”This is my output message! 
”);
22   fscanf(myFile,”%c”,&x2);
23 
24   fclose(myFile);
32 
33 (2)
34     FILE *inFile = NULL;
35     FILE *outFile = NULL;
36     // Lab7 infile.dat outfile.dat
37     inFile = fopen(argv[1],”r”);
38     if(inFile == NULL)
39   {
40       Printf(“Error message! 
”);
41       exit(1);
42   }
43   outFile = fopen(argv[2],”w”);
44     if(outFile == NULL)
45   {
46       Printf(“Error message! 
”);
47       exit(1);
48   }
49   while( feof( inFile == 0 ) )
50   {
51       If( fgets(Buffer,1000,inFile) )
52       {
53           fprintf(outFile,”%s”,Buffer);
54     }//Read one line at a time
55   }
56   fclose(inFile);
57   fclose(outFile);
58 
59 //OR:
60 //while (   fgets( Buffer,1000,inFile )  != NULL )
61 //{
62 //    fprintf( outFile,”%s”,Buffer);
63 // }
64 
65     (3)
66         FILE *inFile   = NULL;
67         FILE *outFile = NULL;
68         char Buffer[100];
69     // Lab7 infile.dat outfile.dat
70         inFile = fopen(argv[1],”rb”); // rb->read binary 
71         if(inFile == NULL)
72         {
73             printf(“error Msg!”);
74             exit(1);
75      }  
76      outFile = fopen(argv[2],”wb”); // wb->write binary    
77      int x; double y; int z[10];
78      fread( &x, sizeof(int), 1, inFile ); // data type of &x: void*
79      fread( &y, sizeof(double), 1, inFile);
80      fread ( z, sizeof(int), 10, inFile);
81      char A[20];
82      fread(A, sizeof(char), 20, inFile);
83      fwrite(A, sizeof(char), 20, outFile);
84      fclose(inFile);
85      fclose(outFile);

 

以上是关于File IO (Basic Knowledge)的主要内容,如果未能解决你的问题,请参考以下文章

Machine Learning Basic Knowledge

hausaufgabe--python 36-- Basic knowledge of Class

basic knowledge

Selenium Basic Knowledge

[Tango] Basic Knowledge

[Journey with golang] 1. Basic knowledge