Thinking about the problem caused by a carriage return

Source: Internet
Author: User

In the process of maintaining and developing communication software products, some software failure problems often need to be handled. In the beginning of the problem, we may seem to be helpless, there is a kind of day to fall down the feeling. But after the problem is found, people will feel that the cause of the problem is very simple, if the development of the time carefully, it will not make such a low-level mistake. Recently, I have encountered a carriage return caused by the problem.

Recently, in the development of a software version of the need to operate the file, in the code used in the Linux C language of the Opendir function, but the function always returns NULL, the hint file path does not exist.

I looked up the specific situation of the function. The prototype of the Opendir function is:

dir* opendir (const char * path);

Features are:

Open a directory when successful and return an empty pointer when it fails.

Since NULL is always returned in the version, it means that the Opendir function failed to execute.

I find it very strange to meet this problem. Because before you write code using this function, you write a test program to verify the function of the functions and verify that the results are normal. In addition, the program in the test program and version uses the same directory, which also has 777 of the permissions.

What is the reason? The problem of long-term unresolved is easy to cause panic, want to try different solutions. At this point, I suspect that the development platform may be limited to the function, so it will be ported on various platform versions, but the results will be the same.

After struggling for a long time, the problem persists. Helpless, had to turn to colleagues. However, colleagues begin to use the same method, that is, to check whether the directory exists, whether the function contains headers, etc., but all efforts are futile.

URL Address: http://www.bianceng.cn/Programming/project/201602/49622.htm

Finally, there is no way, I directly to the directory in the Code to write dead (that is, fixed a value) to test (the previous directory is a mosaic of multiple variables, now directly to the final directory into the variable szsrcdir). The test actually succeeded, really is inconceivable! At this point, and then carefully looked at the code, immediately without language.

snprintf (Szsrcdir, sizeof (Szsrcdir)-1, "%s/%d/%d/%d/\n",

......,

......,

......,

......);

It turns out that after the assembly path is one more \ n, it leads to a series of subsequent results. This is no wonder, no matter where the transplant, as long as the use of this statement, the result must be the same, is the return character (\ n) caused the trouble. This is really make people laugh and cry, everything is their own coding when not careful.

Modify the code, remove \ n, test pass.

snprintf (Szsrcdir, sizeof (Szsrcdir)-1, "%s/%d/%d/%d/",

......,

......,

......,

......);

The problem is found, but we have to learn chastened, wiser. How do you avoid similar problems in the future? In addition to being extremely attentive in coding, this also requires mastering some basic debugging methods and long-term accumulation of experience. In their own way can not locate the problem, you can only rely on experience, if you do not have enough experience, you need to consult the relevant colleagues, must not be a person buried in there to brood. When you fall into a misunderstanding, it is difficult to get out of it, need to use the strength of others to do. We are working in a team and we always have to remember this sentence.

I would like to say a few words about the software developer's carefulness:

(1) in the process of reading other people's code, I often see the phenomenon of words or writing errors. If the "flag" written "Falg", Will "phone" written "Pone", will "account" written "account" and so on. This does not affect the execution of the program, but it also reflects a developer's literacy from the side. When a customer sees the wrong word or text in your code, he may wonder if the program you're writing is also problematic. After the completion of the code, we have to check more than a few times, generally can avoid such problems. The document is the same, after writing the document, it is best to use the grammar checker to check again.

(2) We have to deal with a lot of emails every day, when reading emails, often see typos, punctuation errors and other phenomena. It's not a serious problem, but it's a bad impression when people see the typos in your emails. Sometimes, a typo can lead to the wrong understanding of others, what the consequences will not be known. Therefore, as a developer, we should also learn Chinese, we must be strict with ourselves.

"Careful sailing ship", for software products, more so. In the process of writing code and documentation, we must eliminate some low-level errors, so that our energies can focus on the most creative things.

AUTHOR:CSDN Blog Zhou

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.