Parsing list of strings ending with double NULL [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to parse in c++ list of null-terminated Unicode strings where the list is terminated with two NULL characters?

There's this little example on Raymond Chen's blog (which, perhaps not surprisingly, is the first find in Google for "double null terminated string"):
This reinterpretation of a double-null-terminated string as really a
list of strings with an empty string as the terminator makes writing
code to walk through a double-null-terminated string quite
straightforward:
> for (LPTSTR pszz = pszzStart; *pszz; pszz += lstrlen(pszz) + 1) { ...
> do something with pszz ... }
The LPTSTR and lstrlen are wrappers which change depending on whether or not _UNICODE is set.

You simply build a list of strings and abort when one is empty:
std::vector<std::string> result;
result.push_back( std::string() );
while (std::cin) {
char c = std::cin.get();
if ( c == 0 ) {
if ( result.back().empty() ) { result.pop_back(); return; }
else result.push_back(std::string()); }
} else {
result.back().push_back(c);
}
}

Related

Need to set a array string length so that it is exactly 13 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
hi there i'm currently finishing a project at college but need to set a length of 13 to a string so that it will return an error message if it is shorter or longer than 13 my code so far is
void add_new_text_book()
{
//this option is here to add a new book to the list
printf("Please enter the Title of a new text book\n");
scanf("%s",book[number_of_books].title);
printf("Please enter the Authors firstname\n");
scanf("%s",book[number_of_books].firstname);
printf("Please enter the Author surname\n");
scanf("%s",book[number_of_books].surname);
printf("Finally please enter the ISBN number of the book\n");
scanf("%s",book[number_of_books].isbn);
if(length_of_string==13)//will be used to check the length of the book is valid
{
if(number_of_books==15)//will check to see how many records have been used
{
printf("book not added as you have used all free space\n");
}else
{
printf("Book has been added to the libary\n");
number_of_books=number_of_books+1;
}
}else{
printf("You have entered too many or few characters the books has not been saved\n");
}
getch();
length_of_string=strlen(book[number_of_books].isbn);
but even when i enter 13 it comes up with the error message it only seems to accept 123-456-789-1 any help will be greatly accepted
It would help considerably if you calculated length_of_string before you used it, rather than after.
You can use str.size() to do your calculations.
Taken from C++ reference
// string::size
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << "The size of str is " << str.size() << " characters.\n";
return 0;
}
what the value of "length_of_string" ?
is its value is :123-456-789-1?
if so you can use it :
if(length_of_string.size()==13)//will be used to check the length of the book is valid
{
if(number_of_books.size()==15)//will check to see how many records have been used
{
printf("book not added as you have used all free space\n");
}else
{
printf("Book has been added to the libary\n");
number_of_books=number_of_books+1;
}
}
else
{
printf("You have entered too many or few characters the books has not been saved\n");
}
getch();
}

Reading string from vector until whitespace in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is probably easy but im not sure how. I tried searching multiple websites and yes Google and couldn't find anything on this.
My vector result[0] looks like this
A3 * * B4 * *
Declaration
vector<string> result = v.formVectorFile("Prj3 Config.txt");
I know that cin reads until whitespace so I was trying to use this to figure it out.
If I read straight from fstream I can read until whitespace, but im trying to do this with a string inside a vector and something like result[0] >> s; obviously doesnt work.
I need to read until it hits a whitespace then read the next one until whitespace. Etc...
So extract A3 by itself. Operate on it then extract * etc...
Your question is unclear because you don't tell us precisely what result is.
If we can assume that result is a std::vector<std::string>, then you can do something like this:
std::istringstream iss(result[0]); // consider only first string in vector
std::string item;
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}
If we assume that result is std::vector<char>, then you can do this:
std::string s(result.begin(), result.end()); // consider entire vector as single string
std::istringstream iss(s);
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}

characters and files in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to make a program where the filename is composed of two parts: the first one is fixed and the second one can change during the program e.g "fixpart_integer.dat". I tried to do this in C++ but I did not succeed. The fisrt probelm is: how can i convert a number to a char ; and how can i concanate these 2 characters ; and how to declare this final char in the right way in order to open this filename ?
Many questions but I did find an easy way to do this.
#include <string>
#include <sstream>
std::string make_filename(std::string prefix, int id) {
std::stringstream ss;
ss << prefix << "_" << id << ".dat";
return ss.str();
}
and then I convert the string to char.
if you are working with c you may try the function described in here itoa
else if you are working woth c++ you may try something like this:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

c++ textfile borland [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I ask about the function that seek in the K-th line in a text file and the one that read the text file by line or by character in C++! knowing that i'm working with borland.
fpeek is an open source application that does exactly that. Check the sources and see how its done.
I took a quick look and I believe you'll end up with something like this (I haven't tested this code):
std::ifstream file(filename);
std::string line;
int pos = 1;
while (std::getline(file, line))
{
// Find if current line should be displayed
if (15 == pos) // looking for the 15th line in the file
{
std::cout << pos << ": " << line << std::endl;
}
pos++;
}

Problems with garbage characters when reading file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I'm having trouble reading data from a file, and concatenating selected parts of the data (text) into a buffer of my own.
The code is like follows:
char buffer[1000];
char* allNewData = (char *)malloc(10000);
while (! myfile.eof() )
{
myfile.getline (buffer, 1000);
pch = strstr (buffer,"bla bla");
if(pch == NULL)
{
char* temp = buffer;
strcat(allNewData, temp);
strcat(allNewData, "\n");
}
else
{
strcat(allNewData, "here's bla bla");
strcat(allNewData, "\n");
}
}
cout<<allNewData<<endl;
When I run the program, allNewData first has some garbage text, followed by the proper/expected results, like this:
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii <-rubbish data
hello <- actual data
I need to get rid of this rubbish data, how can I change the code to achieve this?
You need to clear your newly allocated buffer before using string concatenation functions. They expect a valid string, to be able to find the end and thus the start of where to concatenate.
Use:
allNewData[0] = '\0';
this makes allNewData into an empty string. Do this before the loop, before you start concatenating all the found data.
Also, your code needs to better take care of the various "gotchas" when it comes to I/O and handling memory:
Don't check for EOF before doing a read access.
Check that the read was successful, before using the results of the read.
Make sure you don't exceed the capacity of your buffer when storing data.
Some comments, which you may find helpful or disregard:
What if there is a line longer than 1000 characters? (and say, that 1001-1008 is 'blah blah')? The line will be split into two in your new file and there will be an extra line before "here's blah blah"? Is this now a bug or desired functionality?
What if the line is longer than 1000, but "blah" is 996-1000 and the second "blah" is on the second segment - now you've lost one
What if your file is longer than 10000 characters?
They may sound like trivial questions, but answering them correctly will mean that you'll have to change your approach, I suggest purer C++ approach:
ifstream f_in(<file>);
ostringstream s_out;
string line;
while(f_in.good())
{
getline(f_in, line); // global getline function in <string>
if (line.find("blah blah") != string::npos)
{
s_out << "here's blah blah" << endl;
}
else
{
s_out << line << endl;
}
}
This way you don't have to worry about any of the questions above...
You can also use a combination of getline and ignore
Again... you have to check that your IO operation don't fail and eof() should be used only after a failed IO operation.