Hefei Zhongke Shengu Embedded Project Practice - Smart Home System Based on ARM Voice Recognition (2)

Table of contents

Smart home system based on ARM voice recognition

Exercise 1

1. Program compilation

Exercise 2:

2. File IO

3. Commonly used API interface functions for file IO

1. Open the file open()

2. Write the data content to the file write()

3. Close (save) the file

4. Programming examples

Summarize


Smart home system based on ARM voice recognition

We talked about some of the qualities of the Linux system in the previous article. Today, we are going to compile the program under the Linux system. We will familiarize ourselves with it through two small exercises.

Exercise 1

Be familiar with the use of basic commands:
    1. Create a new directory file demo/ in the user desktop directory.
    2. Create a new ordinary file hello.c in the directory demo/.
    3. The permissions to modify the file hello.c are:
        Readable, writable and executable for users rwx 7
        For the same Group users can write and execute -wx 3
        Readable by other users r-- 4
    4. Copy the directory demo/ to the root directory.

1. Program compilation

We write a program to output hello world!!, I believe everyone here can write the program.

//#include <stdio.h>   //宏:包含导入c语言标准输入输出头文件

int main()   //程序入口
{
	printf("hello world!!\n");   //在终端上打印 hello world!! 字符串 
	
	return 0;
}

gcc: 编译器、编译c语言的工具。

编译: gcc hello.c    编译得到一个可执行文件 a.out
执行: ./可执行文件   ./a.out
gcc hello.c -o bin
-o:将可执行文件重命名为bin,方便使用,防止文件覆盖。

 In our Linux system, we can get an .out file by running gcc hello.c. If there is an error in the program, it will prompt where the error is. The following is an introduction to the GCC compiler: 

GCC (GNU Compiler Collection) is a free open source compiler that supports multiple programming languages, such as C, C++, Objective-C, Fortran, Ada, etc. It is the cornerstone of many operating systems and software, including GNU/Linux, Mac OS X, FreeBSD, and others. Due to GCC's widespread use and large community, it has become one of the most popular and widely used compilers in the industry.

GCC (GNU Compiler Collection) is a free open source compiler that supports multiple programming languages, such as C, C++, Objective-C, Fortran, Ada, etc. It is the cornerstone of many operating systems and software, including GNU/Linux, Mac OS X, FreeBSD, and others. Due to GCC's widespread use and large community, it has become one of the most popular and widely used compilers in the industry.

 

 

Exercise 2:

    Use notpad++ to write a C program to print your name + school + major. In the Ubuntu shared directory, compile and run.
    Extension: Print 99 multiplication tables (loop).

The programs we have here are relatively simple, so I will show the code directly. The main purpose here is to practice how to compile the program with GCC and become familiar with this process.

#include <stdio.h> 

int main()  
{
	int i, j;
	for(i = 1; i<=9; i++)
	{
		for(j = 1; j<=i; j++)
		{
			printf("%dx%d=%d ", i, j, i*j);
		}
		printf("\n");
	}
	
	return 0;
}

If the results you get are exactly the same as mine, it means that you have mastered this method. Next, we introduce the file.

2. File IO

What is a file?
    A file is a collection of data.
    For example: .txt file .bmp file .jpg file,,,,


Unix/Linux management of data files (*.mp3, *.bmp), program files (*.c, *.h, *.o), device files (LCD, touch screen, mouse), network files (socket), etc. They are all abstracted into files and managed in a unified way.

In Linux system, everything is a file!!!

Control an LCD screen device----》Control the device file corresponding to the LCD device---》File IO

3. Commonly used API interface functions for file IO

Open the file----》Read/write the file-----》Close (save) the file

1. Open the file open()

头文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

函数原型:
    int open(const char *pathname, int flags);
参数列表:
    const char *pathname:需要打开的文件路径的字符串  "./1.txt"
     int flags: 打开文件的方式
         O_RDONLY 以只读方式打开文件
         O_WRONLY 以只写方式打开文件
         O_RDWR   以可读写方式打开文件
返回值:
        失败: -1,错误码errno被设置。
        成功:返回一个新的文件描述符(文件的唯一标识符,遥控器)
        我们就可以通过这个文件描述符操纵这个文件。

2. Write the data content to the file write()

头文件:
	#include <unistd.h>

函数原型:
    ssize_t write(int fd, const void *buf, size_t count);
从buf写入数据的内存缓冲区中  取count个字节数据  写入fd指代的文件中
参数列表:
    int fd:需要写入数据的目标文件的文件描述符,就是open函数的返回值。
    const void *buf:临时存放待写入的数据的内存缓冲区
    size_t count:写入的数据字节大小
    
返回值:
        失败: -1,错误码errno被设置。
        成功:返回实际写入的字节个数。

3. Close (save) the file

头文件:
	#include <unistd.h>
函数原型:
    int close(int fd);
参数列表:
	int fd: 需要关闭的目标文件的文件描述符。
返回值:
	成功:返回0;
	失败:返回-1,错误码errno被设置。

4. Programming examples

#include <stdio.h> 
#include <sys/types.h>  //open()
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>    //write()

int main()  
{
	//1、打开文件 1.txt
	int fd = open("./1.txt", O_RDWR);
	if(fd == -1)
	{
		//printf("open file failed!");
		perror("open file failed!");  //通过错误码打印错误信息
		return -1;   //程序非正常结束
	}
	
	//2、向文件中写入数据
	char buf[50] = "好好学习,天天向上!";
	int w_size = write(fd, buf, 50);
	printf("返回实际写入的字节个数: %d\n", w_size);
	
	//3、保存退出
	close(fd);

	return 0;
}
  • //1、打开文件 1.txt: This is the part that opens the file. It tries to open the "1.txt" file in the current directory, and returns -1 if the file does not exist.
  • //通过错误码打印错误信息: This is the error handling code. If an error occurs while opening the file, it prints an error message and returns -1.
  • //2、向文件中写入数据: This is the part that writes data to the file. It creates a character array buf and writes "Study hard and make progress every day!" "This sentence. Then use the write() function to write the contents of buf to the file.
  • //返回实际写入的字节个数: %d: This is the part that prints the actual number of bytes written to the file.
  • //3、保存退出: This is the part that closes the file. After writing the data, use the close() function to close the file.

Here we have opened the file, written to the file, and finally closed the file. Today's program is not difficult, the main thing is to familiarize yourself with the operation.

Summarize

Today, we are mainly familiar with using GCC to compile programs and related operations on files. In the next article, we will introduce how to display pictures on the LED screen.

Guess you like

Origin blog.csdn.net/BROKEN__Y/article/details/134406124