#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void main() { char str[40]; scanf("%s", str); system(str); }运行结果:
D:\Study\Data_Structure\传智播客\文件重定向\Debug>1.exe<3.txt>2.txt其中3.txt文件中中内容为:tasklist & pause
'<'代表向程序1.exe中输入内容,'>'代表将程序的执行结果重定向到2.txt文件中。
void staticLib(); int staticAdd();源文件: 静态库.c 用于函数的实现
#include<windows.h> #include"静态库.h" void staticLib() { MessageBoxA(0, "静态库文件打印内容", "静态库", 0); } int staticAdd(int a, int b) { return a + b; }
#include<windows.h> _declspec(dllexport) void msg() { MessageBoxA(0, "动态库的调用", "动态库", 0); } _declspec(dllexport) int add(int a, int b) { return a + b; }打开 项目属性---- 常规---- 配置类型----改为 动态库.dll---- 生成解决方案,即可生成静态库文件 动态库.dll
#include<stdio.h> #include<stdlib.h> #include"静态库.h" //加不加该头文件都可以 #include<windows.h> #pragma comment(lib, "静态库.lib") //将静态库文件加载进来 //重定义动态库函数 typedef void(*pmsg)(); //简化函数指针 typedef int(*padd)(int a, int b); void main() { //静态库文件可以实现文件的私有,每次更新需要重新编译exe,浪费资源。 //静态库文件的使用 staticLib(); printf("result = %d\n", staticAdd(1, 2)); //动态库文件谁都可以用,只要外部接口一样,不用更新exe,更新dll即可,可以实现功能的更新 //节约计算机资源,需要使用的时候调用,否则释放,动态库可以劫持 //动态库的使用 HMODULE mydll = LoadLibraryA("动态库.dll"); //加载动态库,返回一个指针 if (!mydll) printf("动态库加载失败\n"); else { pmsg pmsg1; //定义一个函数指针 pmsg1 = (pmsg)GetProcAddress(mydll, "msg"); //获取函数地址 if (pmsg1) pmsg1(); //执行函数 padd padd1; //定义一个函数指针 padd1 = (padd)GetProcAddress(mydll, "add"); //获取函数地址 if (padd1) printf("result = %d\n", padd1(1, 3)); } //释放动态库 FreeLibrary(mydll); system("pause"); }
通过学习和使用个人认为,在字符串格式不是很复杂,但是也并不简单的时候用这个函数比较合适,这个尺度就要靠自己把握了,字符串不是很复杂,但自己写个处理的函数比较麻烦,效率也不高,就用这个函数,如果字符串很复杂,那就用正则表达式吧。
不多说了,看看下面这些介绍和列子吧!
函数原型: int sscanf( const char *buffer, const char *format [,argument ] ...);
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.
A format specification has the following form:支持集合操作:
%[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
%[aB'] 匹配a、B、'中一员,贪婪性
%[^a] 匹配非a的任意字符,贪婪性
例子:
char buf[512] = { 0 }; sscanf("123456 ", "%s", buf); printf("%s\n", buf);结果:123456
char buf[512] = { 0 }; sscanf("123456 ", "%4s", buf); printf("%s\n", buf);结果:1234
char buf[512] = { 0 }; sscanf("123456 abcdedf", "%[^b]", buf); printf("%s\n", buf);结果:123456 a
char buf[512] = { 0 }; sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf); printf("%s\n", buf);结果:123456abcdef; 遇到不是中括号中的字符就结束
char buf[512] = { 0 }; sscanf("ABC123456abcdedfBCDEF", "%[1-9a-z]", buf); printf("%s\n", buf);结果为空。
char buf[512] = { 0 }; sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf); printf("%s\n", buf);结果:123456abcdef
char buf[512] = { 0 }; sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf); printf("%s\n", buf);结果:12DWDFF
char buf[512] = { 0 }; sscanf("hello, world", "%*s%s", buf); printf("%s\n", buf);结果:world
char s1[20], s2[20]; char *s = "1try234delete5"; sscanf(s, "1%[^2]234%[^5]", s1, s2); printf("s1 = %s\n", s1); printf("s2 = %s\n", s2);
s1 = try s2 = delete 请按任意键继续. . .分析:scanf的format中出现的非转换字符(%之前或转换字符之后的字符),即此例中的1234用来跳过输入中的相应字符;
char test[] = "222,333,444,555,666,,777888"; char s1[4] = { 0 }, s2[4] = { 0 }, s3[4] = { 0 }, s4[4] = { 0 }, s5[4] = { 0 }, s6[4] = { 0 }, s7[4] = { 0 }; sscanf(test, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", s1, s2, s3, s4, s5, s6, s7); printf("sssa1=%s\n", s1); printf("sssa2=%s\n", s2); printf("sssa3=%s\n", s3); printf("sssa4=%s\n", s4); printf("sssa5=%s\n", s5); printf("sssa6=%s\n", s6); printf("sssa7=%s\n", s7);结果:
sssa1=222 sssa2=333 sssa3=444 sssa4=555 sssa5=666 sssa6= sssa7= 请按任意键继续. . .
char a[20] = { 0 }; char b[20] = { 0 }; //假设email地址信息以';'结束 sscanf("email:[email protected];", "%*[^:]:%[^;]", a); //假设email地址信息没有特定的结束标志 sscanf("email:[email protected]", "%*[^:]:%s", b); printf("%s\n", a); printf("%s\n", b);
[email protected] [email protected] 请按任意键继续. . .分析:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void main1() { printf("main1\n"); char buf[512] = { 0 }; sscanf("123456 ", "%s", buf); printf("%s\n\n", buf); } void main2() { printf("main2\n"); char buf[512] = { 0 }; sscanf("123456 ", "%4s", buf); printf("%s\n\n", buf); } void main3() { printf("main3\n"); char buf[512] = { 0 }; sscanf("123456 abcdedf", "%[^b]", buf); printf("%s\n\n", buf); } void main4() { printf("main4\n"); char buf[512] = { 0 }; sscanf("ABC123456abcdedfBCDEF", "%[1-9a-z]", buf); printf("%s\n\n", buf); } void main5() { printf("main5\n"); char buf[512] = { 0 }; sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf); printf("%s\n\n", buf); } void main6() { printf("main6\n"); char buf[512] = { 0 }; sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf); printf("%s\n\n", buf); } void main7() { printf("main7\n"); char buf[512] = { 0 }; sscanf("hello, world", "%*s%s", buf); printf("%s\n\n", buf); } void main8() { printf("main8\n"); char s1[20], s2[20]; char *s = "1try234delete5"; sscanf(s, "1%[^2]234%[^5]", s1, s2); printf("s1 = %s\n", s1); printf("s2 = %s\n\n", s2); } void main9() { printf("main9\n"); char test[] = "222,333,444,555,666,,777888"; char s1[4] = { 0 }, s2[4] = { 0 }, s3[4] = { 0 }, s4[4] = { 0 }, s5[4] = { 0 }, s6[4] = { 0 }, s7[4] = { 0 }; sscanf(test, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", s1, s2, s3, s4, s5, s6, s7); printf("sssa1=%s\n", s1); printf("sssa2=%s\n", s2); printf("sssa3=%s\n", s3); printf("sssa4=%s\n", s4); printf("sssa5=%s\n", s5); printf("sssa6=%s\n", s6); printf("sssa7=%s\n\n", s7); } void main10() { printf("main10\n"); char a[20] = { 0 }; char b[20] = { 0 }; //假设email地址信息以';'结束 sscanf("email:[email protected];", "%*[^:]:%[^;]", a); //假设email地址信息没有特定的结束标志 sscanf("email:[email protected]", "%*[^:]:%s", b); printf("%s\n", a); printf("%s\n\n", b); } void main() { main1(); main2(); main3(); main4(); main5(); main6(); main7(); main8(); main9(); main10(); system("pause"); }
main1 123456 main2 1234 main3 123456 a main4 main5 123456abcdedf main6 12DDWDFF main7 world main8 s1 = try s2 = delete main9 sssa1=222 sssa2=333 sssa3=444 sssa4=555 sssa5=666 sssa6= sssa7= main10 [email protected] [email protected] 请按任意键继续. . .