AKB Forums

Go Back   AKB Forums > Technical sections > Languages, Compilers and Interpreters
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Languages, Compilers and Interpreters C,C++,C#,.NET,Java,PHP,Perl,SQL and more

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Jul 15, 2004, 09:08   #1
панаехавший
 
Obelix's Avatar
 
Join Date: Jun 2003
Location: форпост
Posts: 4,005
Rep Power: 6
Reputation: 10
Send a message via ICQ to Obelix
*scanf wierd problem

This is something I don't really understand. I pass a string to a server process via TCP/IP (that's not really important though). Anyway, on the server we do something like
Code:
char buf[16];
if(sscanf(s,"%*s %s",buf)!=1) {
printf("assignment failed\n");
}
else {
printf("assignment ok... buf=%s",buf);
}
Now at first I send s to be:
KEY
I get something like "assignment of... buf= @". I hit a space after KEY, the same tihng. I get two or three spaces and i get assignment failed.
No if after that I send a simple KEY with no spaces i always get an assignment failure.
Same thing happens if i send a normal string:
KEY VAL
and then give the bad ones, i get a failure on aech following bad one.

Any idea? Could this be a bug?
__________________
Իսկ ԴՈՒ արդեն վաճառե՞լ ես Հայրենիքդ ռուսներին:

My Exchange Rate Monitor | Իմ Արտարժույթի Մոնիտորը

Obelix is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 10:42   #2
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
Code:
if(sscanf(s,"%*s %s",buf)!=1) {
what does the red part of SCANF ?
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 11:33   #3
Ребе - коп!
 
Speedy Gonzales's Avatar
 
Join Date: Mar 2003
Location: sfba
Posts: 4,183
Rep Power: 6
Reputation: 32
праильно
попробовав scanf() вместо sscanf() проблема четко обозначилась
Speedy Gonzales is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 11:38   #4
панаехавший
 
Obelix's Avatar
 
Join Date: Jun 2003
Location: форпост
Posts: 4,005
Rep Power: 6
Reputation: 10
Send a message via ICQ to Obelix
Quote:
Originally Posted by greco El
Code:
if(sscanf(s,"%*s %s",buf)!=1) {
what does the red part of SCANF ?
According to the manual a '*' after the '%' will mean that a conversion is done but no variable is assigned, i.e. buf should get the second conversion.
__________________
Իսկ ԴՈՒ արդեն վաճառե՞լ ես Հայրենիքդ ռուսներին:

My Exchange Rate Monitor | Իմ Արտարժույթի Մոնիտորը

Obelix is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 11:53   #5
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
очень плохой английский, извини.
Можно на русском - что ожидалось этой строкой кода?

Если я прально понял, то ты хочешь проигнорировать первую строку?

Возможно, все дело в этой *.. Так ты лучше считай ее в буффер, и забудь, вместо того, чтоб "доверять" это дело системе. Надежнее.
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 13:25   #6
the mochinger
 
Hans Andersen's Avatar
 
Join Date: Feb 2002
Location: Paranoid Android, @10:50
Posts: 1,769
Rep Power: 7
Reputation: 109
Send a message via ICQ to Hans Andersen Send a message via MSN to Hans Andersen Send a message via Yahoo to Hans Andersen
2OP: what compiler you are using?

I've tried with VC60 and got ok result

Code:
	char *s = "key val";
	char buf[16];
	if(sscanf(s,"%*s %s",buf)!=1) {
		printf("assignment failed\n");
	}
	else {
		printf("assignment ok... buf=%s",buf);
	}

output was:
assignment ok... buf=valPress any key to continue
so what's your problem?
__________________
The flower that blooms in adversity is the most rare and beautiful of all.
Hans Andersen is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 14:23   #7
панаехавший
 
Obelix's Avatar
 
Join Date: Jun 2003
Location: форпост
Posts: 4,005
Rep Power: 6
Reputation: 10
Send a message via ICQ to Obelix
Sorry False alarm
Со scanf все нормально, просто я по сокетам получаю что-то непонятное. Кстати, пользуясь случаем, кто знает есть ли в стандартной библиотеке что то типа trim() в пхп или если не ошибаюсь chomp() в перле?

>greco
Да, нужно проигнорировать первых параметр, но это повидимому работает, а с буффером придется лишние переменные вмешивать.
__________________
Իսկ ԴՈՒ արդեն վաճառե՞լ ես Հայրենիքդ ռուսներին:

My Exchange Rate Monitor | Իմ Արտարժույթի Մոնիտորը

Obelix is offline   Reply With Quote Quote selected
Old Jul 15, 2004, 14:41   #8
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
step#1: for check use another data instead of "buf=@" - only letters, say.
step#2:
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Jul 16, 2004, 09:40   #9
Грустно...
 
Agregat's Avatar
 
Join Date: Aug 2002
Location: Там, где всегда идут дожди
Posts: 21,614
Rep Power: 11
Reputation: 202
Send a message via ICQ to Agregat Send a message via MSN to Agregat
zvezdochka, po idee dolzhan trebovat' dlinnu bufera, kak parametr function a...
2 pol'zujtes' C++ om v konce kontsov to...
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Agregat is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird problem in WinXP Boyov Software 4 May 4, 2005 17:11
IIS problem Hovo Web Development 6 Jun 21, 2004 11:17
Pancake flipping problem acid Algorithms 1 Jun 2, 2004 19:16
PHP: Header and Cookies problem Aram Ghazanchyan Web Development 9 Jun 13, 2003 06:48
Priner -- Problem :( Mono Hardware 4 Aug 25, 2002 03:06


All times are GMT. The time now is 08:50.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link