Converting long to char* issue.

Nov 15, 2010 at 11:39pm
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

struct FILEKEY
{
long account_id;
long customer_id;
long product_id;
long bill_eff_dt;
};
struct TOTAL
{
long bill_qty;
double int_charge;
double final_charge;
double credit_charge;
};
struct FILE_ELEMENTS
{
FILEKEY key;
TOTAL total;
};

FILE_ELEMENTS elementFile;

void populateDataInBuffer ( ) {

ifstream inputfile;
int size = 50;
inputfile.open("firstFile.txt");

while ( !inputfile.eof() )
{
inputfile.getline( elementFile.key.account_id, size, "~");
}
inputfile.close();
}

int main()
{
populateDataInBuffer ( );
printf("elementFile.key.account_id\n");
return 0;
}

firstFile.txt
123456~12345654~23465893~11/04/2010~11.0~234.0~238.0~234.0
124456~12345654~23478893~11/05/2010~12.0~244.0~248.0~244.0
228456~34345654~23423893~11/06/2010~13.0~254.0~258.0~254.0
320456~56345654~23442893~11/07/2010~14.0~264.0~268.0~264.0
429456~78345654~23425893~11/08/2010~15.0~274.0~278.0~274.0

I have writen a very simple program for using getline() method.
I am failing to convert elementFile.key.account_id to char *.

Can you please help?

Thanks and Regards,
Aj
Nov 16, 2010 at 3:00am
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

You could try printf ("%ld\n",elementFile.key.account_id);.
I hope it works, I don't really use printf.
Last edited on Nov 16, 2010 at 3:01am
Nov 16, 2010 at 7:16am
actually, I am failing in type casting here.
inputfile.getline( elementFile.key.account_id, size, "~" );

elementFile.key.account_id is long type where as first parameter of getline has to be char *.
I am not getting what could be easy way of convert long to char *.

Thanks,
Aj
Nov 16, 2010 at 8:05am
I don't know your platform a long datatype is how big but for me it is 10 digits.

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main() {
  long test = 1234567890L;
  char strLong[1024];
  sprintf(strLong, "%ld", test);
  printf("%s\n",strLong);
  return 0;
}
Topic archived. No new replies allowed.