a "?" before the string - c++

I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"‪D:\\testlas\\BigOne.pcd",(char*)"‪‪D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?‪D:\\testlas\\BigOne.pcd
m.name: ?‪D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "‪abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?

std::string test = "‪abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.

Related

how to read a const char* from keyboard input and perform strlen() on it?

So I have been trying for 1.30 hour to get this to work. I am new indeed, but I have searched all over the place and couldn't find an exact answer. I do not wish to do this another way, as it would take away the entire purpose of learning to code. I have to find why this thing isn't working. I tried dozens if not hunderds of syntaxes, but nothing works.
I want to read in a const char* name, than count the number of elements in it, so I thought had to be strlen(), and than output the name and the number of elements. If that works I can write the rest of the code.
#include <iostream>
using namespace std;
int main()
{
//writing your name, and counting the characters including \0
int a;
const char* name;
a = int strlen(name);
cin.getline(name);
cout << name;
cout >> a;
return 0;
}
There are a lot of problems with your code.
You are not allocating any memory for cin.getline() to read into. const char* name; is declaring an uninitialized pointer to nothing. You have to allocate memory for name before you can then read any data into it.
cin.getline() expects two input parameters (a pointer to an allocated buffer, and the max number of characters the buffer can hold), but you are only passing in one value.
You are calling strlen() before you have read anything into name (and there is a syntax error on your strlen() statement anyway).
You are passing a to std::cout using >>, but std::ostream does not implement the >> operator. You have to use << instead.
And lastly, don't use using namespace std;.
Try this instead:
#include <iostream>
#include <cstring>
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
std::cin.getline(name, 32);
a = std::strlen(name);
std::cout << "You entered: " << name << std::endl;
std::cout << "It is << a << " chars in length" << std::endl;
return 0;
}
Or, if you really don't like using std:: everywhere, at least use using <identifier>; instead of using namespace std;:
#include <iostream>
#include <cstring>
using std::cin;
using std::strlen;
using std::cout;
using std::endl;
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
cin.getline(name, 32);
a = strlen(name);
cout << "You entered: " << name << endl;
cout << "It is " << a << " chars in length" << endl;
return 0;
}
Now, that being said, the preferred solution is to use std::getline() instead of cin.getline():
#include <iostream>
#include <string>
int main()
{
int a;
std::string name;
std::getline(std::cin, name);
a = name.length();
std::cout << "You entered: " << name << std::endl;
std::cout << "It is " << a << " chars in length" << std::endl;
return 0;
}
I found a working solution, although I don't see where I had gone wrong. But this does exactly what I want using const char* and strlen() without using std::string.
Thanks for all your help, you have all pointed me to the correct direction.
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main ()
{
const char *name;
int len;
name = "stephane";
len = strlen(name);
cout << name;
cout << len;
return(0);
}
As another user has pointed out, I think it's a good idea for you to take a few steps back and read the basics until you understand how pointers work.
A const char* is that: const. It could be used usually while doing things like this:
const char* cpName = "Stephane"; //expected not to change through the program's lifetime
char* pName = "Stephane"; //can be changed to point to something else
char *pOther = "Vada";
pName = pOther; //pName now points to the string "Vada"
cpName = pOther; //this won't compile as cpName is const

Text Parser c++ code

I need a C++ code for the following problem:
i have a text file that i want to start reading from a specific line, then i need to print the output located between the characters --- <\s>
example: hello<\s>
i want the output to be hello
I think i should use text parser but not sure how!
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
std::string line_;
ifstream file_("tty.txt");
if (file_.is_open())
{
while (getline(file_, line_))
{
std::cout << line_ << '\n';
}
file_.close();
}
else
std::cout << "error" << '\n';
std::cin.get();
system("PAUSE");
return 0;
}
You can load all text in one variable, and then with regex search all occurences of your desired pattern (in your case <sth>(any_aplha_numeric_character)*</sth> where * means one or more occurence, you can read about it at any std::regex tutorial)
Example:
std::smatch m;
std::string text = "<a>adsd</a> <a>esd</a>";
std::string::const_iterator searchStart(text.cbegin());
std::regex rgx("<a>[A-Za-z0-9\\s]*</a>");
while (std::regex_search(searchStart, text.cend(), m, rgx))
{
cout << m[0] << endl;
searchStart += m.position() + m.length();
}
gives: <a>adsd</a> and <a>esd</a> as a result, from which is very easy to extract that inner string

Cannot push C style strings into std::vector

