关于sscanf_s的字符串分解用法

hanyekanxue2010 2010-12-14 02:06:46
读取如下的txt文件:
课程代号 课程名称 先修课程代号
11049 高等代数C 44444
25003 计算机高级语言
22222 计算机导论
33333 计算机结构与组成 25003,22222
我用ReadString读取整行后,想用sscanf_s将字符串分解,
char t_num[20]="";
char t_name[20]="";
char t_class_pre1[20]="";
char t_class_pre2[20]="";
mfile.ReadString(M_temp);
sscanf_s(M_temp, "%s %s %s", t_num,t_name,t_class_pre1);
可是怎么搞都有问题,哪位高手能指点下啊
...全文
951 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2010-12-14
  • 打赏
  • 举报
回复
发这么多,什么意思啊
龙哥依旧 2010-12-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
char tokenstring[] = "33333 计算机结构与组成 25003,22222";
int t_num;
char t_name[20];
int t_class_pre1 = 0;
int t_class_pre2 = 0;

sscanf_s( tokenstring, "%d %s %d,%d", &t_num, t_name, _countof(t_name), &t_class_pre1, &t_class_pre2 );

printf_s( "t_num = %d\n", t_num );
printf_s( "t_name = %s\n", t_name );
printf_s( "t_class_pre1 = %d\n", t_class_pre1 );
printf_s( "t_class_pre2 = %d\n", t_class_pre2 );
}
bdmh 2010-12-14
  • 打赏
  • 举报
回复

char * ch = "11049 高等代数C 44444";
int a=0;
int b=0;
char* p = new char[50];;
sscanf(ch,"%d %s %d",&a,p,&b);
printf("%d",a);
printf("%s",p);
printf("%d",b);
delete[] p;
龙哥依旧 2010-12-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
char tokenstring[] = "33333 计算机结构与组成 25003 22222";
char t_num[20];
char t_name[20];
char t_class_pre1[20];
char t_class_pre2[20];

sscanf_s( tokenstring, "%s %s %s %s",
t_num, _countof(t_num), t_name, _countof(t_name), t_class_pre1, _countof(t_class_pre1),
t_class_pre2, _countof(t_class_pre2) );

printf_s( "t_num = %s\n", t_num );
printf_s( "t_name = %s\n", t_name );
printf_s( "t_class_pre1 = %s\n", t_class_pre1 );
printf_s( "t_class_pre2 = %s\n", t_class_pre2 );
}
libinfei8848 2010-12-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 daliancat1984 的回复:]

你可以用stringstream试试~~这个很好用的~
只要按行读出文件,将他们输入流,在输出到你想保存的字符串里面就可以。
默认的分隔符是空格
[/Quote]

是蛮好用,但是感觉效率有点低
龙哥依旧 2010-12-14
  • 打赏
  • 举报
回复
// crt_sscanf_s.c
// This program uses sscanf_s to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
char tokenstring[] = "15 12 14...";
char s[81];
char c;
int i;
float fp;

// Input various data from tokenstring:
// max 80 character string plus NULL terminator
sscanf_s( tokenstring, "%s", s, _countof(s) );
sscanf_s( tokenstring, "%c", &c, sizeof(char) );
sscanf_s( tokenstring, "%d", &i );
sscanf_s( tokenstring, "%f", &fp );

// Output the data read
printf_s( "String = %s\n", s );
printf_s( "Character = %c\n", c );
printf_s( "Integer: = %d\n", i );
printf_s( "Real: = %f\n", fp );
}
daliancat1984 2010-12-14
  • 打赏
  • 举报
回复
你可以用stringstream试试~~这个很好用的~
只要按行读出文件,将他们输入流,在输出到你想保存的字符串里面就可以。
默认的分隔符是空格
ouyh12345 2010-12-14
  • 打赏
  • 举报
回复
格式不是很统一啊
整形和字符串还是要分开的

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