473,441 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

input streams, setw(), and strings vs. char*?


If setw() is being used to limit how many characters are pulled from
a stream, should the # of characters taken from the stream depend on
whether the stream is being sent to a std::string or a char*?

string s1;
char s2[256];
cin >> setw(4) >> s1;
cin >> setw(4) >> s2;

(I've found there to be a difference between compilers -- on some,
they results are identical (ie, 3 characters pulled), and on others, the
string receives a 4th character)

Thanks,
Matt

----------------------------------------------------------------------
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;
int main(void)
{
string s;
char buffer[256];
istringstream is1("0123456789");
is1 >> setw(4) >> s;
is1.seekg(0);
is1 >> setw(4) >> buffer;

cout << "s = " << s << endl;
cout << "buf= " << buffer << endl;

return 0;
}
Jul 19 '05 #1
1 9587

"Matthew David Hills" <hi***@Stanford.EDU> wrote in message
news:bj**********@news.Stanford.EDU...

If setw() is being used to limit how many characters are pulled from
a stream, should the # of characters taken from the stream depend on
whether the stream is being sent to a std::string or a char*?

string s1;
char s2[256];
cin >> setw(4) >> s1;
cin >> setw(4) >> s2;

(I've found there to be a difference between compilers -- on some,
they results are identical (ie, 3 characters pulled), and on others, the
string receives a 4th character)

Thanks,
Matt


Its a good point, and the answer is yes the number should vary. The reason
is that setw is being used for subtly different reasons in the two cases.
For std::string setw is that maximum numbers of characters to be extracted,
for char* setw is intended as the size of the array that the char* is
pointing at. Since C style strings require a null terminator only setw - 1
characters can be extracted.

john
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Henry Jordon | last post by:
I have everything pretty much done but I need to fix something in my coding. I want to be able to enter strings such as "love", "hate", "the", etc. but am unable to figure how to do this. I have...
2
by: tvn007 | last post by:
Below is my code: #include <string> #include <iostream> #include <iomanip> int main (void){ using namespace std; string name1 = "JOHN"; int id1 = 1234;
30
by: Richard | last post by:
Level: Java newbie, C experienced Platform: Linux and Win32, Intel Another programmer and I are working on a small project together. He's writing a server process in Java that accepts input...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
1
by: Gaijinco | last post by:
Hi! I was coding something which had some lines like these: int main(){ int n; scanf("%d",&n); fflush(stdin); int* ans = new int; int idx=0;
4
by: Kza | last post by:
Hi, just in the process of maintaining some software that used some funy old string library and char*s , and we are updating everything to use std::strings. (or should I say std::basic_string<>s) ...
4
by: Sudip | last post by:
I'm new in c programming. I am writing a program which reads the ID3 V1 tag from a mp3 file and edits it. But everytime i try to take inputs , the first character of my album contains 0. So, it...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.