File operation functions in C Language

Source: Internet
Author: User

During this time, it took a long time to sort out the C language file operations functions, and I was not comfortable with the C language. I finally sorted out some of the functions and it was enough to come to an end.

However, there is another bug. When writing a file, if the length of the written data is greater than the length of the Written string itself, the content of the file will contain more inexplicable data, if you have encountered such problems, tell me why. grateful!

The following is an example of source code:

 

 

# Include <stdio. h>
# Include <stdlib. h>

// ************** Function declaration *******************
Int f_read (char *, long, char *, INT );
/*
Function: Read files.
Parameters:
Char * filename: Address of the file to be read
Offset of long POS records in files
Char * sdata: Address for saving read data
Length of data to be read by INT Len
Return Value:
Read successful 0
Other Error 1
*/

 

Int f_exist (char *);
/*
Function: determines whether a file exists.
Parameters:
Name of the char * filename file to be determined
Return Value:
0
1 does not exist
*/

 

Int f_write (char *, Int, long, char *, INT );
/*
Function: Write a file.
Parameters:
Char * filename name of the file to be written
Int mode write mode
1 append
0 rewrite
Long POS records the offset in the file, which is invalid for Mode 1.
Char * sdata: Address for saving read data
Length of data written by INT Len
Return Value:
Write successful 0
Other Error 1
*/

// ******************* Function Definition ***************
// Entry function
Int main (INT argc, char * argv [])
{
// Step 1: Check whether a file exists? 0: Yes; 1: No
Int rel1 = f_exist ("ww.txt ");
Printf ("file exist? : % D/N ", rel1 );
Char data [255] = {''}; // The initialization string array is empty.

// Step 2: Read the file
F_read ("ww.txt", 0, Data, 255 );
Printf ("Read File: % s/n", data );

// Step 3: Write a file
F_write ("ww.txt", 0, 0, "this is a test file.", strlen ("this is a test file ."));
Printf ("after writing.../N ");
// Step 4: Read the file
F_read ("ww.txt", 0, Data, 255 );
Printf ("Read File: % s/n", data );
System ("pause ");
Return 0;
}

 

// Write an object
Int f_write (char * filename, int mode, long POs, char * sdata, int Len)
{
File * FP;
Int I = 0;
If (mode = 0)
{
Fp = fopen (filename, "WT ");
While (I <Len + POS)
{
If (I> = POS)
Fputc (* sdata ++, FP );
Else
Fputc ('', FP );
I ++;
}
}
Else if (mode = 1)
{
Fp = fopen (filename, "");
While (I <Len)
{
Fputc (* sdata ++, FP );
I ++;
}
}
Else
{
Return 1;
}
Fclose (FP );
Return 0;
}

 

// Determine whether a file exists
Int f_exist (char * filename)
{
File * fp = fopen (filename, "rb ");
If (FP = NULL)
{
Return 1;
}
Else
{
Fclose (FP );
Return 0;
}
}

 

// Read the file
Int f_read (char * filename, long POs, char * sdata, int Len)
{
File * fp = fopen (filename, "RT ");
If (FP = NULL)
{
Return 1;
}
Char CH = fgetc (FP );
If (CH = EOF)
{
Return 1;
}
Int I = 0;
While (I <Len + POS)
{
If (I> = POS)
* Sdata ++ = CH;
Ch = fgetc (FP );
I ++;
}
Fclose (FP );
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.