GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Sep-2003, 07:48
realpopeye realpopeye is offline
New Member
 
Join Date: Sep 2003
Location: Glasgow, UK
Posts: 2
realpopeye is an unknown quantity at this point

convert long to pointer to char


Hello all,

I have created a function which accepts a pointer to a char array (unsigned char *insert) and inserts the corresponding byte values into a file at a specified position(unsigned long pos) with length(unsigned long insertlen). this appears to work ok. Does such a routine already exist ?

this is part of a much larger program to build a database of repeating words/sequences in a file and their corresponding file positions. . . . . . .

However, I also need to insert a long into positions within the file, so I tried to convert it to a pointer to a char array, which is where errors occur, either in conversion or when *longvar is passed into the insertintofile function:

unsigned char *longvar=reinterpret_cast<unsigned char*>(wordpos); //wordpos=long var of file position of sequence

This is the insertintofile function:
CPP / C++ / C Code:
#include <iostream> //used for console input/output
#include <fstream> //used for file input/output
#include <io.h> //used to enable filelength checking
#include <conio.h> //used to enable getch() function

using namespace std;

void insertintofile(char *fname, unsigned char *insert, unsigned long pos, unsigned long insertlen);

void main () {
	unsigned char *sequence=new unsigned char[4];
	*sequence=97;
	*(sequence+1)=98;
	*(sequence+2)=99;
	*(sequence+3)=100;
	insertintofile("testfile.txt", sequence, 10, 4);
}

void insertintofile(char *fname, unsigned char *insert, unsigned long pos, unsigned long insertlen) {
	fstream orig;
	fstream temp;
	orig.open(fname, ios::in | ios::out | ios::binary);
	temp.open("tempfile.dat", ios::out | ios::trunc | ios::binary);
	orig.seekg(pos, ios::beg);
	char ch=0;
	while (orig.get(ch)) temp.put(ch);
	temp.close();
	orig.clear();
	orig.seekp(pos, ios::beg);
	for (unsigned long insertpos=0; insertpos<insertlen; insertpos++) {
	int belter=*(insert+insertpos);
		cout << "belter=" << belter;
		orig.put(belter);
	}
	temp.open("tempfile.dat", ios::in | ios::binary);
	while (temp.get(ch)) orig.put(ch);
	orig.close();
	temp.close();
	remove("tempfile.dat");
}
Any help would be appreciated,
thanks
Gary
  #2  
Old 25-Sep-2003, 07:26
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Hey Gary, welcome to the boards.

Sounds like you need to convert a long int to a string (you're sticking to C stylee). Sounds like a ltoa() function, if only it was in the C standard lib!

However, the good networthy folk have made reinventing the wheel an infrequent task. Here is a good looking ltoa() function, courtesy of ftp.osterman.com

CPP / C++ / C Code:
/*
**  LTOA.C
**
**  Converts a long integer to a string.
**
**  Copyright 1988-90 by Robert B. Stout dba MicroFirm
**
**  Released to public domain, 1991
**
**  Parameters: 1 - number to be converted
**              2 - buffer in which to build the converted string
**              3 - number base to use for conversion
**
**  Returns:  A character pointer to the converted string if
**            successful, a NULL pointer if the number base specified
**            is out of range.
*/

#include <stdlib.h>
#include <string.h>

#define BUFSIZE (sizeof(long) * 8 + 1)

char *ltoa(long N, char *str, int base)
{
      register int i = 2;
      long uarg;
      char *tail, *head = str, buf[BUFSIZE];

      if (36 < base || 2 > base)
            base = 10;                    /* can only use 0-9, A-Z        */
      tail = &buf[BUFSIZE - 1];           /* last character position      */
      *tail-- = '\0';

      if (10 == base && N < 0L)
      {
            *head++ = '-';
            uarg    = -N;
      }
      else  uarg = N;

      if (uarg)
      {
            for (i = 1; uarg; ++i)
            {
                  register ldiv_t r;

                  r       = ldiv(uarg, base);
                  *tail-- = (char)(r.rem + ((9L < r.rem) ?
                                  ('A' - 10L) : '0'));
                  uarg    = r.quot;
            }
      }
      else  *tail-- = '0';

      memcpy(head, ++tail, i);
      return str;
}

Haven't tested it myself, so finger crossed!

GF
  #3  
Old 26-Sep-2003, 11:22
realpopeye realpopeye is offline
New Member
 
Join Date: Sep 2003
Location: Glasgow, UK
Posts: 2
realpopeye is an unknown quantity at this point
Hi Garth,

thanks for your reply. The ltoa function seems to use an incredible amount of code (much of which I don't really understand) to do what I thought was a fairly simple reassignment.

Anyway, all I needed to do was when converting the original unsigned long, refer to it by reference ie.
CPP / C++ / C Code:
unsigned long wordpos=12345678;
unsigned char*longvar=new unsigned char[4];
longvar=reinterpret_cast<unsigned char*>(&wordpos);
insertintofile("testfile.txt", longvar, 10, 4);

This now does exactly what I want.

Is my insertintofile function 'acceptable' or do you know of a more efficient method ?

Gary
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i/o: set value x (long) before it is used by a function gmn C Programming Language 1 18-Nov-2003 02:12
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 12:33
char to operator calculus87 C++ Forum 3 04-Sep-2003 11:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.