1 Replies - 701 Views - Last Post: 30 November 2008 - 06:02 PM

#1 heckyj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-November 08

Files

Posted 30 November 2008 - 12:24 PM

/* I have been working with this program for a while. It must read a file from /home/smccasli/pub/cis160/chinca.jpg here is teh file it must read. I compile and I get no errors, but when I run it keeps telling the same message ..... no such file or directory
What am I missing?

Can some one help me please?

#include <stdio.h>
#include <errno.h>
#define MAXBUF 512

struct dEntry
{
unsigned short tag;
unsigned short type;
unsigned long count;
unsigned long offset;
};
typedef struct dEntry Entry;
short toshort(int bOrder, unsigned char a[2]);
long tolong(int bOrder, unsigned char a[4]);

int main(int argc, char **argv)
{
FILE *infile;
char filename[256];
unsigned char buf[MAXBUF];
int TIFFoffset = 12;
int IFDoffset;
int IFDEntryPtr;
int entryCount;
Entry entry;
short int byteOrder;
int i;
printf(" Enter file you want to read: ");
scanf("%255c",filename);
/* Open the file fountain.jpg */
infile = fopen(filename,"rb");
if (infile == NULL)
{
fprintf(stderr,"Error opening %s: ",filename);
perror("");
exit(1);
}
/* Find the byte order of fountain.jpg */
fseek(infile, TIFFoffset, SEEK_SET);
fread(&byteOrder, sizeof(byteOrder), 1, infile);
if (byteOrder = 0x4949)
byteOrder = 0;
else if (byteOrder = 0x4D4D)
byteOrder = 1;
else
{
fprintf(stderr,"Error in %s: undefined byte order\n",
filename);
exit(1);
}
/* find first IFD */
fseek(infile, TIFFoffset+4, SEEK_SET);
fread(buf, 4, 1, infile);
IFDoffset = tolong(byteOrder, buf);
/* find IFD entry count */
fseek(infile, TIFFoffset + IFDoffset, SEEK_SET);
fread(buf, 2, 1, infile);
entryCount = toshort(byteOrder, buf);
/* Find tag 10E by searching through entrys
in first IFD table */
IFDEntryPtr = TIFFoffset + IFDoffset + 2;
for (i = 0; i < entryCount; i++)
{
fseek(infile, IFDEntryPtr, SEEK_SET);
fread(buf, 1, 12, infile);
IFDEntryPtr += 12;
entry.tag = toshort(byteOrder, buf);
if (entry.tag != 0x10E)
continue; /* start next iteration of for loop */
entry.type = toshort(byteOrder, buf + 2);
entry.count = tolong(byteOrder, buf + 4);
entry.offset = tolong(byteOrder, buf + 8);
if (entry.type != 2)
{
fprintf(stderr, "Entry %d not ASCII string\n", i);
exit(1);
}
/* for string, offset is where string is stored,
count is length, including NULL terminator */
/* read and print string */
fseek(infile, TIFFoffset + entry.offset, SEEK_SET);
fread(buf, 1, entry.count, infile);
printf("%s\n", buf);
break;
}
if (i >= entryCount)
fprintf(stderr, "tag 10E not found\n");
}


short toshort(int bOrder, unsigned char a[2])
/* bOrder == 0 little-endian (49) */
/* bOrder == 1 big-endian (4D) */
/* returns byte string converted */
{
short x;
switch(bOrder)
{
case 0: x = a[1] << 8 | a[0]; break;
case 1: x = a[0] << 8 | a[1]; break;
}
return x;
}

long tolong(int bOrder, unsigned char a[4])
/* bOrder == 0 little-endian (49) */
/* bOrder == 1 big-endian (4D) */
/* returns byte string converted */
{
int x = 0, i;
switch(bOrder)
{
case 0:
for (i = 3; i >= 0; i--)
x = x<<8 | a[i];
break;
case 1:
for (i = 0; i < 4; i++)
x = x<<8 | a[i];
break;
}
return x;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Files

#2 TonicX57  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 84
  • Joined: 13-September 08

Re: Files

Posted 30 November 2008 - 06:02 PM

You should post this in the board for this particular language, which looks like C to me? Also, it works a lot better for everyone if you use [code] tags around your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1