473,387 Members | 3,781 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,387 software developers and data experts.

Reading JPG File

Hi everyboy.

I trying to create a C promang in an AIX System to read JPG files but
when it read just the first 4 bytes when it found a DLE character (^P)
doesn't read anymore. I using fread function. Here a few lines:

char *sAnv;
....

sprintf(file_a, "%s/anverso.jpg", strDir);

if ((fImagen = fopen(file_a,"rb")) == NULL)
{
printf("ERROR: No se pudo abrir el archivo |%s|\n", file_a);
continue;
}

fseek(fImagen, 0, SEEK_END);
lLongitud = ftell(fImagen);
rewind(fImagen);

sAnv = (char *) malloc(sizeof(char) * lLongitud);

if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Tamao: (%d)\n",
lLongitud);
fin(-1);
}

fread(sAnv, 1, lLongitud, fImagen);

fclose(fImagen);
.....
free(sAnv);

Does anybody help with this problem.

P.D. Sorry about my english but it doesn't my native
language. :-)
Jun 27 '08 #1
6 4203
jc*************@gmail.com wrote:
I trying to create a C promang in an AIX System to read JPG files but
when it read just the first 4 bytes when it found a DLE character (^P)
doesn't read anymore. I using fread function. Here a few lines:
char *sAnv;
....
sprintf(file_a, "%s/anverso.jpg", strDir);
if ((fImagen = fopen(file_a,"rb")) == NULL)
{
printf("ERROR: No se pudo abrir el archivo |%s|\n", file_a);
continue;
}
fseek(fImagen, 0, SEEK_END);
lLongitud = ftell(fImagen);
rewind(fImagen);
This will rather likely work on your system, but it won't
hurt to take a look at question 19.12 in the C-FAQ.
sAnv = (char *) malloc(sizeof(char) * lLongitud);
Better drop the cast, it will only keep the compiler from
warning you if you forgot to include <stdlib.h(which in
turn can lead to nasty problems). You may also consider to
drop the 'sizeof(char)' since that's always 1 by definition
(and you use 1 under similar circumstances in the call of
fread()).
if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Taman?o: (%d)\n",
lLongitud);
fin(-1);
}
fread(sAnv, 1, lLongitud, fImagen);
How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).
P.D. Sorry about my english but it doesn't my native
language. :-)
Don't worry, neither is it mine;-)

Best regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #2
On 14 mayo, 18:18, j...@toerring.de (Jens Thoms Toerring) wrote:
jcasique.tor...@gmail.com wrote:
I trying to create a C promang in an AIX System to read JPG files but
when it read just the first 4 bytes when it found a DLE character (^P)
doesn't read anymore. I using fread function. Here a few lines:
char *sAnv;
....
sprintf(file_a, "%s/anverso.jpg", strDir);
if ((fImagen = fopen(file_a,"rb")) == NULL)
{
printf("ERROR: No se pudo abrir el archivo |%s|\n", file_a);
continue;
}
fseek(fImagen, 0, SEEK_END);
lLongitud = ftell(fImagen);
rewind(fImagen);

This will rather likely work on your system, but it won't
hurt to take a look at question 19.12 in the C-FAQ.
Thank you I made the change and it work's ok.
>
sAnv = (char *) malloc(sizeof(char) * lLongitud);

Better drop the cast, it will only keep the compiler from
warning you if you forgot to include <stdlib.h(which in
turn can lead to nasty problems). You may also consider to
drop the 'sizeof(char)' since that's always 1 by definition
(and you use 1 under similar circumstances in the call of
fread()).
if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Taman?o: (%d)\n",
lLongitud);
fin(-1);
}
fread(sAnv, 1, lLongitud, fImagen);

How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).
Well I set a variable with the result of fread something like this

size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);

