由买买提看人间百态

topics

全部话题 - 话题: sscanf
1 (共1页)
v*********o
发帖数: 5
1
来自主题: Programming版 - 问sscanf
在C程序的头文件中,假如有结构体定义如下:
typedef struct FrameworkComponent
{
char Name[256];
char NameIons[256];
} FRAMEWORK_COMPONENT;
extern FRAMEWORK_COMPONENT FrameworkComponent[2];
而在主函数中有:
char arguments[256];
sscanf(arguments,"%s",(char*)&FrameworkComponent[0].Name);
sscanf里面 (char*)&是什么意思?
Name作为字符数组名,本身已经代表地址了,为什么前面还有个取地址符号?
外面那个(char*)是不是强制类型转换?
好像不需要加啊?
H**********5
发帖数: 2012
2
txt文档三行数据,想读到结构体中
yingduSB, 61 , 65, 43, 65, 54, 43, 65
sanjie.haojian, 91, 90, 80, 100, 89, 99, 88
sange.haoer, 98, 92, 80, 100, 89, 99, 88
while (NULL!=fgets(line,60,inputFile))
{
sscanf(line,"%20[^,],%[0-9^,],%[0-9^,],%[1-9^,],%[1-9^,],%[1-9^,
][^/n]",&stu[nIndex].name,
&stu[nIndex].quiz1,
&stu[nIndex].quiz2,
&stu[nIndex].quiz3,
&stu[nIndex].quiz4,
&stu[nIndex].midi);
//&... 阅读全帖
i*****f
发帖数: 578
3
来自主题: Programming版 - sscanf problem in MSVC 7
Hi, I'm having trouble with the following codes;
=======================================
sscanf(s, "%d %s %s %lf %lf %lf %d",
&(intsec[i].ref),
intsec[i].name,
intsec[i].state,
&(intsec[i].dis),
&(intsec[i].logi),
&(intsec[i].latt),
&(intsec[i].pop) );
======================================
When the string s is like "342 omak WA 0.8 119.5141 ...."
The 4th value intsec[i].dis will scan in 0.799999999...
h**o
发帖数: 548
4
来自主题: Programming版 - how to sscanf this case.
I want to have a input string parsed by sscanf (or other function which
can work) in this way:
if the first part of the string is less than 10bytes, parse it and
assign it to a variable;
if the first part of the string is more than 10bytes, truncate it and
assign it to a variable.
Her is my expected result:
suppose input[] = "ThisStringFieldShouldBeTruncated 123
OtherStringFieldWhichAlsoShouldBeTruncated 456";
I want to parse it to
char v1[11]="ThisString"; int b1= 123, char v2[5] = "Othe", int
h**o
发帖数: 548
5
来自主题: Unix版 - how to sscanf this case.
I want to have a input string parsed by sscanf (or other function which
can work) using solaris C language in this way:
if the first part of the string is less than 10bytes, parse it and
assign it to a variable;
if the first part of the string is more than 10bytes, truncate it and
assign it to a variable.
Her is my expected result:
suppose input[] = "ThisStringFieldShouldBeTruncated 123
OtherStringFieldWhichAlsoShouldBeTruncated 456";
I want to parse it to
char v1[11]="ThisString"; int b1= 123,
r*********r
发帖数: 3195
6
来自主题: Programming版 - c++ 中如何把str转换为float?
这些function的适用性和性能成反比。
atof 只能用在 float/double。
sscanf 只能用在 integer, float.
lexical_cast 则可以使用在任何有operator>>定义的类型。
从性能上看,我猜的是 sscanf 比 lexical_cast 快5到10倍。
atof 又比 sscanf 快5倍左右。
还有,sscanf 是最容易用错的一个函数,对初学者简直是噩梦。
r*********r
发帖数: 3195
7
来自主题: Programming版 - c++ 中如何把str转换为float?
the signature of sscanf is:
int sscanf(const char *, const char *,...)
the arguments are supposed to be passed by their address,
but most beginners often forget to use the "&" operator.
the compiler won't complain since there's no type checking at all.
running the executable will cause a segmentation fault.
another funny fact is that if you switch the string to be scanned
and the format string, it still runs! like the following:
float x = 4.3;
...
sscanf("%f", "12.34", &x);
the x will still be 4
c*****e
发帖数: 737
8
来自主题: JobHunting版 - 问一个facebook的电面
正常的程序员:
int n1, n2;
sscanf (str1,"%d",&n1);
sscanf (str2,"%d",&n2);
sprintf (buffer, "%d", n1 * n2);
c*****e
发帖数: 737
9
来自主题: JobHunting版 - 问一个facebook的电面
正常的程序员:
int n1, n2;
sscanf (str1,"%d",&n1);
sscanf (str2,"%d",&n2);
sprintf (buffer, "%d", n1 * n2);
I**********s
发帖数: 441
10
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
I**********s
发帖数: 441
11
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
p***s
发帖数: 78
12
来自主题: Programming版 - 问个C/C++题目
需要读文件, 文件形式
abc dada 4 6 8 10 30 ...
dfa dfas 5 7 23 123 45 ...
...
每行前两个为字符串, 后面为数字, 要读进相应变量里.
我的方法, 先用getline读进一行, 再用sscanf读一行内的每个内容.
但现在有问题:
后面的数字很多, 用sscanf 必须 用同样多个 %d, 不太现实
更不现实的是后面的数字个数是不确定的, 没法用%d来实现, 因为不知道有多少个.
请问这个问题怎么解决. C++ 里用sringstram 能实现么?
多谢!
k**f
发帖数: 372
13
来自主题: Programming版 - 一个c语言的问题

