Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am wondering how std::string handles its buffers in terms of memory.
As I understand it char buf[BUFFER_SIZE]; will be allocated on the stack. So if it begin reading into that buffer
string result;
oFile.read(buf, BUFFER_SIZE-1);
bytesRead = oFile.gcount(); //get # of chars read into buffer
buf[bytesRead] = '\0'; //terminate with a null
sFinal += buf;
So my question is mainly on the += operation. When the buffer is concatenated with the string, does it need to reallocate more memory? As a follow up question does this memory need to be a continuous block? If so, would that allocation be a heap or stack operation?
std::string is an object in C++, then implicit constructors get char pointers as string to support C's string literals.
If you care about memory a std::ostringstream can be more useful that the + operator:
std::ostringstream result("hello");
result << " world!";
std::cout << result.str();
//prints "hello world!"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to substring console output of an std::string using std::string_view?
For example:
std::string toolong {"this is a string too long for me"};
std::string_view(toolong);
// do something...
expected console output: this is a string
Yes, it's called substring-ing.
std::string toolong {"this is a string too long for me"};
std::string_view view(toolong);
std::cout << view.substr(0, 16);
Alternatively, you can use the remove_prefix() and remove_suffix() methods as well.
Example:
view.remove_suffix(16); // view is now "this is a string"
view.remove_prefix(5); // view is now -> "is a string"
If you want to do it in-place without creating a variable of string_view, use substr()
std::string toolong {"this is a string too long for me"};
std::cout << std::string_view (toolong).substr(0, 16);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been looking and I have not found anything on scanf that really helps me. I am completly new to this and was hoping for help on reading a string with scanf. The first thre numbers can be any digits. I was attempting to read them into a variable int. the last one in the string is a char. this is my string
(1,2,123, 0)
(1,2,1,s)
This is my code:
int i,j,k;
char c, final;
scanf ("%c", c, "%d",&i, "%c", c, "%d", &j, "%d",&k, "%c", final);
I know this is not right but any help is appreciated
If the first 3 are digits and the last one is a character and are seperated by a space,which you want to assign to 3 integer variables and a character variable, use scanf like this:
int a,b,c;
char ch;
scanf ("%d%d%d %c",&a,&b,&c,&ch);
Or else if you want to extract 3 integers and a character from a string , use sscanf. It is not possible to do it with scanf.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Trying to write the following function but confused, as get() only reads in the first character?
Write C-string's chars to the screen one char at a time.
void writeString(const char*)
Rule:
cannot use [].
Hints:use put();
make use of '\0' – but don't write it out.
It sounds like you just need a simple loop to output the string. Something like this perhaps.
void writeString(const char* str)
{
while(str++ != '\0') put(*str);
}
The while(str++ != '\0') will iterate over the string buffer pointed to by str and output each character. It also increments the str pointer to the next character and checks for null terminator ('\0').
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
recently I made a program that, given a string, the function returns its corresponding ascii code. For example, the string "h", the function returns an int 104. Now, I want to do the reverse process, ie, given an int, return its corresponding ascii character. For example, given the 104 int return the string "h". Please, help.
Looking at the string constructors, we can see one that takes a count and a character value. So we can use that:
return std::string(1, ascii_value);
You probably don't need a whole string for this. Given that you're looking for a single character, the correct type to use is a char:
int x = 65;
char xc = (char)x;
assert(xc == 'A');
char c;
...
std::string mystring(1, c);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to display image from different locations. There are car, airplane, chair etc. files need to be display. I put those words (car, airplane) in a text file. I can read words but when I put them in sprintf, I get nonsense characters.
I can display the words with cout<<*j<<endl;. But cout<<filename<<endl; gives me weird result.
string words;
std::vector<string>list;
fstream file;
file.open("h.txt");
while(!file.eof())
{
file >> words;
list.push_back(words);
}
for(vector<string>::iterator j=list.begin(); j!=list.end(); j++)
{
cout<<*j<<endl;
for(i=1; i<5; i++)
{
sprintf( filename,"D:\\101_ObjectCategories\\%s\\image_%04d.jpg",*j,i);
cout<<filename<<endl;
The C function sprintf() is oblivious of the C++ classes. If you really want to print a std::string using sprintf() you'll need to extract a C string:
sprintf(filename, "D:\\101_ObjectCategories\\%s\\image_%04d.jpg", j->c_str(), i);
You should also use snprintf() together with the size of the buffer you pass as filename to prevent overflow. Personally, I wouldn't really bother and rather using std::ostringstream in the first place:
std::ostringstream out;
out << "D:\\101_ObjectCategories\\" << *j << "\\image_"
<< std::setfill('0') << std::setw(4) << i << ".jpg";
std::string filename = out.str();
(after including <sstream> and <iomanip>).
Use must use *j.c_str():
sprintf( filename,"D:\\101_ObjectCategories\\%s\\image_%04d.jpg",*j.c_str(),i);
Otherwise, the string class itself is cast into a char* explicitly which is garbage of course :)