473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(namn[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!

Feb 5 '06 #1
8 7725
* 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::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 );

Any help Please!

--
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?
Feb 5 '06 #2
you cannot assign char* strings in c++. use

strcpy(doc[i],document);

instead.

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

Feb 5 '06 #3
*have to

Feb 5 '06 #4
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.

Feb 5 '06 #5
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++

Feb 5 '06 #6
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.

Feb 5 '06 #7
electrixnow schrieb:
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>
This two are C headers. The C++ equvalents are <cstdio> and <cstdlib>,
but you don't need them here.
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
Feb 5 '06 #8
In article <11*********************@o13g2000cwo.googlegroups. com>,
"electrixnow" <info...@charter.net> wrote:
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";
}
}
--
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.
Feb 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
5
by: Anton Pervukhin | last post by:
Hello! Imagine the situation you have a class that interprets the command line of the program you are executing. This class is written by third party and has a main function parse with arguments...
4
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
11
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about...
9
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc(...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.