Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string, char* and static char prob

Reply
Thread Tools

string, char* and static char prob

 
 
lumo2000@gmail.com
Guest
Posts: n/a
 
      07-09-2007
hello NG,

i am trying to do some syscalls and therefore i need to put some text
together.
its no problem as long i want to cout the text to display, but when i
want to use it
as parameter for functions the variable only passes NULL....
the out commented code segments is my try to convert the static char*
to char* so i can work with this one, but it does not work either...
(compiles but app crashes then...)

i load the names from the list to memory, and i

my code

#include "stdafx.h"
//#include "fstream.h"

bool listexists();
void getPopularNames();

int _tmain(int argc, _TCHAR* argv[])
{
if (listexists())
printf("taxa list file found!\n");
else
printf("error 404 - file [%S] not found\n");
getPopularNames();
// to get popular name call php file with latin name as
parameter
// php.exe getPopularName.php "latin name"
return 0;

}

void getPopularNames()
{
ifstream filestr;
std::string name;
//char* name;
std::string res;
filestr.open("taxa.liste",ios::in);
int counter = 0;
cout << "processing";
if( filestr.is_open() )
{
while( getline(filestr, name) ) {
char call[1024];
cout << name << '\n';

//char *nonconstant_namecopy;
//nonconstant_namecopy = new
char[name.length() + 1];
//strcpy_s(nonconstant_namecopy, name.length()
+1, name.c_str());

sprintf_s(call, "php.exe getPopularName.php
\"%S\"", name);
//cout << "<" << nonconstant_namecopy << ">";

//strcpy(call, name.c_str );
cout << "[call]" << call << "[/call]\n";
system(call);
// clean up
// delete [] nonconstant_namecopy;
cout << ".";
counter++;
}
}
cout << "finished processing " << counter << " latin names.";
filestr.close();

}

// checking if the list exists or not
bool listexists()
{
bool flag = false;
fstream filestr;
filestr.open("taxa.liste",ios::in);
if( filestr.is_open() )
{
flag=true;
}
filestr.close();
return flag;
}

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      07-09-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> hello NG,
>
> i am trying to do some syscalls and therefore i need to put some text
> together.
> its no problem as long i want to cout the text to display, but when i
> want to use it
> as parameter for functions the variable only passes NULL....
> the out commented code segments is my try to convert the static char*
> to char* so i can work with this one, but it does not work either...
> (compiles but app crashes then...)
>
> i load the names from the list to memory, and i
>
> my code
>
> #include "stdafx.h"
> //#include "fstream.h"
>
> bool listexists();
> void getPopularNames();
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> if (listexists())
> printf("taxa list file found!\n");
> else
> printf("error 404 - file [%S] not found\n");
> getPopularNames();
> // to get popular name call php file with latin name as
> parameter
> // php.exe getPopularName.php "latin name"
> return 0;
>
> }
>
> void getPopularNames()
> {
> ifstream filestr;
> std::string name;
> //char* name;
> std::string res;
> filestr.open("taxa.liste",ios::in);
> int counter = 0;
> cout << "processing";
> if( filestr.is_open() )
> {
> while( getline(filestr, name) ) {
> char call[1024];
> cout << name << '\n';
>
> //char *nonconstant_namecopy;
> //nonconstant_namecopy = new
> char[name.length() + 1];
> //strcpy_s(nonconstant_namecopy, name.length()
> +1, name.c_str());
>
> sprintf_s(call, "php.exe getPopularName.php
> \"%S\"", name);
> //cout << "<" << nonconstant_namecopy << ">";
>
> //strcpy(call, name.c_str );
> cout << "[call]" << call << "[/call]\n";
> system(call);
> // clean up
> // delete [] nonconstant_namecopy;
> cout << ".";
> counter++;
> }
> }
> cout << "finished processing " << counter << " latin names.";
> filestr.close();
>
> }
>
> // checking if the list exists or not
> bool listexists()
> {
> bool flag = false;
> fstream filestr;
> filestr.open("taxa.liste",ios::in);
> if( filestr.is_open() )
> {
> flag=true;
> }
> filestr.close();
> return flag;
> }


Since you are using C++ and not C, don't bother with sprintf_s. Just use
std::string.

Untested code:

while( getline(filestr, name) ) {
cout << name << '\n';

std::string call;
call = "php.exe getPopularName.php " + name;

cout << "[call]" << call << "[/call]\n";
system(call.c_str());
// system(const_cast<char*>( call.c_str() );
cout << ".";
counter++;
}

Note, logic of program wasn't looked at, just this funciton.

If a function is not const correct, sometimes you have to throw away the
const with const_cast. I think that most of windows calls are const
correct, however, so the const_cast won't be needed.



 
Reply With Quote
 
 
 
 
lumo2000@gmail.com
Guest
Posts: n/a
 
      07-10-2007
thanks for your reply!
i already thought noone has a clue about that (although this might
come quite often)
your untested code is working perfect!
thanks for your great help, the program is up and running now!

next dodo for me is multithreading, as the program waits for replies
most of the time.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments