473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to display binary file contents if it is stored in an integer array?

Dear all,

I am writing a client-server application and the client should upload
a file to the server, then the server should display the content of
the file in stdout.

Because I have to deal with binary files, I cannot use character
strings, so I chose integer array instead.

But the problem is that how can I display them in stdout? I tried "%c"
and "%d", but neither will work.

Client side:

int fdin;
int *up_file;
struct stat buf;

fdin = open(file, O_RDONLY);
fstat(fdin, &buf);
up_file = (int *)malloc(buf.st_size * sizeof(int));
up_file = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fdin, 0)

Server Side:

for (int i = 0; i < iarray_len; i++)
printf("%d", iarray_val[i]);

The uploading procedure will be handled by Sun RPC.

Thanks!

May 1 '07 #1
9 4197
Well, what I actually mean is that the client is going to upload a
file to the server and the server will store the file content in the
memory. Later, the client will fetch the file from the server and
display it in stdout.

So the test script will be

../client add server test.pdf
../client fetch server test_2.pdf
diff test.pdf test_2.pdf

So in my case, neither printf("%c") nor printf("%d") will work ...

int i;
for (i=0; i < integer_array_len; i++)
printf("%c", integer_array_val[i]);

On 5ÔÂ1ÈÕ, ÏÂÎç3ʱ34·Ö, loudking <loudk...@gmail.comwrote:
Dear all,

I am writing a client-server application and the client should upload
a file to the server, then the server should display the content of
the file in stdout.

Because I have to deal with binary files, I cannot use character
strings, so I chose integer array instead.

But the problem is that how can I display them in stdout? I tried "%c"
and "%d", but neither will work.

Client side:

int fdin;
int *up_file;
struct stat buf;

fdin = open(file, O_RDONLY);
fstat(fdin, &buf);
up_file = (int *)malloc(buf.st_size * sizeof(int));
up_file = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fdin, 0)

Server Side:

for (int i = 0; i < iarray_len; i++)
printf("%d", iarray_val[i]);

The uploading procedure will be handled by Sun RPC.

Thanks!

May 1 '07 #2
loudking wrote:
I am writing a client-server application and the client should upload
a file to the server, then the server should display the content of
the file in stdout.
(The details of the client-server interaction are off-topic here.)
Because I have to deal with binary files, I cannot use character
strings, so I chose integer array instead.
Why not a character array?
But the problem is that how can I display them in stdout? I tried "%c"
and "%d", but neither will work.
Both will work (for some value of "work") /if you do them right/.

What do you expect to display on stdout? The characters, the decimal
representation of their values, or what?

What actually happened?
Client side:

int fdin;
int *up_file;
struct stat buf;

fdin = open(file, O_RDONLY);
fstat(fdin, &buf);
Non-standard magic; I can't speak to it.
up_file = (int *)malloc(buf.st_size * sizeof(int));
You don't need, and should not use, that cast, if you're programming
in C.

Assuming that `fstat` puts the file size in `buf.st_size`, you
should realise that you're probably allocating `sizeof(int)`
times too much space.

You should check that `malloc` hasn't returned null.
up_file = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fdin, 0)
More unspeakable magic. It doesn't seem to have anything to do
with `up_file`. Is that deliberate?
Server Side:
for (int i = 0; i < iarray_len; i++)
printf("%d", iarray_val[i]);
Presumably some more magic has copied the file mentioned above
into `iarray_val`, whose type you have not told us.

If -- I say "if" because there is so much magic buzzing around
my ears are dizzy -- `iarray_val` (a name from hell, surely) has
the bytes of the file you mention, you're going to be printing
out (the decimal representation of) `sizeof(int)` bytes at a
time. So you'll see numbers like 12386359126 rather than 42
and 96.
The uploading procedure will be handled by Sun RPC.
Not just magic -- sorcery!

--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 1 '07 #3
On 1 May, 14:34, loudking <loudk...@gmail.comwrote:

this may be off-topic to comp.lang.c. comp.programming may be better.
I am writing a client-server application and the client should upload
a file to the server, then the server should display the content of
the file in stdout.

Because I have to deal with binary files, I cannot use character
strings, so I chose integer array instead.
so on the server you load the data into an int[]. Consider using
unsigned char[].

But the problem is that how can I display them in stdout? I tried "%c"
and "%d", but neither will work.
what does "will work" mean? Did it display anything? Or just not what
you
expected.
>
Client side:

int fdin;
int *up_file;
struct stat buf;

fdin = open(file, O_RDONLY);
fstat(fdin, &buf);
up_file = (int *)malloc(buf.st_size * sizeof(int));
up_file = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fdin, 0)
lots of not standard C stuff there.

Server Side:

for (int i = 0; i < iarray_len; i++)
printf("%d", iarray_val[i]);

The uploading procedure will be handled by Sun RPC.
would %x (hex) in the printf() solve your problem?
--
Nick Keighley
May 1 '07 #4
I finally realize that I should use write(STDOUT_FILENO) instead of
printf !!

Thank you for your messages!