did you allocate space for s3_1, s3_2, s_3 before calling sscanf()? As it is
now, these pointers to char are wild pointers pointing to random locations.
So you get the error when you call sscanf() with them.
One way to get rid of the error is to declare s3_1, s3_2 and s3_3 as
character array, assuming the strings are no longer than 31 characters:
char s3_1[32], s3_2[32], s3_3[32];
Also, the previous post correctly pointed out that you should use &s4_4 as
the last argument for the call.
c**b
发帖数: 2999
14
use sscanf
char s[2000],b[200];
int a,c;
cin.getline(s,2001);
sscanf(s, "%d %s %d", &a, b, &c);
x******a
发帖数: 6336
15
来自主题: Programming版 - C++ problem
I got thousands problems on the following piece of code "dumpfile.h" when I
compile under cygwin. it is ok under visual stduio... can anyone help?
Thanks!
#include
#include
#include //ostream_iterator
#include //cerr
#include //std::copy
template
void dump_to_file(const char* filename, const std::vector& v_d){
std::ofstream ofs(filename);
if(!ofs){
std::cerr<<"Unable to open the file to write!n";
return ;... 阅读全帖
q***i
发帖数: 627
16
来自主题: Programming版 - C 语言,数字变字符,有点难度
sscanf可以解决几乎所有的数字
#include
int main ()
{
char sentence []="Rudolph is 12 years old";
char str [20];
int i;
sscanf (sentence,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);

return 0;
}
A**u
发帖数: 2087
17
【 以下文字转载自 Programming 讨论区 】
【 原文由 Aliu 所发表 】
谁知道 sscanf 这个东东? 为什么读浮点数总有问题? 比如:
sscanf(char_array, "%d %f", &i, &f);
其中char_array是从文件或屏幕上读入的一行. f值好象总有问题, 为什么??
谢谢先!
t*q
发帖数: 104
18
来自主题: JobHunting版 - 狗狗电面
int dayOfWeek(const char* str) {
int y, m, d;
sscanf(str, "%d-%d-%d", &m, &d, &y);
int w = y + y/4 - y/100 + y/400 + d - 1;
for (int i = 1; i < m; ++i) {
if (i == 2)
w += /* 28 + */ y % 4 == 0 && (y % 100 || y % 400 ==0);
else
w += 30 + (i % 2 ^ i >= 8);
}
return w % 7;
}
m******e
发帖数: 353
19
来自主题: JobHunting版 - one amazon interview problem
#include
#include
void countOnesAndZeros(int &numZeros, int &numOnes, const std::vector &
sequence) {
numOnes = 0;
numZeros = 0;
for(size_t i = 0; i sequence[i] == 1 ? ++ numOnes : ++ numZeros;
}
}
int trimLeft(const std::vector &sequence, int start, int numToTrim, int
untilSeenSymbol) {
int trimCnt = 0;
while(start + trimCnt >= 0 && start + trimCnt < (int) sequence.size() &&
numToTrim > 0) {
if(sequenc... 阅读全帖
m******e
发帖数: 353
20
来自主题: JobHunting版 - 从水木上看到个数组题
ok, my bad, keep track of range [0, neg) and [numNeg, pos), and do not
rearrange those (since they are already in the correct place)
use [neg, numNeg) and [pos, N) as circular buffer for un-processed elements
#include
#include
#include
using namespace std;
void print(const vector& input) {
for(size_t i = 0; i < input.size(); ++i) {
cout << input[i] << " ";
}
cout << endl;
}
int numNegatives(const vector& input) {
int cnt = 0;
... 阅读全帖
n*******e
发帖数: 612
21
来自主题: JobHunting版 - 上一道我以前喜欢出的题目吧
c++ 用strtok在atoi吧
或者sscanf循环
l*********8
发帖数: 4642
22
double readNumber(const char * &str) {
double num;
sscanf(str, "%lf", &num);
for (++str; isdigit(*str) || *str == '.'; ++str)
;
return num;
}
double calculate(const char * str) {
if (*str == '\0') return 0.0;
double result = 1.0;
char op = '*';
while (op == '*' || op == '/') {
if (op == '*')
result *= readNumber(str);
else
result /= readNumber(str);
op = *str++;
}
return result + calc... 阅读全帖
l*******b
发帖数: 2586
23
学习了...发愁那个减号怎么处理呢...原来是sscanf...哈哈
l*********8
发帖数: 4642
24
double readNumber(const char * &str) {
double num;
sscanf(str, "%lf", &num);
for (++str; isdigit(*str) || *str == '.'; ++str)
;
return num;
}
double calculate(const char * str) {
if (*str == '\0') return 0.0;
double result = 1.0;
char op = '*';
while (op == '*' || op == '/') {
if (op == '*')
result *= readNumber(str);
else
result /= readNumber(str);
op = *str++;
}
return result + calc... 阅读全帖
l*******b
发帖数: 2586
25
学习了...发愁那个减号怎么处理呢...原来是sscanf...哈哈
d**********x
发帖数: 4083
26
来自主题: JobHunting版 - 报Offer
"够用了"。。。
1. 逻辑问题,哪个写lib的人会抱怨库少?我们都是用库的。
2. c++11的标准库。。。它是比c++03强多了,但是你要是拿来和java这么多年的积累
比,还是渣。和Qt/boost比,也是渣。
举个例子,就看那个半残的string接口,我每次都有一种灭了标准委员会
的想法。你能trim吗?不能,找regexp吧。你能split吗?不能,找regexp吧。你能
tolower吗?不能,transform。你能toUtf8吗?你能fromUtf8吗?你能转URL encoding
吗?你能直接sscanf吗?连个string这么基本的东西都不好好弄一下的,固守什么什么
哲学,不引进实用的特性,就是个渣,有什么好辩护的。面对这样渣的库最后就是东一
家西一家的山寨。
好,看看c++11的新库,像模像样还引进了一个thread库,看看里面有啥,mutex,
condition,恩,不错,哎哟semaphore呢?还得老子亲自从mutex和condition山寨?坑
爹还是坑娘呢?我想要个thread pool有现成的吗?是,不难,又山寨一坨。reader/
writer ... 阅读全帖
s********u
发帖数: 1109
27
来自主题: JobHunting版 - 算法题:Find the latest version
http://www.careercup.com/question?id=5673258271637504
Find the latest version of released software. For e.g1. 2 and 2.2.. latest
is 2.2. eg2: 3.1 and 3.1.3... latest version is 3.1.3... version is passed
as string in above format.
一道面经,感觉还是蛮有意思的。正好复习了下怎么用stringstream把string转换成
int/double/
下面的回复,一种解法是把3.1.3转换成一个数组{3,1,3},然后对所有数组补0,再依
次从右往左对每一位排序(有点像radix sort)但是这样感觉还是比较繁琐,相当于对
每一位都要做一次快排,也就是O(knlogn)
另一位说用radixsort,就是从右往左,每次做一个bucket sort。这样的好处是,每一
位只要O(n),总共O(kn)。但坏处是,比如其中一位是很大的数,那么开的空间就要很
大,其实t... 阅读全帖
s********u
发帖数: 1109
28
来自主题: JobHunting版 - 问一个post fix 算式计算的问题
如果是prefix,用递归方便一点;如果是postfix,用stack方便。
字符串输入进来转换成数字的话,可以用sscanf,个人喜欢用stringstream。
l****h
发帖数: 1189
29
带逗号的就不是数字了
l*********8
发帖数: 4642
30
又写了一遍,供参考
int getValue(const char * &expr) {
int val;
sscanf(expr, "%d", &val);
while (isdigit(*++expr))
;

return val;
}
int evaluate(const char *expr) {
if (!expr || !*expr) return 0;

int ans = getValue(expr);
while (*expr == '*' || *expr == '/') {
if (*expr == '*')
ans *= getValue(++expr);
else
ans /= getValue(++expr);
}

return ans + evaluate(expr);
}
h****j
发帖数: 15
31
longway2008 的code是对的。
sscanf 正负数都可以读。
while (isdigit(*++expr))
;
这里用后加,就跳过了开始的符号位。
比如
3 - 2 - 1. 事实上longway的code会分解成
3 + (-2) + (-1)。只用加法足矣。
膜拜longway大神。
yuren的code的结果就错了,他的算的是 3 - (2 - 1).
c**y
发帖数: 127
32
比如
一行里是
1 3 -1
如果我要读到这个行的每个数,最方便的
操作是怎样。
还有,要输出到文本文件时如何规定格式之类的。
在c++ 里,俺一般用
sscanf,sprintf.
谢谢。
N****w
发帖数: 21578
33
you are talking about sprintf or
sscanf?
O******e
发帖数: 734
34
来自主题: Programming版 - 如何把一个char转换成一个int啊?
Call sscanf(), skip the characters you don't want, read only the character(s)
you want.
e*****w
发帖数: 144
35
来自主题: Programming版 - 问个C/C++题目
C:
int x, n;
while (sscanf(line, "%d%n", &x, &n) == 1) {
// x is good
line += n;
}
C++:
int x;
std::istringstream is(line);
while (is >> x) {
// x is good
}
d*******d
发帖数: 2050
36
来自主题: Programming版 - 问个C/C++题目
他用的真是很牛啊。
我去读了半天sscanf的manu,参数的最后一行是这个n的用法。
还从来没用。
k****e
发帖数: 100
37
来自主题: Programming版 - sscanf problem in MSVC 7
It seems right.
0.7999999..... == 0.8
i*****f
发帖数: 578
38
来自主题: Programming版 - sscanf problem in MSVC 7
But why is that when I try that using gcc, I got exactly 0.8 ???
m****e
发帖数: 7
39
来自主题: Programming版 - sscanf problem in MSVC 7
How do you know it's exactly 0.8 on gcc?? There is never an "exact 0.8" for
floating point value. 0.8 = 4/5, and 5 is not a power of 2.
i*****f
发帖数: 578
40
来自主题: Programming版 - sscanf problem in MSVC 7
All right, you guys are right. I found it's printed like 0.8000...4 in gdb
the same phenomena as in MSVC. But when I compare it with 0.8, it returns
true.
t****t
发帖数: 6806
41
来自主题: Programming版 - sscanf problem in MSVC 7
没听说过直接比较两个浮点数相等的
d*******d
发帖数: 2050
42
来自主题: Programming版 - sscanf problem in MSVC 7
normal,负点精度。
l*****e
发帖数: 64
43
来自主题: Programming版 - c++ template中如何判断类型
i guess one reason maybe that:
it's more flexible to control the reading/writing using fprintf/sscanf than
ofstream, especially when working with a bit complex data.
l***8
发帖数: 149
44
来自主题: Programming版 - 一个c语言的问题
char s1[256], s2[256], s3[256];
float f1;
int n1, n2, n3;
sscanf(input_string, "%*s%*s%s%s%*[^\"]\"%[^\"]\"%*s%*s%f%d%d%d", s1, s2, s3
, &f1, &n1, &n2, &n3);
t*i
发帖数: 72
45
来自主题: Programming版 - 一个c语言的问题
I would like divide s3 in 4 strings like SP,C, 0806, 250.0.
I tried to use
char *s3_1;
char *s3_2;
char *s3_3;
float s4_4;
sscanf(s3,"%s%s%s%f,s3_1,s3_2,s3_3,s3_4);
But it shows Segmentation fault when I run it.
could you tell me what's correct way to do it? Thank you very much

s3
l***8
发帖数: 149
46
来自主题: Programming版 - 一个c语言的问题
you should pass in pointers in sscanf of integers and floats
"&s3_4"
H***a
发帖数: 735
F********g
发帖数: 475
48
来自主题: Programming版 - 一个c语言的问题
借题请教
如果需要处理这样一个GPS数据
"$GPGGA,210917.000,3021.37564,N,09107.50991,W,0,00,99.0,003.75,M,-25.9,M,,*
65"
需要抓出210917.000
3021.37564
N
09107.50991
W
0
用SSCANF如何处理?
A******g
发帖数: 612
49
来自主题: Programming版 - 一个c语言的问题
读到逗号的符号是%[^','],
比如
sscanf(str,"%[^','],%[^','] ...",para1,para2);
个人不喜欢parse逗号,我宁愿把逗号换成空格,比如
F********g
发帖数: 475
50
来自主题: Programming版 - 一个c语言的问题
Thanks, I tried
sscanf(g_string[g_i-1],"%*[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%*s",gps_
data.utc,gps_data.lat,gps_data.lat_s,gps_data.lon,gps_data.lon_s,gps_data.qi
);
and worked.Not sure how safe it is though.
1 (共1页)