string, char* and static char prob

Discussion in 'C++' started by lumo2000@gmail.com, Jul 9, 2007.

  1. Guest

    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;
    }
     
    , Jul 9, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Jul 9, 2007
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jul 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. Santa
    Replies:
    1
    Views:
    1,094
    Mark A. Odell
    Jul 17, 2003
  3. wwj
    Replies:
    24
    Views:
    2,522
    Mike Wahler
    Nov 7, 2003
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,066
    Ian Collins
    May 9, 2006
  5. Zigzag
    Replies:
    9
    Views:
    793
    mayeul.marguet
    May 23, 2012
Loading...

Share This Page