文章目录
  1. 1. fopen
  2. 2. atan与atan2
  3. 3. fabs
  4. 4. fread

main函数传参数

int main(int argc,char *argv[])

  • 参数argc:表示参数的个数

    argc记录了用户在运行程序的命令行中输入的参数的个数(包括程序本身)

  • 参数*argv:表示输入字符的数组

    arg[]指向的数组中至少有一个字符指针,即arg[0].他通常指向程序中的可执行文件的文件名。

    在有些版本的编译器中还包括程序文件所在的路径。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
int main(int argc,char *argv[])
{
if (argc==5)
{
std::cout<< argc<<std::endl;
for(int i=0;i<sizeof(argv);i++)
std::cout<<argv[i]<<" ";
std::cout<<std::endl;
}
return 0;
}

【OutPut】

1
2
3
[root@localhost test]# ./now 1 2 3 4           
5
./now 1 2 3

附一段 K&R C 原话

In environments that support C, there is a way to pass command-line arguments or parameters to a program when it begins executing. When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line (注意所在位置,是WIN命令提示符中,或LINUX命令行中)arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string. We customarily use multiple levels of pointers to manipulate these character strings.


ioctl函数
一、 什么是ioctl
ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。它的调用个数如下:
int ioctl(int fd, ind cmd, …);
其中fd是用户程序打开设备时使用open函数返回的文件标示符,cmd是用户程序对设备的控制命令,至于后面的省略号,那是一些补充参数,一般最多一个,这个参数的有无和cmd的意义相关。
ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。

1
int ioctl(int fd,int request,int cmd);
  • fd  :函数Open

IOCTL(2) Linux Programmer’s Manual IOCTL(2)

NAME
ioctl - control device

SYNOPSIS
#include <sys/ioctl.h>

   int ioctl(int d, int request, ...);

DESCRIPTION
The ioctl() function manipulates the underlying device parameters of special files. In particular, many operating characteristics of character
special files (e.g., terminals) may be controlled with ioctl() requests. The argument d must be an open file descriptor.

   The second argument is a device-dependent request code.  The third argument is an untyped pointer to memory.  It's traditionally char  *argp  (from
   the days before void * was valid C), and will be so named for this discussion.

   An  ioctl() request has encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp in bytes.  Macros
   and defines used in specifying an ioctl() request are located in the file <sys/ioctl.h>.

RETURN VALUE
Usually, on success zero is returned. A few ioctl() requests use the return value as an output parameter and return a nonnegative value on suc‐
cess. On error, -1 is returned, and errno is set appropriately.

ERRORS
EBADF d is not a valid descriptor.

   EFAULT argp references an inaccessible memory area.

   EINVAL Request or argp is not valid.

   ENOTTY d is not associated with a character special device.

   ENOTTY The specified request does not apply to the kind of object that the descriptor d references.
 

其它:
Q1:是不是要有open函数操作,itoctl才能成功操作成功呢?

结论:必须要有Open函数才能执行itoctl,命令才能执行成功。

测试代码1:无open函数

1
2
3
4
5
6
7
8
9
10
#include <sys/ioctl.h>	
#include<stdio.h>

int main(int argc,char *argv[])
{
int fd;
char *leds = "/dev/leds";
ioctl(fd,atoi(argv[1]),atoi(argv[2]));
printf("ioctl %s success\n",leds);
}

结果:指令无效

测试代码2:有Open函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sys/ioctl.h>	
#include<stdio.h>

int main(int argc,char *argv[])
{
int fd;
char *leds = "/dev/leds";

if((fd = open(leds, O_RDWR|O_NOCTTY|O_NDELAY))<0)
printf("open %s failed\n",leds);


ioctl(fd,atoi(argv[1]),atoi(argv[2]));
printf("ioctl %s success\n",leds);
}

atoi

头文件:#include <stdlib.h>

atoi() 函数用来将字符串转换成整数(int),其原型为:
int atoi (const char * str);

【函数说明】atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束符(‘\0’)才结束转换,并将结果返回。

【返回值】返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0。

温馨提示:ANSI C 规范定义了 stof()、atoi()、atol()、strtod()、strtol()、strtoul() 共6个可以将字符串转换为数字的函数,大家可以对比学习。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。


EOF

EOF是指文件的结束符,是一个宏定义.  
对于键盘输入来说,getchar()只有在遇到文本结束标记(ASCII编码为26)时才会返回EOF,其它情况都会返回一个输入符号值。所以对于这种程序,要想让循环信息运行,只能输入这个文本结束标记。输入这个标记有多种方法,常用的就是按F6键,或者按Ctrl-Z组合键,或者打开数字小键盘之后按住Alt键再依次按小键盘上的26两个数字键最后放开Alt键,等,都可以输入这个文本结束标记.


scanf

scanf函数会忽略空白字符(white-space character,包括空格符、水平和垂直制表符、换页符和换行符)。

  1. 如果某一项不能成功读入,那么scanf函数将不能查看格式串剩余的部分(或余下的输入数据)而立即返回。
    1
    2
    3
    4
    5
    6
    7
    #include<stdio.h>
    int main(void)
    {
    int a, b;
    scanf("%d /%d", &a,&b);
    return 0;
    }
    若程序输入:
    1
    ?4¤

    ¤   :回车

