Error 1 error C2664: 'parse' : cannot convert parameter 1 from
'std::vector<_Ty>' to 'char *' c:\documents and
settings\luckie\ultra\ultra\ultra\ultra.cpp 122
And how to convert vector<char> into PCSTR
PCSTR buf2 = buf;
Error 2 error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>'
to 'PCSTR' c:\documents and settings\luckie\ultra\ultra\ultra\ultra.cpp 137
Thanks
Jack
Thanks
Jack
Still seeking assistance for the second question
Thanks
Jack
>
> Thanks
> Jack
>
>
>
Jack:
PCSTR is const char*.
std::vector<char> x(100);
const char* buf = &x[0];
This works because std::vector::operator [] returns a reference to the
element at the chosen position.
--
David Wilkinson
Visual C++ MVP
Jack:
What makes you think \n is stripped? std::vector knows nothing about
what is being put in it.
Nope!
> I want to avoid this and keep those endlines.
> Is it possible?
std::vector<char> vc;
vc.push_back('\n');
vc.push_back('a');
vc.push_back('@');
vc.push_back('\n');
-- or even --
vc.push_back('\0');
What's the problem you're trying to solve?
Regards,
Stefan
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
void process_file(std::string filename)
{
//FILE *afile;
char c;
ifstream infile(filename.c_str ());
//if ((afile = fopen (filename, "rt")) == NULL)
//{
// exit(0);
//}
if(!infile)
{
cerr<<"Can't open file " << filename << endl;
exit(-1);
}
//fileData = (char *) malloc (50000);
// memset (fileData, 0, sizeof(fileData));
// fread (fileData, sizeof(char), sizeof(fileData), afile);
while (infile >> c)
{
fileData.push_back(c);
}
printf ("Parsing %s\n", filename.c_str());
parse (fileData, fileData.size());
}
void parse (vector<char> buf, long count)
{
int i,j;
char *tokens[256];
long cnt = 0; // count for number of tokens
char c;
bool ignore = false;
int k = 0;
char buff[256];
PCSTR buf2 = &buf[0];
// char p;
std::string buf3;
// static long token_count = 0;
for (;;) {
memset (buff, 0, 256);
trim (buf2);
is_comment(buf2[0], buf2[1]);
if (one_line_comment)
{
while (*buf2 != '\n') <<<<<<<<<<<< when I took a view at buf2, all \n
disappeared
buf2++;
buf2++; // Skip tail \n
I think that even the std::string can be fooled to skip special
characters, e.g.:
#define count_array(a) (sizeof(a)/sizeof(a[0]))
void Test()
{
const char a[] = _T("a\0b");
const std::string str(a, count_array(a) - 1);
_ASSERT(str.length() == 4);
}
be careful however with translations back to const char* (e.g.
str.c_str())
then see boost.spirit parsing library at www.boost.org.
>
...
> trim (buf2);
perhaps trim above considers "\n" as white space and removes them?
> is_comment(buf2[0], buf2[1]);
> if (one_line_comment)
> {
> while (*buf2 != '\n') <<<<<<<<<<<< when I took a view at buf2, all \n
> disappeared
> buf2++;
> buf2++; // Skip tail \n
Jeff Flinn
Jack:
1. This is happening because you use >> operator. It does not have to do
with std::vector. Use std::istream::get() like this:
while (infile.get(c))
{
//
}
2. How does this code compile?
PCSTR buf2 = &buf[0];
trim (buf2);
Isn't the trim function modifying buf2 here? Don't you need non-constant
buffer:
char* buf2 = &buf[0];
3. You don't need to pass the size of the vector, because std::vector
knows its own size.
4. You code will not work because you are passing fileData by value. You
must pass it by reference;
void parse (vector<char>& buf, long count)
{
}
5. It is almost always best to pass standard library objects by
reference even when it is not necessary. If the object is not modified
then pass by const reference:
void process_file(const std::string& filename)
{
}
6. What is "one_line_comment? It would seem to be a global variable. It
is almost always wrong to use global variables in C++. Why not just
if(is_comment(buf2)
{
}
7. I would recommend you read a good introductory book on modern C++,
one that emphasizes (correct) usage of the standard library. You have a
lot of bad programming habits.
"David Wilkinson" <no-r...@effisols.com> 撰寫於郵件新聞:%23PeWGNP...@TK2MSFTNGP06.phx.gbl...
In addition to David's suggestion, if you're interested
in preserving the line feeds, why not just read the file
by line into a std::list<std::string> or std::vector<std::string>
to begin with instead of by char.
Not tested and missing some validation:
template <typename T>
bool LoadStringContainerFromFile(T &container, const std::string &FileName)
{
// attempt to open the file for reading
std::ifstream infile(FileName.c_str());
if(!infile.is_open()) return false;
//If file can be opened create a buffer
std::string buffer;
container.clear(); // Clear the list
while(std::getline(infile,buffer,'\n')) {
//need some validation here
container.push_back(buffer);
}
return true;
}
So you can load a vector or list with this and maintain
the lines.
"Duane Hebert" <sp...@flarn.com> 撰寫於郵件新聞:uHm5nVP2...@TK2MSFTNGP06.phx.gbl...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Jack" <j...@knight.com> wrote in message
news:efUI02O2...@TK2MSFTNGP03.phx.gbl...