The path of C pointer programming and the path of pointer Programming

Source: Internet
Author: User

The path of C pointer programming and the path of pointer Programming
// Pointer to the file type
// In C language files, you generally use the system's library functions to read and write data.
// File type pointer
// File structure
# Include <iostream>
# Include <cstdio>
Using namespace std;
Typedef struct
{
Short level; // The degree to which the buffer is full or empty
Unsigned flags; // indicates the File status.
Char fd; // file descriptor
Unsigned hold; // do not read characters without a buffer
Short basize; // buffer size
Unsigned char * buffer; // data buffer location
Unsigned char * curp; // The current pointer
Unsigned istemp; // temporary file
Short token; // used for validity check
} FILE;




// Pointer to the file
FILE * fp;


// Example:
// Enter a character and display it on the screen
# Include <stdio. h>
Int main ()
{
Char ch = getchar ();
Putchar ();
Return 0;
}


// The prototype of the scanf () function is
// Int scanf (char const * format ...);
//
// Putchar () function
// The prototype is
// Int putchar (int character)
// Example
# Include <stdio. h>
Int main ()
{
Char ch = 'a ';
Puchar (ch); // output character
Putchar ('\ n ');
Putchar ('abc'); // only one character is output
Putchar ('\ 101'); // output character 'a'
Putchar ('\ 015'); // press the Input key without line breaks to move the cursor to the beginning of the line.
Putchar ('\ ''); // output single quotes
Return 0;
}




// File Processing Site
// Function ====/// Function
// Enable or disable fopen () and fclose ();
// Read/write characters fgetc (), fputc ();
// Read/write strings fgets (), fputs ();
// Read/write fread (), fwrite () by data block ()
// Format and read/write fscanf (), fprintf ()
// File fseek (), rewind (), ftell ()
// Determine whether the file ends feof ();
//
// Application example:
# 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 the source file
If (fpr = fopen (argv [1], 'R') = NULL)
{
Printf ("open source file error! ");
Exit (1 );
}


// Open the target file
If (fpw = fopen (argv [2], 'w') = NULL)
{

Printf ("open destination file error! ");
Exit (1 );
}


Char ch;
// Copy the source file to the target file and read and write one character at a time.
While (ch = fget (fpr ))! = EOF)
{
Fputc (ch, fpw );
}
// Close the source file and target file
Fclose (fpr );
Fclose (fpw );
Return 0;

}



// Merge two integer files in ascending order into one object in ascending order.
# 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;
}

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.