By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

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

P: n/a
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: tr**@mailandnews.com
*/

#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[i], "-?") || !strcmp(argv[i], "-h"))
ShowHelp();
else if (!strcmp(argv[i], "-b")) swap_type = 0;
else if (!strcmp(argv[i], "-w")) swap_type = 1;
else if (!strcmp(argv[i], "-d")) swap_type = 2;
else if (!strcmp(argv[i], "-q")) swap_type = 3;
else
{
if ((fp = fopen(argv[i], "rb+")) == NULL)
fprintf(stderr, "swap: Failed to open file: %s\n",
argv[i]);
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[i]);
else
{
fread(buffer, sizeof(unsigned char), j, fp);
rewind(fp);
Swap(buffer, j, argv[i]);
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,
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"OZ" <ob*****@sandstorment.com> wrote in message
news:7b**************************@posting.google.c om...
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
Jul 22 '05 #2

P: n/a
OZ
PLEASE DISREGARD!!!!
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.