About Linux File Operations 1.1

Source: Internet
Author: User

This article records people in the learning of Linux, some of the better problems to share.

First come to the experimental topic:

Programming to implement a program that is capable of printing the current system time to the screen every second, and the current line number such as the following

        

The program should loop indefinitely until the process is forced to break (for example, by Ctrl-c Interrupt program). then start the program , Append the system time to the original file, and the sequence number can follow the last sequence number:

Well, after reading the topic, what we should think is, what knowledge do we need to achieve such a function? What is the logic that we implement the function? We need to be aware of the details in writing the code (PS: Make the code perfect!) )

A): Take five minutes to figure out what the hell this thing is.

b): Take another 10 minutes and think about what logic we're using to implement it.

c): 10 minutes to finish writing the code ' 0_0 ' ....

D): Finally take some time to see what the code needs to improve (of course, this code is relatively simple, when calling the standard IO function, it is okay to be aware of the error. Of course, this needs to be a man at the Linux terminal. Help documentation)

================ tools used Ubantu virtual machines---gcc compilers =====================

===============================================================================================

Well, don't say anything. Code stickers on

1#include <time.h>2#include <stdlib.h>3#include <stdio.h>4#include <string.h>5 6 intMainvoid)7 {8FILE *FP = fopen ("Test.txt","A +");//opened in append mode, not created automatically, readable and writable. 9 Ten     Charmsg[ -]; One     intline =0; A      while(1) -     { -         if(Fgets (MSG, -, fp) = =NULL)//Use a speculative method to figure out how many lines are in the current file the              Break; -line++; -     } -printf"Line :%d\n", line); +  - time_t t; +      while(1) A     { at         //returns the current system time (from 0 o'clock 1970.01.01 to the current number of seconds) -Time (&t);  -line++; -  -         Char*now = CTime (&t); Conversion Format -printf"%d.%s", line, now); in  -snprintf (MSG, -,"%d.%s", line, now); Save to Memory msg to fputs (MSG, FP); + fflush (FP); Force flush to memory -  theSleep1); *     } $ Panax Notoginseng     return 0; -}

What code is simple?

And I said, "Yes ......"

Let's start with the first question:

A): What a ghost.

This is a program that continuously prints the current system time to the screen, displays the current line, and uses the file store information for the next program to read and write

This requires the use of Linux file Operations knowledge: From this knowledge and the two major knowledge 1. Open close file, read file related information 2. File buffer type

If these do not understand go out to the left there is a Baidu, see no 0_0-....

Say the second question.

============= consider the situation that just came in .... ======================

: b) What logic is used to implement

1. First we need a file, how does this file open? By looking at the man manual we know that using a + mode is the best (see note)

2. We define the variables to print out the line number and time.

3. We use a common TXT file to hold the current system time, the line number of this string, using the snprintf function to implement the string copy memory, 50 bytes per storage (because each row of data less than 50 bytes),

Then use the Fgets function to copy the data inside the memory msg into the file.

3. Because the normal file is fully buffered, this file will not be flushed to the kernel when the current program does not exit normally! So we'll just add a sentence after the snprintf function Fflush Force flush to the kernel

================ accidentally by Ctrl + C off =============================

4. We need to find the current line number in the file where we saved the information (the system helps us to generate it automatically), and we can use a more speculative approach:

Because we were 50 bytes in a row to the file, so we can be 50 bytes a line to read out, each line to read a row number plus one, so you can get the current line number (time also need to add one ...)

Say the third question.

c) Write code: 0_0^ ...

At last

:D) What needs to be improved in this program?

1. Open a file, generally can be opened smoothly, but also may not open, it is we need to judge

if (fp = = NULL)  {      //requires related header file 3 printf (" %s open failed%s", "test.txt", Strerror (errno)); 4 exit (0);  3;
Free (FP);

2. Because the stack space is limited, we generally use the larger memory when using heap memory

  

char *buf  =  Cmalloc (1,+);
You need free (msg) to run out.

=========================== Summary ================================

This function is actually very simple, the implementation is not complex, in fact, it is used to write and read the file.

There is another solution to this topic in the next article, "About Linux file operation 1.2."

About Linux File Operations 1.1

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.