C Language Files

Source: Internet
Author: User

The little white one, seeing there was a mistake, wished to point out, extremely thankful

/****************************************************************
C Language File Overview:
Generic files can be divided into binary and text files.
Convention: We refer to the form of data on the terminal as the terminal form of the data, without
is called a character form.
Note: The terminal refers to the keyboard display and so on and the user directly deals with the input.
A binary file is the output of a book in memory as it is stored in memory to a magnetic
Storage, which means that the original style of the data is stored.
The text file is the data terminal form of binary data output to disk storage, also
is to say that the data is stored in the terminal form.
****************************************************************/
/* File Open function: fopen function
* Prototype is FILE * fopen (char * filename, char * mode);
* Parameter: FileOpen indicates the specified file name mode refers to how the file is opened
* File Close function: fclose (file pointer)
* The Fclose function also takes back a value, performs a close operation smoothly, returns a value of 0, otherwise
* The return value is EOF (-1).
Note
* (1) Unless otherwise specified, the input is called read, which refers to the input from the file (external memory) to the memory
* Output or write refers to the output from memory to external memory.
* (2) If the open task cannot be implemented, the fopen function will bring back an error message.
* At this point the fopen function will bring back a null pointer value NULL
* NULL is already defined as 0 in the stdio.h file
* Use the following method to open a file
* if (fp = fopen ("filename", "r")) = = = NULL)
*         {
* printf ("Cannot open this file\n");
* Exit (0); or return;
*         }
* (3) When the program starts to run, the system automatically opens 3 standard files: standard output standard
* Input standard error output. Usually these three files are associated with the terminal. So from
* Terminal files are not required to be entered or output to the terminal. The system automatically defines
* 3 file Pointers stdin stdout stderr, respectively, refer to the sub-terminal input terminal output
* Out of standard error output.
*/
FSCANF fprintf formatted input and output for text
The general invocation form is fscanf or fprintf (file pointer, format string, input or output list)
Read and write file test files
# include <stdio.h>

typedef struct STUDENT
{
int code;
Char name[10];
int age;
}st;

int main (void)
{
 file * out;
 int i;
 st a[2];
 //Check for open operation error
 if ((out = fopen ("Student.dat", "w+") = = NULL)
     printf (" Cannot open output file. ");
 for (i = 0; i < 2; ++i)
 {
  //use scanf can also implement this function
  fscanf (stdin, "%d%s%d" , &a[i].code, &a[i].name, &a[i].age);
  fprintf (out, "%d%s%d\n", A[i].code, A[i].name, a[i].age);
 }
 fclose (out);
 
 return 0;
 }
 
 /*
     Run Result:
     1 Kobayashi
   2 Stalin
  
  --------------------------------
  process exited after 29.82 seconds with return value 0
   Press any key to continue ...
    A file named Student.dat is generated in the folder for this program file.
 
 */

/*
The fread and fwrite functions are used for input and output related to binary files
The general invocation form is:
Fread (buffer, size, count, FP);
fwrite (buffer, size, count, FP);
Parameter description:
Buffer is a pointer, for fread, it is read into the data
Address. For Fwrite, it is the address of the data to be output.
The size is the number of bytes to read and write.
Count is the number of bytes of data to read and write to
FP is a file type pointer
*/

# include <stdio.h>

typedef struct STUDENT
{
int code;
Char name[10];
int age;
}st;

int main (void)
{
FILE * out, * in;
int i;
St A[2] = {{1, "www", +}, {2, "HHH", 99}};
if (out = fopen ("Stu.dat", "wb") = = = NULL)
printf ("Cannot open output file.");
for (i = 0; i < 2; ++i)
{
Fwrite (&a[i], sizeof (ST), 1, out);
}
Fclose (out);
if (in = fopen ("Stu.dat", "RB")) = = NULL)
printf ("Cannot open input file.");
for (i = 0; i < 2; ++i)
{
Fread (&a[i], sizeof (ST), 1, in);
printf ("%d,%3s,%d\n", A[i].code, A[i].name, a[i].age);
}
Fclose (in);

return 0;
}

/*
Operation Result:
1, www, 18
2, HHH, 99

--------------------------------
Process exited after 0.0915 seconds with return value 0
Please press any key to continue ...
*/

/*
FGETC functions and FPUTC functions can be used to read and write text files or binaries
Note: The FGETC function and the FPUTC function read and write byte level (1 bytes each time)
Data, so when the FGETC function and the FPUTC function read and write binary files
It is possible to read and write data of any data type through multiple operations.
fgetc function General Invocation form
Character variable = fgetc (file pointer);
FPUTC function General Invocation form
FPUTC (character, file pointer);
Here the character can be part of a byte of data of any data type.
*/

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

int main (void)
{
FILE * FP;
Char ch;
if (fp = fopen ("Out.dat", "w") = = = NULL)
printf ("Cannot open file.");
ch = getchar ();
while (ch! = ' # ')
{
FPUTC (CH, FP);
ch = getchar ();
}
Fclose (FP);

return 0;
}

/*
Operation Result:
W
W
E
3
#

--------------------------------
Process exited after 7.194 seconds with return value 0
Please press any key to continue ...
*/

C Language Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.