sscanf的应用

1.提取字符串

2.提取指定长度的字符串

3.提取指定字符为止的字符串

4.取仅包含指定字符集的字符串

5.取到指定字符集为止的字符串


#include <stdio.h>
int main()
{
char str[512]={0};
sscanf("123456","%s",str);
printf("str=%s\n",str);
sscanf("123456","%4s",str);
printf("str=%s\n",str);
sscanf("123456 abcdef","%[^ ]",str);
printf("str=%s\n",str);
sscanf("123456abcdefBCDEF","%[1-9a-z]",str);
printf("str=%s\n",str);
sscanf("123456abcdefBCDEF","%[^A-Z]",str);
printf("str=%s\n",str);
return 0;
}

运行结果如下:

sscanf的应用,布布扣,bubuko.com

时间: 2024-08-08 10:11:05

sscanf的应用的相关文章

STL :sscanf sprintf的应用

PAT 1054 网上看到的,很好的应用了这两个函数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <bits/stdc++.h> using namespace std; int main() {     int n, cnt = 0;     char a[50], b[50];     double temp, sum = 0.0

C语言关于利用sscanf实现字符串相加减

#include<stdio.h>#include<string.h>void main(){ int a; int b; char str1[10] = "99999"; char str2[10] = "1111111"; char str[30]; int k = 0, i = 0, j = 0; for (k = 0; k < 30&&i<strlen(str1);){ str[k++] = str1[i+

sscanf的常见用法

例子: 1. 常见用法. char buf[512] = ; sscanf("123456 ", "%s", buf); printf("%s\n", buf); 结果为:123456 2. 取指定长度的字符串.如在下例中,取最大长度为4字节的字符串. sscanf("123456 ", "%4s", buf); printf("%s\n", buf); 结果为:1234 3. 取到指定

sscanf()函数。

#include <stdio.h> int main() { char str[10002]; char a[1002],b[1002]; while(gets(str)) { sscanf(str,"%s %s",a,b); printf("%s\n",a); printf("%s\n",b); } } #include <stdio.h> int main() { char str[10002]; char a[10

C语言函数sscanf()的用法 (转载

在我的学习过程中,从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( string str, string fmt, mixed var1, mixed var2 ... ); int scanf( const char *format [,argument]... ); 说明: sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)

读取文件信息,并通过sscanf从中获取所需数据

#include <stdio.h> #include <stdlib.h> #include <string.h> int file_length(char* fileName) { FILE* fp; int file_set_val,file_end_val; fp = fopen(fileName, "r"); if(fp == NULL) { printf("[%s][%s]read file fail\r\n",__L

C语言函数sscanf()的用法(转)

转自:http://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( string str, string fmt, mixed var1, mixed var2 ... ); int scanf( const char *format [,argument]... ); 说明: sscanf与scanf

sscanf函数

1 //用法举例 2 #include <stdio.h> 3 #include <string.h> 4 #define N 512 5 int main() 6 { 7     char buf[N], buf2[N], buf3[N]; 8     memset(buf,  0, sizeof(buf)); 9     memset(buf2, 0, sizeof(buf2));10     memset(buf3, 0, sizeof(buf3));//包含头文件strin

linux c sscanf函数

sscanf函数 是从一个字符串中读取与指定格式相符的数据 函数原型 int sscanf(const char *str, const char *format, ...); 参数说明 str:要读取的原字符串. format:可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '\t' | '\n' | 非%符号} 注: 1.* 亦可用于格式中, (即 %d 和 %*s) 加了星号 () 表示跳过此数据不读入. (也就是不把此数据读入

sscanf引发的Heap corruption detected error问题——有待完美解决

抛出问题 今天在写程序的时候遇到了一个以前没有暴露出来的问题,问题如题目所示.下面写了一个小的测试程序,如下: #include <stdio.h> #include <stdlib.h> int main(){ unsigned char *ua = (unsigned char*)malloc(sizeof(unsigned char) * 3); char line[40] = "177 100 88"; sscanf(line, "%hhu %