convert long to pointer to char array

This is a discussion on convert long to pointer to char array within the C++ Programming forums, part of the General Programming Boards category; Hi there, I have created a function which accepts a pointer to a char array (unsigned char *insert) and inserts ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    convert long to pointer to char array

    Hi there,

    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 (char *fname) at a specified position(unsigned long pos) with length(unsigned long insertlen). this appears to work ok. Does such a routine already exist ?

    Anyway, 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. For efficiency, I don't really want to split the long variable into 4 unsigned char's then to a pointer to an unsigned char array eg. *sequence in void main():

    Code:
    //this code causes the program to halt
    unsigned char *longvar=reinterpret_cast<unsigned char*>(wordpos); //wordpos=long var of file position of sequence
    insertintofile("testfile.dat", longvar, 10, sizeof(long));

    This is the insertintofile test program & function:

    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. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I don't understand well what is the problem, if you use a long pointer, it would just be ok, the pointer, once converted will be treated as a unsigned char * and will be written normally, if not, there probably something I missed.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    To write a long using your insertintofile function:
    Code:
    long num = 0x64636261;
    insertintofile("testfile.txt", reinterpret_cast<char *>(&num), 10, sizeof(long));
    Or you can take your original function, and make a new function to write a long:
    Code:
    void insertintofile(char *fname, long insert, unsigned long pos) {
    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);
    orig.write(reinterpret_cast<char *> (&insert), sizeof(long));
    temp.open("tempfile.dat", ios::in | ios::binary);
    while (temp.get(ch)) orig.put(ch);
    orig.close();
    temp.close();
    remove("tempfile.dat");
    }
    Last edited by swoopy; 09-25-2003 at 02:00 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void main ()

    Oh, and don't use void main(), use int main(). Read the programming faq here for why.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    Swoopy - you're a genius. I used your first suggestion - converting the long variable within the function call, like this:

    Code:
    unsigned long filepos=1684234849;
    insertintofile("testfile.txt", reinterpret_cast<unsigned char *>(&filepos), 10, sizeof(long));
    The above unsigned long value converts into 97,98,99,100 ie. when the file it is inserted into is read in Notepad as a textfile, the letters 'abcd' have effectively been inserted into file positions 10 to 13. I only used ascii values for testing purposes.

    Eg. text file:

    abcdefghijklmnopqrstuvwxyz

    becomes:

    abcdefghijabcdklmnopqrstuvwxyz

    This means I can keep my function unchanged - you see I want to use it not only to insert file positions represented by an unsigned long, but also to insert sequences (which may be from 2 bytes to thousands or more (in theory) ). The whole program I'm building is a compression program (ouch) - more for experience really!!

    Once again - thanks for your help.

    Gaz

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Gaz, I figured you probably wanted to leave the function unchanged. You indicated in the first post you would be inserting various data. I'm glad passing the long's address plus the casting worked. Have fun with the compression program .

    Swoopy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-03-2005, 12:15 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21