char* to string conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • electrixnow

    #1

    char* to string conversion

    I am reading in a comma delimited text file, with a document #,revision
    letter on each line.
    example:

    123,A
    456,B

    There are 19000 lines to this master document file. I will also be
    reading in a file that has about 200 lines that has the same structure
    as the first file. I need to compare File A and B and produce File C.
    The 123 document data is the same in A and B file, I am looking if the
    REVISION has been updated in the master document file A. If there is an
    update of the REVISION then I want to copy that line of File A,
    overwrite it in File B, and create file C with these lines that have
    been found as updates.

    The final program needs to take the data in file C and create a MS Word
    Document, email it, and print it. This program will run once a day
    after file A gets updated from a database query and act as an
    automated document ordering system.

    I am not the master of C by any means but I am trying my best to learn
    more. I am more use to scripting from the 80's.

    I am trying to use Visual C++ 2005 express

    I am getting stuck with the following code because I get the an error
    during compile when I try putting pointers into a new array that I have
    commented out.

    Here is the code that I am having problems with. Maybe I am going about
    it the wrong way.

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;


    #define MOVEX_QUERY "C:\\DWG_DATA\\ DOCUMENTS_movex .dat"

    int main(){
    char * document;
    char * edition;
    char * str1;
    char * next_token1;
    int i=0;
    string tmp;

    using std::ifstream;
    using std::cout;

    ifstream inf(MOVEX_QUERY );

    if (inf)
    {
    char namn[20000][30];
    char doc[20000][30];
    char edi[20000][30];
    while ((inf.getline(n amn[i], 30)) != NULL)++i;
    for (int i = 0; i < 5; ++i){
    str1 = namn[i];
    document = strtok_s( str1, " ,\t\n", &next_token1 );
    edition = strtok_s( NULL, " ,\t\n", &next_token1 );
    // doc[i] = document; // does'nt work
    // edi[i] = edition; // does'nt work

    if(document == edition) printf( "%s\n", document ); // does'nt work

    // printf( "%s\n", document );
    // printf( "%s\n", edition );
    // cout << i <<'\n';
    }

    }
    else
    {
    cout << "Could not open file\n";
    return 1;
    }
    cout << "PROCESSING COMPLETE\n";
    return 0;
    }

    This is only some test code trying to read in and do comparisons on
    variables. I was thinking that I could read in all of file A and B,
    then loop thru A for each record of B while building C.

    Any help Please!

  • Alf P. Steinbach

    #2
    Re: char* to string conversion

    * electrixnow:[color=blue]
    > I am reading in a comma delimited text file, with a document #,revision
    > letter on each line.
    > example:
    >
    > 123,A
    > 456,B
    >
    > There are 19000 lines to this master document file. I will also be
    > reading in a file that has about 200 lines that has the same structure
    > as the first file. I need to compare File A and B and produce File C.
    > The 123 document data is the same in A and B file, I am looking if the
    > REVISION has been updated in the master document file A. If there is an
    > update of the REVISION then I want to copy that line of File A,
    > overwrite it in File B, and create file C with these lines that have
    > been found as updates.[/color]

    std::map<int, char> bData = dataFrom( pathToB );
    std::ifstream a( pathToA );
    std::ofstream c( pathToC );

    while( !!a )
    {
    -- read one line from a
    -- split the line into document # and revision char
    -- check against bData
    -- if different revision char:
    -- update bData
    -- write line to c
    }
    assert( a.eof() );
    save( bData, pathToB );

    [color=blue]
    > Any help Please![/color]


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Konfusius

      #3
      Re: char* to string conversion

      you cannot assign char* strings in c++. use

      strcpy(doc[i],document);

      instead.

      if you use strcpy() you also have include <cstring>.

      Comment

      • Konfusius

        #4
        Re: char* to string conversion

        *have to

        Comment

        • electrixnow

          #5
          Re: char* to string conversion

          Sorry but I am a newbie and I searched for usage on std::map and found
          nothing.
          Can you give me an exact example of

          std::map<int, char> bData = dataFrom( pathToB ); // open file for
          read & write
          std::ifstream a( pathToA ); // open file for read
          std::ofstream c( pathToC ); // open file for write

          while( !!a ) // don't understand the double !! // is it looking for
          NULL or EOF
          {
          -- read one line from a // I can use inf.getline
          -- split the line into document # and revision char // should I
          use strtok_s
          -- check against bData // do I check using strings or pointers,
          do I cast? how
          -- if different revision char:
          -- update bData
          -- write line to c
          }
          assert( a.eof() ); // are you checking for eof again
          save( bData, pathToB ); // could'nt find save usage in help

          Sorry I am not an expert, but I am new and this task seems simple but
          has been
          frustraiting. I was hung up on cannot convert from 'char *' to 'char'

          I understand the flow you created and it's on the money. I wish I had
          some good examples to study. I have been looking all over and have not
          found any good sites with lots of simple VC++ examples.

          Please help.

          Comment

          • electrixnow

            #6
            Re: char* to string conversion

            if I use strcpy(doc[i],document); I get a message from compile that
            says it's unsafe and to use strcpy_s instead. when I run the program I
            get a stack overflow with strcpy
            are you using VS C++

            Comment

            • electrixnow

              #7
              Re: char* to string conversion

              Sorry but I am a newbie and I searched for usage on std::map and found
              nothing. Can you give me an exact example of:

              std::map<int, char> bData = dataFrom( pathToB ); // open file r/w
              std::ifstream a( pathToA ); // open file for read
              std::ofstream c( pathToC ); // open file for write

              while( !!a ) // why 2 ! // looking for NULL or EOF
              {
              -- read one line from a // I can use inf.getline
              -- split the line into doc # and rev char // use strtok_s ?
              -- check against bData // use string or pointer ?
              -- if different revision char:
              -- update bData
              -- write line to c
              }
              assert( a.eof() ); // are you checking for eof again
              save( bData, pathToB ); // could'nt find save usage in help

              Sorry I am not an expert, but I am new and this task seems simple but
              has been
              frustraiting. I was hung up on cannot convert from 'char *' to 'char'

              I understand the flow you created and it's on the money. I wish I had
              some good examples to study. I have been looking all over and have not
              found any good sites with lots of simple VC++ examples.

              Please help.

              Comment

              • Thomas J. Gritzan

                #8
                Re: char* to string conversion

                electrixnow schrieb:[color=blue]
                > I am not the master of C by any means but I am trying my best to learn
                > more. I am more use to scripting from the 80's.
                >
                > I am trying to use Visual C++ 2005 express
                >
                > I am getting stuck with the following code because I get the an error
                > during compile when I try putting pointers into a new array that I have
                > commented out.[/color]

                Your are mixing C and C++ here.
                Don't use pointers and arrays, if you don't need to. Use strings and
                vectors, they are much easier to use once you know them.

                You can't assign and compare arrays of chars:

                char text[100] = "Some text";
                char text2[100] = "some other text";

                if (text == text2) { ... } // does not work
                text = text2; // does not work

                Use this:

                string text = "text";
                string text2 = "other";
                [color=blue]
                > #include <stdio.h>
                > #include <stdlib.h>[/color]

                This two are C headers. The C++ equvalents are <cstdio> and <cstdlib>,
                but you don't need them here.
                [color=blue]
                > using namespace std;
                >
                >
                > #define MOVEX_QUERY "C:\\DWG_DATA\\ DOCUMENTS_movex .dat"[/color]

                const string movex_query = "C:\\DWG_DATA\\ DOCUMENTS_movex .dat";
                [color=blue]
                > int main(){
                > char * document;
                > char * edition;
                > char * str1;
                > char * next_token1;[/color]

                replace char* with string.
                [color=blue]
                > int i=0;
                > string tmp;
                >
                > using std::ifstream;
                > using std::cout;
                >
                > ifstream inf(MOVEX_QUERY );[/color]

                ifstream inf(movex_query .text());
                [color=blue]
                >
                > if (inf)
                > {
                > char namn[20000][30];
                > char doc[20000][30];
                > char edi[20000][30];[/color]

                vector<string> namn;
                and so on...

                Read about vector and string. string has some usefull member functions.

                Thomas

                Comment

                • Daniel T.

                  #9
                  Re: char* to string conversion

                  In article <1139124820.291 022.88670@o13g2 000cwo.googlegr oups.com>,
                  "electrixno w" <info...@charte r.net> wrote:
                  [color=blue]
                  > I am reading in a comma delimited text file, with a document #,revision
                  > letter on each line.
                  > example:
                  >
                  > 123,A
                  > 456,B
                  >
                  > There are 19000 lines to this master document file. I will also be
                  > reading in a file that has about 200 lines that has the same structure
                  > as the first file. I need to compare File A and B and produce File C.
                  > The 123 document data is the same in A and B file, I am looking if the
                  > REVISION has been updated in the master document file A. If there is an
                  > update of the REVISION then I want to copy that line of File A,
                  > overwrite it in File B, and create file C with these lines that have
                  > been found as updates.[/color]

                  Do the lines have to maintain their order?
                  [color=blue]
                  > The final program needs to take the data in file C and create a MS Word
                  > Document, email it, and print it. This program will run once a day
                  > after file A gets updated from a database query and act as an
                  > automated document ordering system.
                  >
                  > I am not the master of C by any means but I am trying my best to learn
                  > more. I am more use to scripting from the 80's.
                  >
                  > I am trying to use Visual C++ 2005 express
                  >
                  > I am getting stuck with the following code because I get the an error
                  > during compile when I try putting pointers into a new array that I have
                  > commented out.
                  >
                  > Here is the code that I am having problems with. Maybe I am going about
                  > it the wrong way.[/color]

                  Maybe this will help:

                  ifstream afile( "document_n ame" );
                  string str;
                  while ( getline( afile, str ) ) {
                  try {

                  // lexical_cast can be found in the boost library

                  string::size_ty pe comma = str.find( ',' );
                  int doc_num = lexical_cast<in t>( str.substr( 0, comma ) );
                  char rev = str.substr( comma + 1 ).at( 0 );

                  // at this point the line has been parsed.
                  // do what you want to it.
                  }
                  catch (...) {
                  cerr << "bad data found in document";
                  }
                  }


                  --
                  Magic depends on tradition and belief. It does not welcome observation,
                  nor does it profit by experiment. On the other hand, science is based
                  on experience; it is open to correction by observation and experiment.

                  Comment

                  Working...