I'm trying to push some const char* into a vector, but the vector remains unpopulated after performing the operations I would presume to fill it.
Here's my attempt, where dict is my command-line argument.
test.cc
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream dict;
size_t dict_size;
dict.open(argv[1]); // Dictionary
vector<const char*> dictionary;
string line;
getline(dict, line);
while(!dict.fail()) {
dictionary.push_back(line.c_str());
getline(dict, line);
}
dict_size = dictionary.size();
for(int i = 0; i < dict_size; i++)
cout << "dictionary[" << i << "] is " << dictionary[i] << endl;
}
dict
Hello
World
Foo
Bar
After compiling this, I get the following output:
dictionary[0] is
dictionary[1] is
dictionary[2] is
dictionary[3] is
However, if I change the dictionary's type to vector and push back line instead of line.c_str(), I get the expected output:
dictionary[0] is Hello
dictionary[1] is World
dictionary[2] is Foo
dictionary[3] is Bar
I'm not terribly familiar with C style strings, so maybe it has something to do with null termination?
You are storing dangling pointers.
std::string::c_str() isn't a pointer to some permanent copy of data — just think, that would be leaked!
Store the std::strings instead.
Your code invokes undefined behavior, because after you do
dictionary.push_back(line.c_str());
On the next line that pointer may get deleted:
getline(dict, line); // line now is a different string
You are pushing into the dictionary pointers that point to the same address and at the last iteration it fills the memory area with an empty string. If you don't care about memory leakage you can try like this:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream dict;
size_t dict_size;
dict.open(argv[1]); // Dictionary
vector<char *> dictionary;
while(!dict.fail()) {
string * line = new string();
getline(dict, *line);
if(line->length()>0)
{
dictionary.push_back((char *)line->c_str());
}
}
dict_size = dictionary.size();
for(int i = 0; i < dict_size; i++)
cout << "dictionary[" << i << "] is " << dictionary[i] << endl;
}

getting a sub string of a std::wstring

How can I get a substring of a std::wstring which includes some non-ASCII characters?
The following code does not output anything:
(The text is an Arabic word contains 4 characters where each character has two bytes, plus the word "Hello")
#include <iostream>
#include <string>
using namespace std;
int main()
{
wstring s = L"سلام hello";
wcout << s.substr(0,3) << endl;
wcout << s.substr(4,5) << endl;
return 0;
}
This should work: live on Coliru
#include <iostream>
#include <string>
#include <boost/regex/pending/unicode_iterator.hpp>
using namespace std;
template <typename C>
std::string to_utf8(C const& in)
{
std::string result;
auto out = std::back_inserter(result);
auto utf8out = boost::utf8_output_iterator<decltype(out)>(out);
std::copy(begin(in), end(in), utf8out);
return result;
}
int main()
{
wstring s = L"سلام hello";
auto first = s.substr(0,3);
auto second = s.substr(4,5);
cout << to_utf8(first) << endl;
cout << to_utf8(second) << endl;
}
Prints
سلا
hell
Frankly though, I think your substring calls are making weird assumptions. Let me suggest a fix for that in a minute:

Inserting a new line feed within a character array

I want to insert a new line feed within an array such that if the string length of my array increases beyond say 14 here the further contents of the array when displayed are displayed in a new line in the output console. Foe e.g Here in below program I want to Display "mein hoon don" in first line and after these 14 characters. I want to DISPLAY next content "Don Don Don" in a new line in output console. I read that using \0xa(hexa) and \10 in decimal are newline feed . But When i tried to use them in my code , I was not able to produce desired output.
# include <iostream.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
char abc[40];
strcpy(abc,"mein hoon don.");
abc[15]='\10';
abc[16]='\0';
strcat(abc,"Don Don Don");
cout << "value of abc is " << abc;
getchar();
}
Change:
abc[15] = '\10';
abc[16] = '\0';
to:
abc[14] = '\n';
abc[15] = '\0';
The escape sequence \10 does not do what you think it does. It is probably easiest to use \n instead.
Note: you could also use strcat to insert the newline and prevent any index calculation problems:
strcpy(abc,"mein hoon don.");
/* abc[15]='\10'; */
/* abc[16]='\0'; */
strcat(abc, "\n");
strcat(abc,"Don Don Don");
You are really better off using std::string instead of char arrays:
#include <iostream>
#include <string>
int main() {
std::string s;
s = "mein hoon don.";
s += "\n";
s += "Don Don Don";
std::cout << value of abc is " << s << std::endl;
return 0;
}
Well, the escape secuence depends on the operating system you are working: there are two caracters \r (carrier return) and \n (line feed), in windows, you need both, in linux, you need only \r and in Mac, you need \n.
but, as long as you are using cpp, you shouldnt use any of them, you should use endln instead:
//# include <stdio.h>
//# include <stdlib.h>
//# include <string.h>
#include <ostream>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
//char abc[40];
std::ostringstream auxStr;
auxStr << "mein hoon don." << std::endl << "Don Don Don" << std::endl;
//if you need a string, then:
//std::string abc = auxStr.str();
std::cout << "value of abc is " << auxStr.str();
getchar();
}
This code introduces a line break condition at position x.
Just set Line_Break=14; and you are done.
# include <iostream.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
int LinePos=0;
int Line_Break_Pos=0;
int Line_Break=10;
char abc[40]={""};
strcpy(abc,"mein hoon don. ");
strcat(abc,"Don Don Don");
cout << "\n" ;
cout << "Without linebreak : \n";
cout << abc ;
cout << "\n\n" ;
cout << "With linebreak : \n" ;
while (LinePos < strlen(abc) )
{
cout <<abc[LinePos];
if (Line_Break_Pos > Line_Break && abc[LinePos ]==' ')
{
cout << "\n" ;
Line_Break_Pos=0;
}
LinePos++;
Line_Break_Pos++;
}
cout << "\n\n" ;
return 0;
}