C Pointer Programming Path---sixth notes

Source: Internet
Author: User
Tags fread

Pointers to file types
In C language files you read and write generally use the library functions of the system to read and write data
Pointer to the type of the file
Structure of the file
#include <iostream>
#include <cstdio>
using namespace Std;
typedef struct
{
Short level; The extent to which the buffer is full or empty
unsigned flags;File Status Flag
Char FD; File descriptor
unsigned hold;If no buffer does not read characters
Short basize; Size of the buffer
unsigned char *buffer;The location of the data buffer
unsigned char *curp;Pointer to the current point
unsigned istemp;Temporary files
Short token; Check for validity
}file;




Pointer to file
FILE *FP;


For example, the following example
Enter a character and display it in the screen
#include <stdio.h>
int main ()
{
char ch = getchar ();
Putchar ();
return 0;
}


The scanf () function prototype is
int scanf (char const *format ...);
//
Putchar () function
Prototype is
int Putchar (int character)
Example
#include <stdio.h>
int main ()
{
char ch = ' a ';
Puchar (CH); Output character A
Putchar (' \ n ');
Putchar (' abc ');Only one character is output
Putchar (' \101 ');Output character ' A '
Putchar (' \015 ');Output Enter, no wrapping, moves the cursor's current position to the beginning of the line
Putchar (' \ ');Output single quote character
return 0;
}




File processing field
function =====//function
Turn on or off fopen (), fclose ();
Read-write characters Fgetc (), FPUTC ();
Read and Write strings Fgets (), fputs ();
Read and write by data block Fread (), fwrite ()
Formatted read-write fscanf (), fprintf ()
File location Fseek (), Rewind (), Ftell ()
Determine if the file is closed Feof ();
//
Application Examples:
#include <iostream>
#include <cstdio>
using namespace Std;
int main (int argc, char *argv[])
{
Determine the number of parameters
if (argc! = 3)
{
printf ("The number of arguments not correct!\n");
Exit (1);
}


FILE *FPR, *FPW;
Open source file
if ((FPR = fopen (argv[1], ' r ')) = = = NULL)
{
printf ("Open source file error!");
Exit (1);
}


Open the destination file
if ((FPW = fopen (argv[2], ' w ')) = = NULL)
{

printf ("Open destination file error!");
Exit (1);
}


Char ch;
Copy source file to target file, read and write one character at a time
while ((ch = fget (FPR))! = EOF)
{
FPUTC (CH, FPW);
}
Close source file, target file
Fclose (FPR);
Fclose (FPW);
return 0;

}



Merges two integer files that have been sorted in ascending order to become a file sorted by incremental file
#include <iostream>
#include <cstdio>
using namespace Std;


int main ()
{
FILE *FP1;
FILE *FP2;
FILE *FP3;
int T1, T2;
if (AGRC! = 4)
{
printf ("The number of arguments not correct!\n");
Exit (1);
}




if ((FP1 = fopen (Argv[1], "r")) = = = NULL)
{
printf ("Open source File1 error\n");
Exit (1);
}


if ((FP2 = fopen (Argv[2], "r")) = = = NULL)
{
printf ("Open source File2 error\n");
Exit (1);
}


if ((FP3 = fopen (Argv[3], "r")) = = = NULL)
{
printf ("Open source File3 error\n");
Exit (1);
}


Fread (&t1, sizeof (int), 1, FP1);
Fread (&t2, sizeof (int), 2, FP2);

while (! feof (FP1) &&!feof (FP2))
{
if (T1 < T2)
{
Fwrite (&t1.sizeof (int), 1, FP3);
Fread (&t1, sizeof (int), 1, FP);
}
Else
{
Fwriter (&t2, sizeof (int), 1, FP3);
Fwriter (&t2, sizeof (int), 1, FP2);
}
}


while (! Feof (FP1))
{
Fwrite (&t1.sizeof (int), 1, FP3);
Fwrite (&t1,sizeof (int), 1, FP1);
}


while (!feof (FP2))
{
Fwrite (&t2, sizeof (int), 1, FP3);
Fwrite (&t2, sizeof (int), 1, FP2);
}
Fclose (FP1);
Fclose (FP2);
Fclose (FP3);
return 0;
}

C Pointer Programming Path---sixth notes

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.