linux 系统编程-学习笔记1
来源:互联网 发布:以色列首都 知乎 编辑:程序博客网 时间:2024/05/19 13:08
1.main函数的调用
/*裸机代码(不带操作系统):
* 启动代码(硬件相关初始化+软件相关初始化)
* bl main 跳转到main函数
* {
* 执行程序;
* }
* loop 等待main返回
* loop
*
*带操作系统:exec ==>启动代码==>main
argc : 外部传人参数的个数
argv : 指针数组(存储指针的数组简称)
********************************************/
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("hello world\n");
for(i = 0; i < argc; i++)
{
printf("%s\n",argv[i]);
}
return 0;
}
/*裸机代码(不带操作系统):
* 启动代码(硬件相关初始化+软件相关初始化)
* bl main 跳转到main函数
* {
* 执行程序;
* }
* loop 等待main返回
* loop
*
*带操作系统:exec ==>启动代码==>main
argc : 外部传人参数的个数
argv : 指针数组(存储指针的数组简称)
********************************************/
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("hello world\n");
for(i = 0; i < argc; i++)
{
printf("%s\n",argv[i]);
}
return 0;
}
2.
常用3本man手册:
第一本:man 1 printf => 命令
第二本:man 2 open => 系统函数
第三本:man 3 printf => c库函数
void perror(const char *s);
char *strerror(int errnum);
当函数调用错误的时候,会返回一个错误号errno
===>调用函数perror
===>可以通过错误号和strerror函数匹配想相对应的错误信息
===>strerror(errno);返回errno所对应的错误信息
1 : Operation not permitted
2 : No such file or directory
3 : No such process
4 : Interrupted system call
5 : Input/output error
6 : No such device or address
7 : Argument list too long
8 : Exec format error
9 : Bad file descriptor
10 : No child processes
11 : Resource temporarily unavailable
12 : Cannot allocate memory
13 : Permission denied
14 : Bad address
15 : Block device required
16 : Device or resource busy
17 : File exists
18 : Invalid cross-device link
19 : No such device
2 : No such file or directory
3 : No such process
4 : Interrupted system call
5 : Input/output error
6 : No such device or address
7 : Argument list too long
8 : Exec format error
9 : Bad file descriptor
10 : No child processes
11 : Resource temporarily unavailable
12 : Cannot allocate memory
13 : Permission denied
14 : Bad address
15 : Block device required
16 : Device or resource busy
17 : File exists
18 : Invalid cross-device link
19 : No such device
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
FILE *fp = NULL;
fp = fopen("test","r");
if(NULL == fp)
{
printf("errno : %d\n",errno);
printf("====>%s\n",(char *)strerror(errno));
perror("fopen");//打印发生错误的原因
return -1;
}
return 0;
}
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
FILE *fp = NULL;
fp = fopen("test","r");
if(NULL == fp)
{
printf("errno : %d\n",errno);
printf("====>%s\n",(char *)strerror(errno));
perror("fopen");//打印发生错误的原因
return -1;
}
return 0;
}
_________________________________________________________________
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
___________________________________________________________________
c库函数:
源文件.c到可执行文件所需要经历的步骤:
预处理==》编译==》汇编==》连接
1.预处理:*.c ==> *.i
gcc -E -o file.i file.c
2.编译==> *.i ==> *.s 生成汇编代码
gcc -S -o file.s file.i
3.汇编==> *.s ==> *.o 生成目标代码
gcc -c -o file.o file.s
4.连接==> *.o ==> main 可执行文件
目标代码+启动文件==>可执行文件
gcc -o main *.o
标准I/O: 基于c库的函数,不依赖平台,可以移植性好
函数:fopen fprintf/fscanf fputs/fgets fread/fwrite
fgetc/fputc puts/gets getc/putc putchar/getchar fclose
sprintf,fseek,ftell,rewind,feof
注意:gets 函数不安全,一般禁止使用
对文件的操作:
打开 | 读 | 写 | 文件指针 | 关闭
-----------------------------------------------------------
fopen |scanf | printf | fseek | fclose
|fscanf | fprintf | ftell
|fgets | fputs | rewind
|fread | fwrite | feof
|fgetc | fputc |
|getc | putc |
|getchar | putchar |
-----------------------------------------------------------
mode :
r : 以只读方式打开,如果文件不存在,则返回错误
w : 以只写方式打开,如果文件存在,清空源文件;如果文件不存在,则创建
w+ : 以读写方式打开,如果文件存在,清空源文件;如果文件不存在,则创建
a : 追加
a+ :
void *memset(void *s, int c, size_t n);
向s指向的缓冲区中写n个字节的数据c
memset(s,0,sizeof(s));==>清零操作
向s指向的缓冲区中写sizeof(s)个字节的0
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
从流指针stream指向的文件中读取数据到ptr指向的缓冲区,
ptr : 存放读取到数据的缓冲区
size : 每块的大小
nmumb: 块数
stream: 流指针
一次读取的字节数:size * nmumb
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);
0 0
- linux 系统编程-学习笔记1
- 学习笔记-Linux系统编程
- Linux系统编程学习笔记(五)进程管理1
- linux 系统编程学习笔记一
- linux 系统编程学习笔记二
- linux 系统编程学习笔记三
- linux 系统编程学习笔记四
- Linux系统学习笔记:BASH编程
- Linux-Unix系统编程手册学习笔记
- Linux系统编程学习笔记-open函数
- Linux系统编程之基本概念--学习笔记
- Linux环境变量与系统编程学习笔记
- linux系统编程笔记1
- Linux编程学习笔记1
- Linux系统学习笔记(1)
- Linux系统编程学习笔记(七)内存管理
- Linux系统编程学习笔记之内存管理
- Linux系统编程学习笔记(十一)守护进程
- struts2的s:iterator嵌套遍历
- Java异常显示声明和不显示声明区别
- UVa 1401 - Remember the Word(Trie + DP)
- linux下文件和权限的详解
- Linux:C/Socket多路复用select(转)
- linux 系统编程-学习笔记1
- 乡土中国
- MySql常用命令收集(持续更新)
- linux中开启samba服务实现文件共享
- 微信密码忘了怎么办
- Building a Space Station
- 8086汇编言语错误的集合
- POJ 3158 Kickdown
- 【PHP分享】WNP本地开发环境搭建