File Operation series-(C run-time Library)

Source: Internet
Author: User
Tags microsoft c

Now I want to make a task. Because of the wide range of knowledge involved, I need to sort out the knowledge of the system. I just used the file operation to sort out the relevant points.

There are many ways to operate files. Here we will focus on a few of them. This article introduces how to operate files in C run-time library.

There are two ways to operate files in the C run-time Library: one is to directly use the library API; one is to use file I/O standard stream operations. The descriptions are as follows.

1. Use the API of the C run-time library to operate files
. Because C run-time library is a process language, there is no class concept, and most of the functions in it are process. Example:

Read files:

Char buffer [60000]; <br/> int FH; <br/> unsigned int nbytes = 60000, bytesread; <br/>/* Open File for input: */<br/> If (_ sopen_s (& FH, "1.txt", _ o_rdonly, _ sh_denyno, 0 )) <br/>{< br/> perror ("Open failed on input file"); <br/> exit (1 ); <br/>}< br/>/* read in input: */<br/> If (bytesread = _ read (FH, buffer, nbytes) <= 0) <br/> perror ("problem reading file"); <br/> else <br/> printf ("read % u bytes from file/N", bytesread ); <br/> _ close (FH );

Write File:

Int filehandle = 0; <br/> unsigned byteswritten = 0; <br/> char buffer [] = "this is a test of '_ write' function "; <br/> If (_ sopen_s (& filehandle, "write. O ", _ o_rdwr | _ o_creat, <br/> _ sh_denyno, _ s_iread | _ s_iwrite) <br/> return; <br/> If (byteswritten = _ write (filehandle, buffer, sizeof (buffer) =-1) <br/>{< br/> switch (errno) <br/>{< br/> case ebadf: <br/> perror ("bad file descriptor! "); <Br/> break; <br/> case enospc: <br/> perror (" no space left on device! "); <Br/> break; <br/> case einval: <br/> perror (" invalid parameter: Buffer was null! "); <Br/> break; <br/> default: <br/> // an unrelated error occured <br/> perror (" Unexpected error! "); <Br/>}< br/> else <br/>{< br/> printf_s (" wrote % u bytes to file. /n ", byteswritten); <br/>}< br/> _ close (filehandle );

 

2. Use a standard I/O file stream to operate files.
In fact, this method also belongs to the C run-time library function, but it introduces a new structure file, which is easier than directly using the library function. Example:

Read files:

Char filename [50]; <br/> cout <"pleast input the file name you want to read:/N"; <br/> CIN> filename; <br/> file * stream; <br/> char list [30]; <br/> int numread; <br/> If (fopen_s (& stream, filename, "R + T") = 0) <br/>{< br/> // attempt to read in 25 characters <br/> numread = fread (list, sizeof (char), 25, stream); <br/> printf ("number of items read = % d/N", numread ); <br/> printf ("Contents of buffer = %. 25 s/n ", list); <br/> fclose (Stream ); <br/>}< br/> else <br/> printf ("file cocould not be opened/N"); <br/>

Write File:

Char filename [50]; <br/> CIN> filename; <br/> file * stream; <br/> char list [30]; <br/> int I, numwritten; <br/> // open file in text mode: <br/> If (fopen_s (& stream, filename, "W + T") = 0) <br/> {<br/> for (I = 0; I <25; I ++) <br/> list [I] = (char) ('Z'-I); <br/> // write 25 characters to stream <br/> numwritten = fwrite (list, sizeof (char), 25, stream ); <br/> printf ("wrote % d items/N", numwritten); <br/> fclose (Stream ); <br/>}< br/> else <br/> printf ("problem opening the file/N ");

The code is very simple and does not need to be explained too much. For details, refer to the function description above msdn, or refer to Microsoft's c run-time library function reference manual (which is attached to the prepared manual for download ).

It is very efficient to use the run-time library function to operate files. It is just like the advantages of C/C ++, and relies on the language instead of the system, in addition, there is a relatively high memory utilization and low time consumption.

The disadvantage is the same. The security is low and the complexity of Direct Memory operations is high.

If you are interested in running-time library, refer to Microsoft C run-time library function reference manual.

Appendix, sample code of this series
The code is tested and passed in vs2008 + xpsp3.

Next, let's start with the object-oriented file operation method.

Related Article

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.