//main.c
#include <stdio.h>
int main(int argc,char **argv)
{
printf("%s(%d)-%sthis is in main\n", __FILE__ ,__LINE__ ,__FUNCTION__);
return 0;
}
//global.h
#ifndef GLOBAL_H
#define GLOBAL_H
#define DEBUGFMT "%s(%d)-%s: "
#define DEBUGARGS __FILE__ ,__LINE__ ,__FUNCTION__
#endif
//此头文件用于辅助调试程序,运行时打出程序运行结果的所在程序行。我将此头文件存于/usr/include/目录下。
//main.c
#include <global.h>
#include <stdio.h>
int main(int argc,char **argv)
{
printf(DEBUGFMT "this is in main\n",DEBUGARGS);
return 0;
}
#gcc -Wall main.c -o main
#./main
main.c(5)-main:this is in main
//hello.c
#include <stdio.h>
int main(int argc,char **argv)
{
printf("hello""Linux"",%s""\n",argv[0]);
return 0;
}
#gcc -Wall hello.c -o hello
#./hello
hello Linux, ./hello
#!gcc //运行前一次运行的gcc命令。
//hello.c
#include <stdio.h>
int main(int argc,char **argv)
{
printf("hello"\
"Linux"\
",%s"\
"\n",argv[0]);
return 0;
}
GDB调试程序,指针,函数调用时的参数传递
//getNextWord.c
#include <global.h>
#include <stdio.h>
char * GetNextWord(char *s)
{
char *d = 0;
if (!s)
return 0;
d = s;
while (*d !=' ' && *d !='\t')
//判断是否为空格键和Tab键 。
d++;
d++;
while (*d == ' ' && *d == '\t')
d++;
return d;
}
//s="hello world" d="world"
//用于查找下一个单词。
//funca.c
#include <global.h>
#include <stdio.h>
int funca(void)
{
char sa[] = "Hello, funca";
char *da = 0;
da = GetNextWord(sa);
printf(DEBUGFMT "sa=%s da=%s\n",DEBUGARGS, sa, da);
return 0;
}
//funcb.c
#include <global.h>
#include <stdio.h>
int funcb(void)
{
char *sb = 0;
char *db = 0;
sb = (char *)malloc(12);//分配内存
bzero(sb,12);//内存置零
memcpy(sb,"Fine, funcb", 11);//向内存填入字符串
db = GetNextWord(sb);
printf(DEBUGFMT "sb=%s db=%s\n", DEBUGARGS, sb, db);
return 0;
}
//main.c
#include <global.h>
#include <stdio.h>
int main(int argc,char **argv)
{
funca();
funcb();
printf(DEBUGFMT "this is in main\n",DEBUGARGS);
return 0;
}
#gcc -g -Wall main.c funcb.c funca.c getNextWord.c
//-g参数是为了下面GDB调试,做准备。
//按Alt+句号键,按一次得到上一个命令的最后一个参数,按两次得到上上一次命令的最后一个参数,......
#gdb ./a.out //对a.out进行GDB调试
(gdb)list
//显示最近的十行源代码,再list显示再后面的十行源代码。
(gdb)list funca
//显示funca.c的源代码。如果源代码多,则需要多list几次。
//程序运行过程中,要查看当前运行位置周围的代码,用list。
(gdb)b
//break(断点)的简写。
(gdb)b funca
//在funca设置一个断点
(gdb)b GetNextWord.c:6
//在GetNextWord.c的第6行设置一个断点。
(gdb)r
//run的简写,让程序运行起来的命令。
(gdb)list
//查看运行所到位置的周围的源代码。
(gdb)n
//next:下一步的简写。
(gdb)print sa
//查看sa的内容
$1 = "Hello, funca"
(gdb)print &sa
//查看sa的地址
$2 = (char (*)[13])0xbf85c797
//0xbf85c797是H的地址。
//0xbf85c798是e的位置。
(gdb)print sa+7
$3 = 0xbf85c79e "funca"
//即:f的位置。
//argc.c
#include <global.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
printf
(DEBUGFMT
"argc=%d &argc=%08X main=%08X\nargv[0]=%s argv[1]=%s argv[2]=%s\n",
DEBUGARGS, argc, &argc, main, arhv[0], argv[1], argv[2]);
//打出argc的值,argc的地址,main函数的地址,打出程序的第一个参数,第二个参数,第三个参数。
if (argv[3])
//如果第四个参数存在。
printf(DEBUGFMT "at least 3 parameters\n", DEBUGARGS);
else
printf(DEBUGFMT "less than 3\n", DEBUGARGS);
if (argc > 3)
//
printf(DEBUGFMT "at least 3 parameters\n", DEBUGARGS);
else
printf(DEBUGFMT "less than 3\n", DEBUGARGS);
return 0;
}
#gcc -g -Wall argc.c
#gdb ./a.out
(gdb)print argc
$1 = 1
(gdb)print sizeof(argv)
@2 = 4
(gdb)q
//退出
//readfile.c
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int fd = -1;
char teststr[1024 * 4 + 1] = "";
int ret = -1;
if (argc < 2);
{
printf(“Usage: %s filename\r\n",argv[0]);
return 1;
}
fd = open(argv[1],0);//打开文件。
if (fd == -1)
{
printf("open file '%s' error. Info:%s\r\n",argv[1],strerror(errno));
//若fd=-1,则说明打开文件失败,并且打出失败的原因。
return 2;
}
printf("show content of '%s' as following:\r\n",argv[1]);
while (1)
{
if((ret = read(fd,teststr,sizeof(teststr)-1))<= 0)
{
if(ret)
printf("read error!!\r\n");
else
printf("end of file reached\r\n");
break;
}
printf("%s",teststr);
bzero(teststr, sizeof(teststr));
//内容置空。
}
if (fd > -1)
close(fd);
return 0;
}
//system.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
int ret;
ret = system(argv[1]);
//执行system的内容的命令。
printf("ret=%d\n",ret);
if (WEXITSTATUS(ret) == 1)
//WEXITSTATUS确保是system的内容的执行命令的返回值 。
printf("parameter error\n");
else if (WEXITSTATUS(ret) == 2)
printf("filename error\n");
else
printf("OK! return value is:%d\n",WEXITSTATUS(ret));
return 0;
}
//popen.c
//本函数块:执行一个命令,并且取得命令执行的输出内容。
#include <stdio.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
FILE *fp = 0;
int ret = 0;
fp = popen(argv[1],argv[2]);
if ((argv[2][0] == 'r') || (argv[2][0] == 'R'))
{
printf("----output from the command '%s' is:----\n",argv[1]);
while ((ret = getc(fp)) != E0F)
printf("%c",ret);
printf("-----------------------\n");
}
if ((argv[2][0] == 'w') || (argv[2][0] == 'W'))
{
fprintf(fp, "mary 20\r\n");
}
if (fp)
ret = pclose(fp);
if (WEXITSTATUS(ret) == 1)
printf("parameter error\n");
else if (WEXITSTATUS(ret) == 2)
printf("filename error\n");
else
printf("OK! return value is:%d\n",WEXITSTATUS(ret));
return 0;
}