变量观察表
scanf没有将4读入变量b中。

  1. 当scanf函数遇到一个不可能属于当前项的字符时,它会“放回原处”,以便在扫描下一个输入向或下一次调用scanf函数时再次读入。
    1
    2
    3
    4
    5
    6
    7
    8
    #include<stdio.h>
    int main(void)
    {
    int i, j;
    float x, y;
    scanf("%d%d%f%f", &i, &j, &x, &y);
    return 0;
    }

输入:

1
1-20.3-4.0e3¤

¤ 回车符号

变量监视表

  1. scanf,

fopen、atan

fopen

FILE *fopen(const char * restrict filename,const char * restrict mode);

fopen函数的第一个参数是含有要打开文件名的字符串。*(“文件名”可能包含关于文件位置的信息,如驱动器符或路径)*

第2个参数是“模式字符串”,它用来指定打算对文件执行的操作。例如,字符串r表明只读取数据。

注意fopen函数原型中,restrict关键字出现了2次。restrict表明filename和mode所执行的字符串的内存单元不共享。
fopen函数返回一个文件指针。

程序可以把此指针存储在一个变量中,稍后需要对文件进行操作时使用它。

用于文本模式字符串

字符串 含义
r 只读
w 写(文件不需要存在)
a 打开文件用于追加(文件不需要存在)
r+ 打开文件用于读和写(从文件头开始)
w+ 打开文件用于读和写(如果文件存在就截去)
a+ 打算开文件用于读和写(如果文件存在就追加)

atan与atan2

C语言的math.h或C++的cmath有2个求正反切的函数atan(double x)与atan2(double y,double x)它们的返回值是弧度要专为角度再自己处理一下。

atan:接受的是一个正切值(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。

相比较ATan,ATan2究竟有什么不同

对于tan(θ) = y / x:

θ = ATan(y / x)求出的θ取值范围是[-PI/2, PI/2]。
atan:接受的是**一个正切值**(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。

θ = ATan2(y, x)求出的θ取值范围是[-PI, PI]。
atan2:第二个atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了

例如:

1
2
3
4
5
6
7
8
#include<stdio.h>
#include<math.h>
int main(void)
{
printf("atan=%f\n", atan(1.0)*180/3.14);
printf("atan2=%f\n\n", atan2(15, 15) * 180 / 3.14);
return 0;
}

其运行结果:

1
2
atan=45.022825
atan2=45.022825

各个象限θ范围

  1. 当 (x, y) 在第一象限, 0 < θ < PI/2.

  2. 当 (x, y) 在第二象限 PI/2 < θ≤PI.

  3. 当 (x, y) 在第三象限, -PI < θ < -PI/2.

  4. 当 (x, y) 在第四象限, -PI/2 < θ < 0.

当点(x, y)在象限的边界也就是坐标轴上时:

  1. 当 y 是 0,x 为非负值, θ = 0.

  2. 当 y 是 0, x 是 负值, θ = PI.

  3. 当 y 是 正值, x 是 0, θ = PI/2.

  4. 当 y 是 负值, x 是 0, θ = -PI/2.

fabs

求绝对值函数,fabs(x),求出x的绝对值,和数学上的概念相同

调用该函数需要#include<math.h>
实例:

1
2
3
4
5
6
7
#include<stdio.h>
int main(void)
{
printf("fabs(3.5)\t=%f\n",fabs(3.5));
printf("fabs(-3.5)\t=%f\n",fabs(-3.5));
return 0;
}

运行结果:

1
2
fabs(3.5)       =3.500000
fabs(-3.5) =3.500000

fread

fread

函数原型:

size_t fread(void buff,size_t size,size_t count,FILE stream)**

作用:从文件中读入数据到指定的地址中

参数:

第一个参数为接收数据的指针(buff),也即数据存储的地址

第二个参数为单个元素的大小,即由指针写入地址的数据大小,注意单位是字节

第三个参数为元素个数,即要读取的数据大小为size的元素个素

第四个参数为提供数据的文件指针,该指针指向文件内部数据

返回值:读取的总数据元素个数

例:.

1
2
3
4
5
int  num,count;

int* pr=new int[num*count];

fread(pr, num*4, count, stream); // stream为fopen中返回的FILE指针

要将数据写入pr中,必须为pr分配内存,一个int为4个字节,所以要x4

补充:
size_t 类型

size _t 为了增强程序的可移植性,便有了size_t ,不同系统上,定义size_t可能不一样。

经测试发现,在32位系统中size_t是4字节的,在64位系统中,size_t是8字节的,这样利用该类型可以增加程序移植性。

size_t一般用来表示一种计数,比如有多少东西被拷贝等。例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。 它的意义大致是“适于计量内存中可容纳的数据项目个数的无符号整数类型”。所以,它在数组下标和内存管理函数之类的地方广泛使用。

文章目录
  1. 1. fopen
  2. 2. atan与atan2
  3. 3. fabs
  4. 4. fread