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

O

OZ

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: (e-mail address removed)
*/

#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,
 
J

John Harrison

OZ said:
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
john
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top