May 1 '07 #5
In article <11**********************@q75g2000hsh.googlegroups .com>,
loudking <lo******@gmail.comwrote:
>I finally realize that I should use write(STDOUT_FILENO) instead of
printf !!
It seems unlikely that this is really the solution. Apart from
buffering and the text/binary distinction, there is little difference
between using C's fwrite() and Unix's write(). Buffering can be
defeated with fflush(), and on a Unix system there's no difference
between text and binary mode.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 1 '07 #6
loudking wrote:
Well, what I actually mean is that the client is going to upload a
file to the server and the server will store the file content in the
memory. Later, the client will fetch the file from the server and
display it in stdout.

So the test script will be

./client add server test.pdf
./client fetch server test_2.pdf
diff test.pdf test_2.pdf

So in my case, neither printf("%c") nor printf("%d") will work ...
You keep saying that, but you don't say why you think it's true.
int i;
for (i=0; i < integer_array_len; i++)
printf("%c", integer_array_val[i]);
Did you read Chris-Dollin-at-HP's reply? Because he points out that
if you write the wrong thing out, you'll get the wrong answer.

Try a simple test case first, for heavens sake. Make sure you can
write out zero bytes, one byte, two, three, four; five bytes, six
bytes, seven bytes, eight. At the moment your problem is nothing to
do with client-server communication: it's about rendering data to
the output.

Try some data like:

unsigned char someData[] =
{ 1, 2, 0, 17, 127, 128, 254, 255, 31, 32 };

and write it out with loops like:

for (i = 0; i < sizeof (someData); i += 1)
writeOutMyData( &someData[0], &someData[i] );

--
Signed And Sealed Hedgehog
Meaning precedes definition.

May 1 '07 #7
loudking wrote:
Dear all,

I am writing a client-server application and the client should upload
a file to the server, then the server should display the content of
the file in stdout.

Because I have to deal with binary files, I cannot use character
strings, so I chose integer array instead.
char is an integer type. You are guaranteed that you can store any
integral value in the range 0...255 in an unsigned char, and -127...127
in a signed char. Plain char could be either. There is nothing about a
stream being 'binary' that keeps you from using char arrays.

But the problem is that how can I display them in stdout? I tried "%c"
and "%d", but neither will work.
You display them as they are meant to be interpreted. "Neither will
work" is a useless description of your problem.
>
Client side:

int fdin;
int *up_file;
struct stat buf;
"struct stat" is, of course, not provided by any standard C header or
library.
fdin = open(file, O_RDONLY);
"open" is, of course, not provided by any standard C header or library.
Nor is "O_RDONLY". The standard function is fopen, when the type of
fdin would be "FILE *", not "int". The standard attempt to open fdin
for binary input would be
#include <stdio.h>
{
FILE *fdin;
fdin = fopen(file, "rb");
/* error checking here */
}
Note the "b" for binary. Perhaps your non-standard "O_RDONLY" needs an
additional flag for binary input.

fstat(fdin, &buf);
up_file = (int *)malloc(buf.st_size * sizeof(int));
Had you followed this newsgroup at all before posting, expected behavior
in all newsgroups, you would know that casting the return value is
unneeded and poor programming practice.
#include <stdlib.h>
{
T* up_file; /* char T is the type of the desired array */
up_file = malloc(buf.st_size * sizeof *up_file);
/* error checking here */
}
up_file = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, fdin, 0)
"mmap" is, of course, not provided by any standard C header or library.
Nor are "PROT_READ" or "MAP_SHARED".

Server Side:

for (int i = 0; i < iarray_len; i++)
printf("%d", iarray_val[i]);
What doesn't work (other than the declaration of 'int' here if your
compiler is a C89 one)? You store values in an int array, you fprintf
the contents of that int array as ints. If that doesn't "work" your
implementation is hopelessly broken.

I suspect that the reality is that you really haven't thought through
your problem enough to know what correct behavior might be.

The uploading procedure will be handled by Sun RPC.
Who cares?
May 1 '07 #8
loudking wrote:
I finally realize that I should use write(STDOUT_FILENO) instead of
printf !!
If "write(STDOUT_FILENO)" is right, then you are in the wrong newsgroup.
That is not standard C and is an implementation-provided invasion of
the program's namespace.
May 1 '07 #9
loudking <lo******@gmail.comwrites:
[...]
for (int i = 0; i < iarray_len; i++)
printf("%d", iarray_val[i]);
[...]

Successive printfs will print successive values from your array with
no blanks between them. For example, if the values in your array are

123, 234, -987654321, 0

then your output will be:

123234-9876543210

The output will be identical if your first two numbers are any of:

1, 23234
12, 3234
1232, 34
12323, 4

Perhaps that's what you meant when you told us that it doesn't work.
Tell us *how* it doesn't work would have been extremely helpful.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
3
by: nguser3552 | last post by:
Hello Everyone, I have a problem I can't surmount, anything is gravy at this point. I need to be able to read any type of file .ext (mov,mpeg,mp3,etc) in binary format. I can do this in C, but ...
0
by: RN | last post by:
Hi everyone, First please let me explain. I am attempting to store pdf files in an MS Access DB (2000) and I have written a subroutine to do this. My code seems to work perfectly (see code...
2
by: pozze | last post by:
Hi, I need to display images and other record information retrieved from an SQL 2005 database in a datagrid on a web page. I'm coding in VB .net I have recently changed over from VB ASP and i'm...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.