In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to write the code of the Linux file IO, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Brief introduction
All resources under Linux are abstracted as files, so the access to all resources is in the form of device files. The main operations of device files include: open, close, read, write, control, modify attributes and so on. The following sample code is mainly a copy of a text file.
In fact, for some complex devices, the main operation is similar, such as the camera, under the linux is also a device file, after opening, you can read the parameters of the camera, and then you can read the image data, read the image data can be encoded and saved to the file, this is the recording process, you can also read the image data to the LCD display frame cache to display.
For example, serial port operation, under Linux, for serial communication, it is also the read and write operation of device file: open device file-> configuration parameters (baud rate, stop bit, parity bit, etc.)-> read / write data.
Code # include # define M 128 int main (int argc, char * * argv) {if (argc < 3) {printf ("Usage:%s,\ n", argv [0]); return-1;} int fd1,fd2; char buf [M]; int count =-1; memset (buf,'\ 0mm, M) If ((fd1 = open (argv [1], O_RDONLY)) =-1) {perror ("open file1 error:"); return-1;} if ((fd2 = open (argv [2], O_RDWR | Oval creation 0644)) =-1) {perror ("open file2 error:"); return-1 } while (count! = 0) {if ((count = read (fd1,buf,M)) = =-1) {perror ("read file1 error:"); return-1;} if ((count = write (fd2,buf,count)) = =-1) {perror ("write error:"); return-1;}} close (fd1) Close (fd2); return 0;}
Compile and run $gcc copy.c-o copy$. / copy copy test
Running result:
$lscopy copy.c test $diff copy test $
As a result, the program copied a test file to itself, and compared the two files with the diff command, there was no difference, exactly the same, indicating that the copy was successful.
The above is how to write the code of the Linux file IO. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
This article shows you what is the configuration and dependence of the retrieval system in Spring cloud. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article. Workflow project dependence
Wechat
About us
Contact us
Product review
car news
thenatureplanet
More Form oMedia:
AutoTimes.
Bestcoffee.
SL News.
Jarebook.
Coffee Hunters.
Sundaily.
Modezone.
NNB.
Coffee.
Game News.
FrontStreet.
GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.