Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Library function file, create, open, read and write, delete, operation implementation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Library function-File access

The previous blog post mainly talked about system calls, and then began to explain library function calls.

Features: C library functions are independent of the operating system platform, whether in nunixlinux, or windows operating system library function calls are the same.

First, open the file

FILE*fopen (cons char*filename,const char*mode)

Filename:

Open file name

Mode:

Open mode

Common open modes:

R rb is opened in read-only mode

W wb is opened in write-only mode. The file does not exist. Create it.

Aminute ab is opened by append. If the file does not exist, it will be created.

Open it in read-only mode. If the file does not exist, create it.

A+, afigb ab+ is opened by reading and appending. If the file does not exist, create a modified file

Creation and opening of library functions

B used to distinguish between binary files

Second, library function reading

Size_t fread (void*ptr,size_t size,size_t n FILECTURE stream)

Features:

Read n fields from the file pointed to by stream, each of which is size bytes, and put the read data into ptr

Returns the number of bytes actually read in the array of characters pointed to.

Third, library function writing

Size_t fwrite (void*ptr,size_t size,size_t n FILECTURE stream)

Features:

Write n fields from the array pointed to by buffer ptr to the file pointed to by stream, each field length is size

That returns the number of fields actually written

Fourth, the library function reads characters

One character from the specified file

# include

Main ()

{

FILE * fp

Char ch

If ((fp=fopen ("c1.txt", "rt") = = NULL)

{

Printf ("\ nCANNOT open file strike any key exit")

Getch ()

Exit (1)

}

Ch=fgetc (fp); / / gets a character and assigns the character to pico ch

Wwhile (chilled EOF)

{

Putchar (ch); / / print the character

Ch=fget (fp); / / continue to get the changed characters

}

Fclose (fp)

}

Fifth, the library function writes characters

# include

Main ()

{

FILE * fp

Char ch

If ((fp=fopen ("c1.txt", "wt+") = = NULL)

{

Printf ("\ nCANNOT open file strike any key exit")

Getch ()

Exit (1)

}

Printf ("input a string:\ n")

Ch=getchar (); / / gets a character and assigns the character to pico ch

While (chauffeured\ n')

{

Fputch (ch,fp); / / write characters

Ch=getchar (); / / continue to get characters such as input

}

Print ("\ n")

Fclose (fp)

}

VI. Library files-formatted reading

Fscanf (FILE*stream,char * format [, argument.])

# include

# include

Void main (void)

{

Int iTunes 10

Double fp=1.5

Char s [] = "this is a string"

Char c ='\ n'

Stream=fopen ("fprintf.out", "w"); / / all operations performed after opening are in the function.

Fprintf (stream, "% s% c", sPermec); / / the first one to write is a string, and the second is a chartype

Fprintf (stream, "% d\ n", I)

Fprintf (stream, "% f\ n", fp); / / floating point quota type

Fclose (steam); / / turn off pointing to file writing

}

Seventh, the location of library function

Int fseek (FILE*stream, long offset,int whence)

Whence:

SEEK_set starts searching from the header of the file

SEEK_CUR searches from the current location

SEEK_END starts searching at the end of the file

Eighth, path acquisition

When writing a program, you sometimes need to know the path.

Char * getcwd (char*buffer,size_t size)

The buffer,getcwd that defines a size size above puts the current pathname copy

In buffer, if the buffer is too small, the function returns-1.

Path acquisition implementation:

# include

Main ()

{

Char buf [80]

Getcwd (buf,sizeof (buf))

Printf ("current working dirctory:% sn", buf)

}

Ninth, create a catalogue

# include

Int mkdir (char*dir, int mode)

Features:

Create a directory

Return value: successful,-1 indicates error.

In the same way as the operation command under linuxshell

# mkdir hello Scripts

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report