C language Review of Android JNI: file read/write and random number Functions

Source: Internet
Author: User

C language is more suitable for operating hardware and is more efficient in some file processing operations, so similar operations generally use JNI to call C code for operations: such as audio, video and image processing:

For example, in audio format Transcoder, similar to photo's rendering of image effects and other applications, a large number of JNI applications are required.


Here we will review the content of the C language operation file:

C file operation mode:

"RT" only opens a text file and only supports reading data.
"WT" only writes to open or create a text file, only data can be written
Append "at" to open a text file and write data at the end of the file.
"Rb" is used to open a binary file and only read data.
"WB" only writes to open or create a binary file, only data can be written
Append "AB" to open a binary file and write data at the end of the file.
"RT +" reads and writes to open a text file, allowing reading and writing
"WT +" reads and writes to open or create a text file, allowing reading and writing
"At +" to open a text file, allow reading, or append data at the end of the file
"RB +" reads and writes a binary file, allowing reading and writing
"WB +" to open or create a binary file and allow reading and writing
"AB +" reads and writes to open a binary file, allowing reading or appending data at the end of the file

The usage of files is described as follows:

The usage of a file is composed of R, W, A, T, B, and +. The meanings of each character are as follows:

R (read): Read
W (write): Write
A (append): append
T (text): Text File, can be omitted without writing
B (banary): Binary File


Write a file in C language:

// Java file = new file ();-> C open a file
// Fos = fileoutputstrem (File)-> C. Get the output stream int Len = FOS. Write () of the file. // how long the data is written?

// FOS. Write ();-C Write File. Fos. Flush ();

# Include <stdio. h>
Main (){
// File * fopen (char * filename, char * type );
// Sharedpreference world_readable world_writeable append private

File * fp = fopen ("1.txt"," WT ");
// Int fwrite (void * PTR, int size, int nitems, file * stream );

// The returned value int indicates the number of successful writes.

// Void * The data to be written to the file by PTR can be char * int *
// Size the length of data written at a time.
// Nitems indicates the number of writes.
// Stream is written to that file.
Char * STR = "hello ";
Int result = fwrite (STR, sizeof (char), 5, FP );
Printf ("% d write times \ n", result );
// It must be called. If it is not called, the file will be occupied.
Fclose (FP );

System ("pause ");
}

C language:
# Include <stdio. h>
Main (){
// File * fopen (char * filename, char * type );
// Sharedpreference world_readable world_writeable append private

File * fp = fopen ("1.txt"," RT ");
// Int fread (void * PTR, int size, int nitems, file * stream );
// The returned value int indicates the number of times the size is actually read.
// * Memory space in which PTR data is read
// Size the length of data read at a time is multiple bytes
// How many times does nitmes read data?
// The file from which stream is read
Char * STR = (char *) malloc (sizeof (char) * 10); // allocate memory
Int READ = fread (STR, sizeof (char), 5, FP );

Printf ("% d times read \ n", read );


Printf ("str = % s \ n", STR );
Free (STR); // manually release the allocated memory
Fclose (FP); // close the stream
System ("pause ");
}

# Include <stdlib. h>
# Include <stdio. h>

Random Number function in C language:

To use the rand () function, you need to import the stdlib. h and stdio. h header files.

# Include <stdlib. h>
# Include <stdio. h>

Int getpressure (){
// The underlying C implementation code. The internal working principle may be very complicated and we don't care about it.

// Method name parameters and return values
Return rand () % 300; // 0 ~ 300

}

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.