then I print a message with two variables printf("Longitud: (%d)
Le*do (%d)\n",lLongitud,resultado); and are the same values

But when I print the sAnv variable it just have 4 bytes
printf("Bytes le*dos: |%s|\n",sAnv); --Byte le*dos: | Ï Ó|

Here I put a extract from JPG file
############################# B E G I N
################################################## ################
Ï Ó^PJFIF^A^A^A╚╚
█CP7<F<2PFAFZUP_x╚^┬xnnx§»╣^Ð╚
└^K^H^A9^B╝^A^A^Q ─^Y^A^A^A^A^A^A^A^B^C^D^E
─A^P^B^B^A^B^D^B^H^D^D^E^C^D
^B^C^A^B^Q^C^R!^D^S1AQ^Ð^T"2Raq^┴ÐS^Ê*ß#Bb 3Cr┴*4^┬ó$cs±^E5^UTd
┌^H^A^A?¤^Z─gZ$l
;¤q ^K&^└^┼`└^U ^├ß,DNy3^Ll^V^╔'░^├^█^X^F▄
Ù┐IND^]Xnh@t^Ka^Í^ã¦f^È^ãPGC╝▓3^ER*h^N*^H ^Pv="
Y4%^╔,]X┐^H░:^Ì▓─^ÃaÏ║®d´,DDI{È^
]óã▀^Y^C^B{¨^Y®./
z▀╩FuP^K^Z^GãPA^V^M^╔bK^G╝▓^R^GSRjZ╗^ U.á:^Ð^A^┴ÞD^B^H▒,DDDH^H"Ã
############################### E N D
################################################## ##################
>
P.D. Sorry about my english but it doesn't my native
language. :-)

Don't worry, neither is it mine;-)

Best regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #3
On 15 May, 04:27, jcasique.tor...@gmail.com wrote:
On 14 mayo, 18:18, j...@toerring.de (Jens Thoms Toerring) wrote:
jcasique.tor...@gmail.com wrote:
I trying to create a C promang in an AIX System to read JPG files but
when it read just the first 4 bytes when it found a DLE character (^P)
doesn't read anymore. I using fread function. Here a few lines:
char *sAnv;
....
sprintf(file_a, "%s/anverso.jpg", strDir);
if ((fImagen = fopen(file_a,"rb")) == NULL)
{
printf("ERROR: No se pudo abrir el archivo |%s|\n", file_a);
continue;
}
fseek(fImagen, 0, SEEK_END);
lLongitud = ftell(fImagen);
rewind(fImagen);
This will rather likely work on your system, but it won't
hurt to take a look at question 19.12 in the C-FAQ.

Thank you I made the change and it work's ok.


sAnv = (char *) malloc(sizeof(char) * lLongitud);
Better drop the cast, it will only keep the compiler from
warning you if you forgot to include <stdlib.h(which in
turn can lead to nasty problems). You may also consider to
drop the 'sizeof(char)' since that's always 1 by definition
(and you use 1 under similar circumstances in the call of
fread()).
if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Taman?o: (%d)\n",
lLongitud);
fin(-1);
}
fread(sAnv, 1, lLongitud, fImagen);
How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).

Well I set a variable with the result of fread something like this

size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);

then I print a message with two variables printf("Longitud: (%d)
Ledo (%d)\n",lLongitud,resultado); and are the same values

But when I print the sAnv variable it just have 4 bytes
printf("Bytes ledos: |%s|\n",sAnv); --Byte ledos: | |
The printf() will print up to the first NUL (0) byte in sAnv
This doesn't mean that fread() did not put more bytes
where sAnv points to.
Here I put a extract from JPG file
This isn't the right place to post binary files.
Jun 27 '08 #4
jc*************@gmail.com wrote:
On 14 mayo, 18:18, j...@toerring.de (Jens Thoms Toerring) wrote:
jcasique.tor...@gmail.com wrote:
if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Tamano: (%d)\n",
lLongitud);
fin(-1);
}
fread(sAnv, 1, lLongitud, fImagen);
How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).
Well I set a variable with the result of fread something like this
size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);
then I print a message with two variables printf("Longitud: (%d)
Le*do (%d)\n",lLongitud,resultado); and are the same values
That shows that fread worked exactly as expected, it did read
as many bytes as you told it to.
But when I print the sAnv variable it just have 4 bytes
printf("Bytes le*dos: |%s|\n",sAnv); --Byte le*dos: | Ï Ó|
That's to be expected. Using '%s' with printf() is for printing
strings. And a string is a char array where the data end with
the first '\0' character (and printf() stops at that position).
But what you read in from the file isn't a string, just an array
of chars with binary data. And, while binary data may contain
'\0' characters they definitely aren't meant to be treated like
strings, so you simply can't use printf() with '%s' to output
them (which normally would result in a lot of unreadable "noise"
anyway).

So the good news is that obviously reading in the data from the
file worked flawlessly. Question is why you want to print
them out. There will hardly be much human readable information
in there. And if you want to see what the binary data are you
will have to write a function that prints out the numerical
values of the binary data, that's what would make the most
sense.
Here I put a extract from JPG file
Which doesn't help at all since also most newsreaders can make
sense of binary data (and even may get them confused, so it's
not a goood idea to include binary data in a message).

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #5
On 15 mayo, 02:37, j...@toerring.de (Jens Thoms Toerring) wrote:
jcasique.tor...@gmail.com wrote:
On 14 mayo, 18:18, j...@toerring.de (Jens Thoms Toerring) wrote:
jcasique.tor...@gmail.com wrote:
if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Tamano: (%d)\n",
lLongitud);
fin(-1);
}
fread(sAnv, 1, lLongitud, fImagen);
How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).
Well I set a variable with the result of fread something like this
size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);
then I print a message with two variables printf("Longitud: (%d)
Ledo (%d)\n",lLongitud,resultado); and are the same values

That shows that fread worked exactly as expected, it did read
as many bytes as you told it to.
But when I print the sAnv variable it just have 4 bytes
printf("Bytes ledos: |%s|\n",sAnv); --Byte ledos: | |

That's to be expected. Using '%s' with printf() is for printing
strings. And a string is a char array where the data end with
the first '\0' character (and printf() stops at that position).
But what you read in from the file isn't a string, just an array
of chars with binary data. And, while binary data may contain
'\0' characters they definitely aren't meant to be treated like
strings, so you simply can't use printf() with '%s' to output
them (which normally would result in a lot of unreadable "noise"
anyway).

So the good news is that obviously reading in the data from the
file worked flawlessly. Question is why you want to print
them out. There will hardly be much human readable information
in there. And if you want to see what the binary data are you
will have to write a function that prints out the numerical
values of the binary data, that's what would make the most
sense.
Really I don't want to print the information on screen i do it just
to check that is working ok, i need to read the severals JPG files to
hold in one file in which i will include the long of each image, this
file will be transfer to another place to extract them with another
program and save into a database.
Here I put a extract from JPG file

Which doesn't help at all since also most newsreaders can make
sense of binary data (and even may get them confused, so it's
not a goood idea to include binary data in a message).
Ok i will never do it again!! ty
>
Regards, Jens
Regards
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #6
jc*************@gmail.com wrote:
Really I don't want to print the information on screen i do it just
to check that is working ok,
Fine. Then the only test you need to do is to check if you got
as many characters as you requested. Only if the return value
of fread() isn't identical to the number of bytes you asked it
read you're in trouble. Otherwise you can assume that everything
is fine.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #7

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

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
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:
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.