Linux C uses strerror to track down error messages

Source: Internet
Author: User

There is a need in recent work: The program processes the files and then moves the processed files away. I used the rename function to move the files, but in a docker environment, the files couldn't be moved. I don't know what the reason is. Now, to adjust the program, if rename to move the file failed, then print the error message, while using another method to move it away. The following is the simplified code.
This program demonstrates the use of the rename (), Strerror (), System () functions.
Preparation:

#include <stdlib.h> #include <stdio.h> #include <errno.h>int main (int argc, char * * argv) {printf (" Process begin at [%p]\n], (void *) &main); if (argc! = 3) {printf ("parameter error! \ n usage: \ t%s old_filename new_filename\n ", argv[0]); return 1;} extern int errno;if (rename (argv[1], argv[2]) {printf ("%s:%s:%d:|errno is [%d] msg is [%s]\n], __file__, __function__,__ Line__,errno, Strerror (errno)); char cmd[1000] = {0};sprintf (cmd, "%s%s%s", "MV", Argv[1], argv[2]); system (CMD); else{printf ("%s:%s:%d:|mv:from [%s] to [%s] ok\n", __file__, __function__,__line__,argv[1], argv[2]);} return 0;}

The normal program, the printed log is:
Process begin at [0X4006B0]
Renamefile.c:main:27:|mv:from [Renametest] to [renametest2] Ok


The Docker program that prints the log is:
filerename| Dccalcfmtbase.cpp|errno is [+] msg is [Invalid cross-device link]
The Docker program, even with the error above, uses the system function to call the MV to move the file away.
The above log, in fact, I am using two programs to test the results of the different branches of the prompt.

Linux C uses strerror to track down error messages

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.