scanf and iostreams

P

paidojoao-groups

Hi,

How do I get the (s)scanf behaviour below using IOStream?


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int m, d, y;

sscanf("05091968", "%02d%02d%04d", &m, &d, &y);

cout << m << "/" << d << "/" << y << endl; // 5/9/1968

return 0;
}


Regards,
Josue Gomes
 
V

Victor Bazarov

How do I get the (s)scanf behaviour below using IOStream?


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int m, d, y;

sscanf("05091968", "%02d%02d%04d", &m, &d, &y);

cout << m << "/" << d << "/" << y << endl; // 5/9/1968

return 0;
}

Unfortunately, you cannot. The simplest work-around is to extract
the characters, put them in a string, then use 'sscanf', just like
you do here.

Victor
 
M

marbac

Hi,

How do I get the (s)scanf behaviour below using IOStream?

Hi,

I dont think that this is possible with iostreams directly.
But you also can use atoi and the c++ string class to solve this problem

regards marbac


#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
string test="05091968";
cout << atoi(test.substr(0,2).c_str()) << "/"
<< atoi(test.substr(2,2).c_str()) << "/"
<< atoi(test.substr(4,4).c_str()) << endl; // 5/9/1968
return 0;
}

or without using c-style headers:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>

using namespace std;

int str2int(string str) {
istringstream istr (str);
int i;
istr >> i;
return i;
}

int main()
{
string test="05091968";
cout << str2int(test.substr(0,2)) << "/"
<< str2int(test.substr(2,2)) << "/"
<< str2int(test.substr(4,4)) << endl; // 5/9/1968
return 0;
}
 
M

marbac

Hi,

How do I get the (s)scanf behaviour below using IOStream?
Hi,

I dont think that this is possible with iostreams directly.
But you also can use atoi and the c++ string class to solve this problem

regards marbac


#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
string test="05091968";
cout << atoi(test.substr(0,2).c_str()) << "/"
<< atoi(test.substr(2,2).c_str()) << "/"
<< atoi(test.substr(4,4).c_str()) << endl; // 5/9/1968
return 0;
}

or without using c-style headers:

#include <iostream>
#include <sstream>
#include <string>


using namespace std;

int str2int(string str) {
istringstream istr (str);
int i;
istr >> i;
return i;
}

int main()
{
string test="05091968";
cout << str2int(test.substr(0,2)) << "/"
<< str2int(test.substr(2,2)) << "/"
<< str2int(test.substr(4,4)) << endl; // 5/9/1968
return 0;
}
 
M

marbac

marbac said:
Hi,

I dont think that this is possible with iostreams directly.
But you also can use atoi and the c++ string class to solve this problem

regards marbac

But then i think the sscanf method and usage of char* is faster
 
R

Richard Herring

marbac said:
But then i think the sscanf method and usage of char* is faster

But is speed actually a problem? ("premature optimisation considered
evil") If you have reason to think so, find out objectively whether it
really is; if not, do whatever is clearest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top