New to C++ and Need a little help with a public domain program

Discussion in 'C++' started by OZ, Jun 26, 2004.

  1. OZ

    OZ Guest

    Hi,

    I am new C++ and need a little help with a public domain program that
    is suppose to perform a byte swap. I am receiving the following error
    messages during the compile process with Microsoft C++ 2003.

    Here are the error messages and source code:


    (97) : error C2664: 'fgetpos' : cannot convert parameter 2 from 'void
    *' to 'fpos_t *'

    Conversion from 'void*' to pointer to non-'void' requires an explicit
    cast

    (99) : error C2440: '=' : cannot convert from 'void *' to 'unsigned
    char *'

    Conversion from 'void*' to pointer to non-'void' requires an explicit
    cast


    Here is the source:

    /*
    * swap Version 0.0
    * Bart Trzynadlowski, October 27, 2000
    * Public domain
    *
    * June 11, 2001:
    * - Updated contact information
    *
    *
    * This program swaps the bytes, words, doublewords, or quadwords in
    files.
    *
    * Usage: swap <-b files, -w files, -d files, -q files>
    * Options: -?,-h Show help
    * -b Byte swap (8-bit) [default]
    * -w Word swap (16-bit)
    * -d Doubleword swap (32-bit)
    * -q Quadword swap (64-bit)
    *
    * Contact Bart Trzynadlowski:
    * Email:
    */

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

    int swap_type = 0; /* 0=byte, 1=word, 2=dword, 3=qword */
    unsigned mask[] = { 0, 1, 3, 7 };
    unsigned data_sz[] = { 1, 2, 4, 8 };
    char *type_l[] = { "byte", "word", "doubleword", "quadword" };
    char *type_u[] = { "Byte", "Word", "Doubleword", "Quadword" };


    void Swap(unsigned char *buffer, unsigned size, char *file)
    {
    unsigned i, j;
    unsigned char d[8];

    if (size & mask[swap_type])
    {
    fprintf(stderr, "swap: File cannot be %s-swapped: %s (%d
    bytes)\n",
    type_l[swap_type], file, size);
    return;
    }

    printf("swap: %s-swapping file: %s (%d bytes)... ",
    type_u[swap_type],
    file, size);
    for (i = 0; i < size; i += (data_sz[swap_type] * 2))
    {
    for (j = 0; j < data_sz[swap_type]; j++)
    d[j] = buffer[i + j];
    for (j = 0; j < data_sz[swap_type]; j++)
    buffer[i + j] = buffer[i + j + data_sz[swap_type]];
    for (j = 0; j < data_sz[swap_type]; j++)
    buffer[i + j + data_sz[swap_type]] = d[j];
    }
    printf("OK\n");
    }

    void ShowHelp()
    {
    printf("swap Version 0.0 by Bart Trzynadlowski: Data-Swapping
    Utility\n");
    printf("Usage: swap <-b files, -w files, -d files, -q
    files>\n");
    printf("Options: -?,-h Show this help text\n");
    printf(" -b Swap bytes [default]\n");
    printf(" -w Swap words\n");
    printf(" -d Swap doublewords\n");
    printf(" -q Swap quadwords\n");
    exit(0);
    }

    int main(int argc, char **argv)
    {
    FILE *fp;
    unsigned char *buffer;
    unsigned i, j;

    if (argc <= 1)
    ShowHelp();

    setvbuf(stdout, NULL, _IONBF, NULL);

    for (i = 1; i < argc; i++)
    {
    if (!strcmp(argv, "-?") || !strcmp(argv, "-h"))
    ShowHelp();
    else if (!strcmp(argv, "-b")) swap_type = 0;
    else if (!strcmp(argv, "-w")) swap_type = 1;
    else if (!strcmp(argv, "-d")) swap_type = 2;
    else if (!strcmp(argv, "-q")) swap_type = 3;
    else
    {
    if ((fp = fopen(argv, "rb+")) == NULL)
    fprintf(stderr, "swap: Failed to open file: %s\n",
    argv);
    else
    {
    fseek(fp, 0, SEEK_END);
    ERROR HERE-> fgetpos(fp, (void *) &j);
    rewind(fp);
    ERROR HERE-> if ((buffer = calloc(j, sizeof(unsigned char))) ==
    NULL)
    fprintf(stderr, "swap: Failed to allocate %d bytes
    of "
    "memory for file: %s\n", j,
    argv);
    else
    {
    fread(buffer, sizeof(unsigned char), j, fp);
    rewind(fp);
    Swap(buffer, j, argv);
    fwrite(buffer, sizeof(unsigned char), j, fp);
    free(buffer);
    }
    fclose(fp);
    }
    }
    }

    setvbuf(stdout, NULL, _IOLBF, NULL);

    return 0;
    }

    Any information would would be greatly appreciated.

    Thanks,
    OZ, Jun 26, 2004
    #1
    1. Advertising

  2. "OZ" <> wrote in message
    news:...
    > Hi,
    >
    > I am new C++ and need a little help with a public domain program that
    > is suppose to perform a byte swap. I am receiving the following error
    > messages during the compile process with Microsoft C++ 2003.


    The program is C not C++. Make sure you call the file something.c not
    something.cpp and you should be OK.

    Since it is C not C++ take any further questions to news:comp.lang.c

    john
    John Harrison, Jun 26, 2004
    #2
    1. Advertising

  3. OZ

    OZ Guest

    PLEASE DISREGARD!!!!
    OZ, Jun 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,343
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    553
    SpamProof
    Oct 21, 2003
  3. Marc
    Replies:
    4
    Views:
    509
    Bob Gailer
    Oct 4, 2003
  4. New Programmer
    Replies:
    1
    Views:
    575
    John B. Matthews
    May 2, 2011
  5. Alfred
    Replies:
    2
    Views:
    93
    Dr J R Stockton
    May 27, 2007
Loading...

Share This Page