char* to string conversion

E

electrixnow

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(namn, 30)) != NULL)++i;
for (int i = 0; i < 5; ++i){
str1 = namn;
document = strtok_s( str1, " ,\t\n", &next_token1);
edition = strtok_s( NULL, " ,\t\n", &next_token1);
// doc = document; // does'nt work
// edi = 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!
 
A

Alf P. Steinbach

* electrixnow:
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.

std::map<int, char> bData = dataFrom( pathToB );
std::ifstream a( pathToA );
std::eek:fstream 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 );
 
K

Konfusius

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

strcpy(doc,document);

instead.

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

electrixnow

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::eek:fstream 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.
 
E

electrixnow

if I use strcpy(doc,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++
 
E

electrixnow

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::eek:fstream 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.
 
T

Thomas J. Gritzan

electrixnow said:
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.

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";
#include <stdio.h>
#include <stdlib.h>

using namespace std;


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

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

replace char* with string.
int i=0;
string tmp;

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

ifstream inf(MOVEX_QUERY);

ifstream inf(movex_query.text());
if (inf)
{
char namn[20000][30];
char doc[20000][30];
char edi[20000][30];

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

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

Thomas
 
D

Daniel T.

"electrixnow said:
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.

Do the lines have to maintain their order?
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.

Maybe this will help:

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

// lexical_cast can be found in the boost library

string::size_type comma = str.find( ',' );
int doc_num = lexical_cast<int>( 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";
}
}
 

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,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top