C Language file reading and writing and C # basic function learning [figure]

Source: Internet
Author: User
Tags finally block try catch

C Language file reading and writing and C # basic function learning [figure]
Learning a semester of C language, but also do not use pointers, file reading and writing can not be written very smoothly, said to be ashamed. It happened that today my friend asked me to help him write a C language program that averages the large amount of data he uses for his digital models (a physical quantity is tested at different times of the day for dozens of days, averaging the physical quantities measured at those moments). The code is very simple, the key is to master how to read the file and write (of course, for the chicken, I lazy let me in a freshman did not study well, just take advantage of this opportunity to the basic operation of the document learned:))
Sub-modules to write is still very important, can make the program look concise and clear. Two functions were written: The data is read into a two-dimensional array, and the average result of the operation is written to a result file.
For beginners, pay attention to the function calls of the two-dimensional array in the code. void ReadFromFile (double flow[][n]) N can't forget, if you don't write it, just wait for the error.
One thing to note is that you can save the TXT file in the C language of project, in the folder named: "Result in the code, do not add txt,txt is only a text type, in the Read function to write up to identify?" Also write TXT file can not be created first, the program will be automatically generated.

C # Basic Learning 2 (inline functions, type constraints, try Catch,vs debugging)
The calling function requires the CPU to execute the parameters of the stack, register save and restore, jump instructions and other operations, the cost is relatively large, high frequency call function has an impact on performance.
C # does not support inline, but the JIT supports automatic inline, and when il turns into a real machine code, some functions are "automatically" expanded inline, only with very harsh conditions:
1) The function inside has the loop statement, the catch statement and so on complex structure, does not do the inline optimization.
2) function body relatively long do not do inline optimization, only relatively simple can be optimized inline. (Some say IL is less than 32 bytes to do inline)
2) when compiling into machine code, the inline expansion code is shorter than the function call and must be inline.
From the above:
1) property is suitable for automatic inline optimization, do not have to worry about the performance of properties than field fields lower,
2) If you expect the function of the inline optimization, be sure to write small, and strive to compile the Il no more than 32 bytes.
In fact, whether to do inline optimization should be considered from the frequency of the call, and the JIT can not understand the frequency, so the automatic inline optimization effect is not ideal, many people on the web discussed this, but Microsoft is not support C # inline, perhaps Microsoft believes that C + + needs to care about performance, and C # Pay attention to rapid development, regardless of these costs.
2, type constraint
. NET supports type parameter constraints in the following 6 ways:
where T:struct | T must be a struct type, and the type parameter must be a value type.
where T:class | T must be a class type, write class, the result is: must be a reference type, string can, int can not
where T:new () | T must have an parameterless constructor, and when used with other constraints, the new () constraint must be specified last.
where T:nameofbaseclass | T must inherit a class named Nameofbaseclass, stating which class
where T:nameofinterface | T must implement an interface named Nameofinterface
where t:u the type parameter provided for T must be either a parameter supplied for u or a parameter derived from U.
What is the use of type constraints: Prevent classes from being used incorrectly, maintain framework structures, and create reasonable schemas.
The wording is as follows:
public class Receivetest:monobehaviour
{
private void Awake ()
{
test<testapple> t = new test<testapple> (new Testapple ());
}
}
public class Testfruit
{
public void Fun () {Debug.Log ("I Am testfruit fun");}
}
public class Testapple:testfruit {}
public class test<t> where t:testfruit//Type constraints, T can only be classes that inherit from Testfruit
{
Public Test (T t)
{
T.fun ();
}
}
3,try Catch catch exception
Catch an exception in a place you can't control. such as file reading, network connection, operation of external devices. It is best to use exceptions to specify the type of exception, such as IOException, WebException, and so on, rather than just a exception base class.Gulliver's Travels reading notesExperience, abnormal capture is a debugging means, used to find the location and cause of the occurrence of anomalies, can not be taken as a program logic, the completely avoidable exception can never be surrounded by try cacth.
The operation is as follows:
try{
The code that foresees the possibility of throwing an exception is included in the TRY statement block. General put trunk code.
}catch (declaring exception type exception variable) {
The exception is handled here, the code that uses this statement block to report the exception will not let the system collapse, generally is the output error type.
}finally{
Whether the program error or normal operation of this finally is bound to execute, when not needed, this finally block can not be written.
}
If the program is wrong, it will not come here, only when the program is normal to go here.
4,vs Commissioning
Step over, follow the procedure, skip the function and treat the function as a statement. If the loop (Foreach,for,while) is inside a function, the step-by-step does not enter, skipping the function directly.
Step by step, represent a statement walk, enter the function.
Jumps out, jumps directly out of the class, if in the loop, also jumps directly, and also jumps out the function which the loop is in.
Basic operations: Add monitoring to add conditions.

C Language file reading and writing and C # basic function learning [figure]

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.