Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to pass vector<char> as parameter?

0 views
Skip to first unread message

Jack

unread,
Aug 7, 2007, 5:24:19 AM8/7/07
to
void parse (vector<char> buf, long count)

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

Jack

unread,
Aug 7, 2007, 5:27:36 AM8/7/07
to

"Jack" <j...@knight.com> 撰寫於郵件新聞:eXehxSN2...@TK2MSFTNGP04.phx.gbl...

> void parse (vector<char> buf, long count)
>
> 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
Forget this.... Sorry

Thanks
Jack


Jack

unread,
Aug 7, 2007, 5:29:44 AM8/7/07
to

"Jack" <j...@knight.com> 撰寫於郵件新聞:eXehxSN2...@TK2MSFTNGP04.phx.gbl...

Still seeking assistance for the second question
Thanks
Jack


>
> Thanks
> Jack
>
>
>


David Wilkinson

unread,
Aug 7, 2007, 6:50:44 AM8/7/07
to
Jack wrote:
> "Jack" <j...@knight.com> ���g��l��s�D:eXehxSN2...@TK2MSFTNGP04.phx.gbl...

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

unread,
Aug 7, 2007, 6:56:13 AM8/7/07
to

"David Wilkinson" <no-r...@effisols.com> 撰寫於郵件新聞:uyJR$CO2HH...@TK2MSFTNGP05.phx.gbl...
> Jack wrote:
>> "Jack" <j...@knight.com>
>> ???g??l??s?D:eXehxSN2...@TK2MSFTNGP04.phx.gbl...

>>> void parse (vector<char> buf, long count)
>>>
>>> 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
>>
>> Still seeking assistance for the second question
>> Thanks
>
> Jack:
>
> PCSTR is const char*.
>
> std::vector<char> x(100);
> const char* buf = &x[0];
Wow, works like magic... Thanks
Jack

Jack

unread,
Aug 7, 2007, 7:22:48 AM8/7/07
to
Sorry, there is one more question.
If I use push_back to build a vector<char>,
the compiler automatically strip \n and so forth.
I want to avoid this and keep those endlines.
Is it possible?
Thanks once again
Jack


David Wilkinson

unread,
Aug 7, 2007, 7:47:14 AM8/7/07
to
Jack wrote:
> Sorry, there is one more question.
> If I use push_back to build a vector<char>,
> the compiler automatically strip \n and so forth.
> I want to avoid this and keep those endlines.
> Is it possible?

Jack:

What makes you think \n is stripped? std::vector knows nothing about
what is being put in it.

Stefan Naewe

unread,
Aug 7, 2007, 7:56:04 AM8/7/07
to
On 8/7/2007 1:22 PM, Jack wrote:
> Sorry, there is one more question.
> If I use push_back to build a vector<char>,
> the compiler automatically strip \n and so forth.

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

Jack

unread,
Aug 7, 2007, 8:23:21 AM8/7/07
to
I am working on a file parser.

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


gas...@hotmail.com

unread,
Aug 7, 2007, 8:31:15 AM8/7/07
to

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())

Jeff Flinn

unread,
Aug 7, 2007, 8:51:35 AM8/7/07
to
Jack wrote:
> I am working on a file parser.

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

unread,
Aug 7, 2007, 9:01:02 AM8/7/07
to

"Jeff Flinn" <Je...@news.microsoft.com>
???????:ea6vTGP2...@TK2MSFTNGP06.phx.gbl...

> Jack wrote:
>> I am working on a file parser.
>
> then see boost.spirit parsing library at www.boost.org.
>
>>
> ...
>
>> trim (buf2);
>
> perhaps trim above considers "\n" as white space and removes them?
No, it isn't. Because I found out that the "\n" were removed when I viewed
the "buf" in the first place and before trim() was trying to do anything
with it.
Thanks
Jack

David Wilkinson

unread,
Aug 7, 2007, 9:03:21 AM8/7/07
to

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.

Jack

unread,
Aug 7, 2007, 9:11:23 AM8/7/07
to
Ok. Seems to be a lot of work improvements that I need to do...
Thank you for your comments
Jack


"David Wilkinson" <no-r...@effisols.com> 撰寫於郵件新聞:%23PeWGNP...@TK2MSFTNGP06.phx.gbl...

Duane Hebert

unread,
Aug 7, 2007, 9:17:58 AM8/7/07
to

"David Wilkinson" <no-r...@effisols.com> wrote in message
news:%23PeWGNP...@TK2MSFTNGP06.phx.gbl...

> 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))
> {
> //
> }
>

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.


Jack

unread,
Aug 7, 2007, 9:26:59 AM8/7/07
to
Hello Duane,
Thanks you for comments too.
I will try them..
Jack

"Duane Hebert" <sp...@flarn.com> 撰寫於郵件新聞:uHm5nVP2...@TK2MSFTNGP06.phx.gbl...

Alexander Nickolov

unread,
Aug 7, 2007, 8:45:23 PM8/7/07
to
I've never bothered with the C++ I/O classes, but basically, your
problem is most likely the same one you have in your commented
code using CRT file I/O - you are reading the file in text mode.
For the commented code to work you need to replace "rt" in
fopen with "rb". Someone else should easily point out the syntax
for using binary I/O with the C++ stream classes...

--
=====================================
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...

0 new messages