string, char* and static char prob

L

lumo2000

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;
}
 
J

Jim Langston

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

lumo2000

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top