Why can't use fgetc() in SWITCH with CASE?

K

kuoyang.hu

I write a program and want to get the byte 30 & 31 from file4 (file4 name came from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the final output is show "printf("Check point 1 ...\n");", I don't know Why?

char IWHi, IWLo; //I tried use int IWHi, IWLo; same result
int ImgLenHi, ImgLenLo;
int FCount = 1, R200=0, R300=0, Rot=0;
int Res;
FILE *file3 = fopen(FN_OK,"r");
if ( file3 != NULL )
{
printf ("Found : %s\n",FN_OK);
fclose(file3);
for (FCount; FCount <=3; FCount++)
{
switch (FCount)
{
// case (2):
//
// break;
// case (3):
//
// break;
// case (4):
//
// break;
default:
strcat (Ext_001,FN_999);
strcat (Ext_001,"_001.tif");
printf("getfileHHMMSS_001.tif = %s\n",Ext_001);
FILE *file4 = fopen(Ext_001,"r");
if ( file4 != NULL )
{
printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);
if (fseek(file4,0,SEEK_SET) == 0)
{
if (fseek(file4,30,SEEK_SET) == 0)
{
printf("Check point 1 ...\n");
IWLo = fgetc(file4);
//printf("IWLo = %X",ImgWidthLo);
fclose(file4);
}
}
fclose(file4);
}
break;
}
}

return 0;
}
 
B

BartC

I write a program and want to get the byte 30 & 31 from file4 (file4 name
came from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the
final output is show "printf("Check point 1 ...\n");", I don't know Why?

(I've had to alter the indents which were causing some grief to my email
program.)
FILE *file4 = fopen(Ext_001,"r");
if ( file4 != NULL )
{
printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);
if (fseek(file4,0,SEEK_SET) == 0)
{
if (fseek(file4,30,SEEK_SET) == 0)
{
printf("Check point 1 ...\n");
IWLo = fgetc(file4);
//printf("IWLo = %X",ImgWidthLo);

What happens when you uncomment this line?
fclose(file4);
}
}
fclose(file4);

You appear to be closing the file a second time here (once it's been closed
above)
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 2013å¹´11月17日星期日UTC+8下åˆ2時49分39秒寫é“:
I write a program and want to get the byte 30 & 31 from file4 (file4 namecame from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the final output is show "printf("Check point 1 ...\n");", I don't know Why?



char IWHi, IWLo; //I tried use int IWHi, IWLo; same result

int ImgLenHi, ImgLenLo;

int FCount = 1, R200=0, R300=0, Rot=0;

int Res;

FILE *file3 = fopen(FN_OK,"r");

if ( file3 != NULL )

{

printf ("Found : %s\n",FN_OK);

fclose(file3);

for (FCount; FCount <=3; FCount++)

{

switch (FCount)

{

// case (2):

//

// break;

// case (3):

//

// break;

// case (4):

//

// break;

default:

strcat (Ext_001,FN_999);

strcat (Ext_001,"_001.tif");

printf("getfileHHMMSS_001.tif = %s\n",Ext_001);

FILE *file4 = fopen(Ext_001,"r");

if ( file4 != NULL )

{

printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);

if (fseek(file4,0,SEEK_SET) == 0)

{

if (fseek(file4,30,SEEK_SET) == 0)

{

printf("Check point 1 ...\n");

IWLo = fgetc(file4);

//printf("IWLo = %X",ImgWidthLo);

fclose(file4);

}

}

fclose(file4);

}

break;

}

}



return 0;

}

Hi Bart,

1. If I comment "IWLo = fgetc(file4);", the program could normally end.
2. If I comment or uncomment "fclose(file4);" under "//printf("IWLo = %X",ImgWidthLo); I got same fault result.
3. I checked several times, the program is terminated when DOS windows showed "Check point 1 ..." then Win7 show "error windows and try find suitable resolution", unfortunately I have to interrupt it by manually.

Kuoyang
 
B

BartC

1. If I comment "IWLo = fgetc(file4);", the program could normally end.
2. If I comment or uncomment "fclose(file4);" under "//printf("IWLo =
%X",ImgWidthLo); I got same fault result.
3. I checked several times, the program is terminated when DOS windows
showed "Check point 1 ..." then Win7 show "error windows and try find
suitable resolution", unfortunately I have to interrupt it by manually.

OK, find out exactly what the name of the file is. Hard-code that into the
fopen() function (and comment out all the stuff to with Ext_001, as we can't
see the definition of that, how it's initialised, or whether it's big enough
to contain the filename).

If that still goes wrong, just extract this bit of code (open the file, seek
to 0, seek to 30, fgetc etc) into its own program, and out of the
dodgy-looking loop and switch statements (eg. case(4)?).
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 2013å¹´11月17日星期日UTC+8下åˆ2時49分39秒寫é“:
I write a program and want to get the byte 30 & 31 from file4 (file4 namecame from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the final output is show "printf("Check point 1 ...\n");", I don't know Why?



char IWHi, IWLo; //I tried use int IWHi, IWLo; same result

int ImgLenHi, ImgLenLo;

int FCount = 1, R200=0, R300=0, Rot=0;

int Res;

FILE *file3 = fopen(FN_OK,"r");

if ( file3 != NULL )

{

printf ("Found : %s\n",FN_OK);

fclose(file3);

for (FCount; FCount <=3; FCount++)

{

switch (FCount)

{

// case (2):

//

// break;

// case (3):

//

// break;

// case (4):

//

// break;

default:

strcat (Ext_001,FN_999);

strcat (Ext_001,"_001.tif");

printf("getfileHHMMSS_001.tif = %s\n",Ext_001);

FILE *file4 = fopen(Ext_001,"r");

if ( file4 != NULL )

{

printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);

if (fseek(file4,0,SEEK_SET) == 0)

{

if (fseek(file4,30,SEEK_SET) == 0)

{

printf("Check point 1 ...\n");

IWLo = fgetc(file4);

//printf("IWLo = %X",ImgWidthLo);

fclose(file4);

}

}

fclose(file4);

}

break;

}

}



return 0;

}

Hi Bart,

1. The Ext_001 definition is "char Ext_001[256];"
2. I changed the "char Ext_001[256];" to "char Ext_001[384];". I got same result.
3. I changed the "char Ext_001[256];" to "char Ext_001[512];". I got followresult:
======== Show on DOS Windows ==========
Found : d:\mtif2stif\transout\gotfile195041.ok <-- This is file that I wantto check.
getfileHHMMSS_001.tif = ç¿‚:\mtif2stif\transout\gotfile195041_001.tif
========================================
I got a dummy character before ":\mtif2stif\transout\gotfile195041_001.tif".. The dummy character shall be "d:".
Shall I have to initialise my Ext_001[512]? If yes, how to do?

I try to fix this question over two days and can you help me?

Kuoyang
 
F

Fuseblower

FILE *file4 = fopen(Ext_001,"r");

Hello, you're obviously reading a tiff file but you open it as a text
file.

Use fopen(Ext_001, "rb").

Note the use of "rb" instead of "r". This will open the file as a
binary stream instead of as a text stream.

Furthermore : if you're writing a tiff reader then I'd suggest you
simply read in the entire file in memory and work with that instead of
reading a single byte at a time.
 
B

BartC

1. The Ext_001 definition is "char Ext_001[256];"
2. I changed the "char Ext_001[256];" to "char Ext_001[384];". I got same
result.
3. I changed the "char Ext_001[256];" to "char Ext_001[512];". I got
follow result:
======== Show on DOS Windows ==========
Found : d:\mtif2stif\transout\gotfile195041.ok <-- This is file that I
want to check.
getfileHHMMSS_001.tif = ç¿‚:\mtif2stif\transout\gotfile195041_001.tif
========================================
I got a dummy character before
":\mtif2stif\transout\gotfile195041_001.tif". The dummy character shall be
"d:".
Shall I have to initialise my Ext_001[512]? If yes, how to do?

Ext_001 should be initialised to "" or {0} if you're going to use strcat()
on it first (otherwise replace the first strcat by strcpy). The size seems
fine.

I put together this minimal program:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int I;
FILE *file4 = fopen("testfile","r");
if (file4 != NULL)
{
printf("One %s\n","abcdef");
if (fseek(file4,0,SEEK_SET) == 0)
{
if (fseek(file4,30,SEEK_SET) == 0)
{
printf("Two\n");
I = fgetc(file4);
printf("Three\n");
fclose(file4);
}
}
fclose(file4);
}
}

That appears to work (in that it doesn't hang at the fgetc() call.) (The
unnecessary extra fclose is still there, althought it's probably not causing
your error.) My suggestion was that you did something similar, and if it
worked, to gradually add in more of your original program until the error
shows up.

You might also like to copy the contents of that complicated file to a local
file with an easier name that you can just give to fopen, which will at
least eliminate that as a possible source of the problem.
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 2013å¹´11月17日星期日UTC+8下åˆ2時49分39秒寫é“:
I write a program and want to get the byte 30 & 31 from file4 (file4 namecame from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the final output is show "printf("Check point 1 ...\n");", I don't know Why?



char IWHi, IWLo; //I tried use int IWHi, IWLo; same result

int ImgLenHi, ImgLenLo;

int FCount = 1, R200=0, R300=0, Rot=0;

int Res;

FILE *file3 = fopen(FN_OK,"r");

if ( file3 != NULL )

{

printf ("Found : %s\n",FN_OK);

fclose(file3);

for (FCount; FCount <=3; FCount++)

{

switch (FCount)

{

// case (2):

//

// break;

// case (3):

//

// break;

// case (4):

//

// break;

default:

strcat (Ext_001,FN_999);

strcat (Ext_001,"_001.tif");

printf("getfileHHMMSS_001.tif = %s\n",Ext_001);

FILE *file4 = fopen(Ext_001,"r");

if ( file4 != NULL )

{

printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);

if (fseek(file4,0,SEEK_SET) == 0)

{

if (fseek(file4,30,SEEK_SET) == 0)

{

printf("Check point 1 ...\n");

IWLo = fgetc(file4);

//printf("IWLo = %X",ImgWidthLo);

fclose(file4);

}

}

fclose(file4);

}

break;

}

}



return 0;

}

I changed follow:

char Ext_001[512];
:
strcpy (Ext_001,FN_999);
strcat (Ext_001,"_001.tif");
printf("getfileHHMMSS_001.tif = %s\n",Ext_001);
FILE *file4 = fopen(Ext_001,"rb");
if ( file4 != NULL )
{
printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);
if (fseek(file4,0,SEEK_SET) == 0)
{
if (fseek(file4,30,SEEK_SET) == 0)
{
printf("Check point 1 ...\n");
IWLo = fgetc(file4);
printf("IWLo = %X",IWLo);
fclose(file4);
}
}
// fclose(file4);

Sometimes (20%) OK (Showed "IWLo = FFFFFFD9"), sometimes (80%) failed.
I using development tool is a "Dev-C++4.9.9.2"). Is it question at "Dev-C++4.9.9.2"? I am very confuse.

Kuoyang
 
B

Ben Bacarisse

(e-mail address removed) writes:

I changed follow:

char Ext_001[512];
:
strcpy (Ext_001,FN_999);
strcat (Ext_001,"_001.tif");
printf("getfileHHMMSS_001.tif = %s\n",Ext_001);
FILE *file4 = fopen(Ext_001,"rb");
if ( file4 != NULL )
{
printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);
if (fseek(file4,0,SEEK_SET) == 0)

What is the purpose of this seek operation?
{
if (fseek(file4,30,SEEK_SET) == 0)
{
printf("Check point 1 ...\n");
IWLo = fgetc(file4);
printf("IWLo = %X",IWLo);

You should add \n at the of the format. Without it, you can't be 100%
that you'll see the data if this is the last output operation to occur.
fclose(file4);

This is the fclose you should remove.
}
}
// fclose(file4);

This one should stay: if the file gets opened, it should be closed.
Sometimes (20%) OK (Showed "IWLo = FFFFFFD9"), sometimes (80%) failed.

Try to write the smallest complete program that still exhibits the
problem.
I using development tool is a "Dev-C++4.9.9.2"). Is it question at
"Dev-C++4.9.9.2"? I am very confuse.

You have to rule out every other possibility first, and only you have
ever seen the whole program. As I say, try to get the smallest complete
program that show this problem. You may find that just trying to do
that reveals what's going on.
 
F

Fuseblower

....

IWLo = fgetc(file4);
printf("IWLo = %X",IWLo);
....

Sometimes (20%) OK (Showed "IWLo = FFFFFFD9"), sometimes (80%) failed.

Change IWLo back to int, it *will* make a difference.

The function fgetc() returns int.It reads an unsigned char from the
stream and casts that to int, returning that result.

But you assign the return value of fgetc() to IWLo which is of type
char. And char is typically signed! (it's actually a setting but in
most cases char is signed)

If you use "%X" in the format string of printf() then an argument of
type int is expected (I believe it expects an unsigned int).

So : change the type of IWLo back to int.

The reason why it shows "FFFFFFD9" is because of the implict casts in
your code.

Assume fgetc(file4) returns an int with the value 255 (which is 0xFF
in hexadecimal notation).

You put that value in a (signed) char. As such it will be interpreted
as -1 instead of 255 (given 2's complement, of course). The difference
between an unsigned and a signed char will show if you assign it to a
bigger signed type like int.

For example :

unsigned char a = 0xFF;
int b;

b = a; // b will be 255
b = (char)a; // b will be 0xFFFFFFFF

(The above assumes 2's complement and an int of 32 bits)
 
P

Phil Carmody

Fuseblower said:
For example :

unsigned char a = 0xFF;
int b;

b = a; // b will be 255
b = (char)a; // b will be 0xFFFFFFFF

(The above assumes 2's complement and an int of 32 bits)

And a whole lot else. Such as char being signed. And the cast,
being implementation-defined behaviour, actually yields -1.

And saying that an 32-bit 2's complement int will be an
expression that represents an unsigned int is more than
a little dodgy.

Phil
 
F

Fuseblower

And a whole lot else. Such as char being signed.

Well, I mentioned that earlier in my response (char being "usually"
signed but its "signedness" is really a setting of the compiler).
And the cast,
being implementation-defined behaviour, actually yields -1.

Oh darn! You're right. It's in 6.2.1.2 (of C90)

....

When a value with integral type is demoted to a signed integer with
smaller size, *OR* an unsigned integer is converted to its
corresponding signed integer, if the value cannot be represented the
result is implementation-defined.

.....

Well.... hehe... it works on my machine...

*poof*

(sound of Fuseblower making a quick getaway to hide under a rock in
shame for the next couple of years)
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 2013å¹´11月17日星期日UTC+8下åˆ2時49分39秒寫é“:
I write a program and want to get the byte 30 & 31 from file4 (file4 namecame from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the final output is show "printf("Check point 1 ...\n");", I don't know Why?



char IWHi, IWLo; //I tried use int IWHi, IWLo; same result

int ImgLenHi, ImgLenLo;

int FCount = 1, R200=0, R300=0, Rot=0;

int Res;

FILE *file3 = fopen(FN_OK,"r");

if ( file3 != NULL )

{

printf ("Found : %s\n",FN_OK);

fclose(file3);

for (FCount; FCount <=3; FCount++)

{

switch (FCount)

{

// case (2):

//

// break;

// case (3):

//

// break;

// case (4):

//

// break;

default:

strcat (Ext_001,FN_999);

strcat (Ext_001,"_001.tif");

printf("getfileHHMMSS_001.tif = %s\n",Ext_001);

FILE *file4 = fopen(Ext_001,"r");

if ( file4 != NULL )

{

printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);

if (fseek(file4,0,SEEK_SET) == 0)

{

if (fseek(file4,30,SEEK_SET) == 0)

{

printf("Check point 1 ...\n");

IWLo = fgetc(file4);

//printf("IWLo = %X",ImgWidthLo);

fclose(file4);

}

}

fclose(file4);

}

break;

}

}



return 0;

}

I wrote a simple program to test the switch command with FILE (called file3& file4). If I put the "file4" out of "switch" command, compiler OK & executable. If I put the "file4" after the "switch" command, compiler fail & couldn't generate execute file.

Below is my simple test program.
#include <stdio.h>
#include <string.h>
#include <time.h>

main ()
{
char Ext_001[512];
int IWHi, IWLo;
int ImgLenHi, ImgLenLo;
int FCount = 1, R200=0, R300=0, Rot=0;
int Res;
FILE *file3 = fopen("d:\\mtif2stif\\transout\\ORGgotfile21381.ok","rb");
FILE *file4 = fopen("d:\\mtif2stif\\transout\\ORGgotfile21381_001.tif","rb"); //Put here compiler OK & execuateable
if ( file3 != NULL )
{
printf ("Found : d:\\mtif2stif\\transout\\gotfile21381.ok\n");
fclose(file3);
for (FCount; FCount <=3; FCount++)
{
switch (FCount)
{
case 1:
break;
default:
// FILE *file4 = fopen("d:\\mtif2stif\\transout \\ORGgotfile21381_001.tif","rb"); //Put here compiler failed
if ( file4 != NULL )
{
printf("Open getfileHHMMSS_001.tif\n");
if (fseek(file4,0,SEEK_SET) == 0)
{
if (fseek(file4,31,SEEK_SET) == 0)
{
printf("Check point 1 ...\n");
IWLo = fgetc(file4);
printf("IWLo = %X\n",IWLo);
fclose(file4);
}
}
fclose(file4);
}
break;
}
}
return 0;
}
printf("Can't open file\n");
return 0;
}
 
B

BartC

I wrote a simple program to test the switch command with FILE (called
file3 & file4). If I put the "file4" out of "switch" command, compiler OK
& executable. If I put the "file4" after the "switch" command, compiler
fail & couldn't generate execute file.
FILE *file4 =
fopen("d:\\mtif2stif\\transout\\ORGgotfile21381_001.tif","rb"); //Put here
compiler OK & execuateable
default:
// FILE *file4 = fopen("d:\\mtif2stif\\transout
\\ORGgotfile21381_001.tif","rb"); //Put here compiler

I get: "a label can only be part of a statement and a declaration is not a
statement", which is self-explanatory.

But that's a minor matter: put FILE *file4; at the top, and the assignment
file4=fopen... here. Or put {...} braces around all the code comprising your
'default' section (then default: is followed by a block statement and will
shut the compiler up).

Does the positioning of this declaration/assignment affect your original
problem?
 
K

kuoyang.hu

(e-mail address removed)æ–¼ 2013å¹´11月17日星期日UTC+8下åˆ2時49分39秒寫é“:
I write a program and want to get the byte 30 & 31 from file4 (file4 namecame from "Ext_001"), the program will stop at "IWLo = fgetc(file4);", the final output is show "printf("Check point 1 ...\n");", I don't know Why?



char IWHi, IWLo; //I tried use int IWHi, IWLo; same result

int ImgLenHi, ImgLenLo;

int FCount = 1, R200=0, R300=0, Rot=0;

int Res;

FILE *file3 = fopen(FN_OK,"r");

if ( file3 != NULL )

{

printf ("Found : %s\n",FN_OK);

fclose(file3);

for (FCount; FCount <=3; FCount++)

{

switch (FCount)

{

// case (2):

//

// break;

// case (3):

//

// break;

// case (4):

//

// break;

default:

strcat (Ext_001,FN_999);

strcat (Ext_001,"_001.tif");

printf("getfileHHMMSS_001.tif = %s\n",Ext_001);

FILE *file4 = fopen(Ext_001,"r");

if ( file4 != NULL )

{

printf("Open getfileHHMMSS_001.tif = %s.\n",Ext_001);

if (fseek(file4,0,SEEK_SET) == 0)

{

if (fseek(file4,30,SEEK_SET) == 0)

{

printf("Check point 1 ...\n");

IWLo = fgetc(file4);

//printf("IWLo = %X",ImgWidthLo);

fclose(file4);

}

}

fclose(file4);

}

break;

}

}



return 0;

}

Hi Bartc,

No. I still can't solve my original program, but I decide to change my program process. I can generate the "getfilehhmmss_nnn.tif" from the original program and send this file to my another program as a argv, maybe I can keepgoing. I am not a professional program designer, I never using C over 10 years.

Thank all of guy kindly support me. I don't know you, but I am very thanks all of you.

Kuoyang
 
B

Barry Schwarz

On Mon, 18 Nov 2013 01:47:19 -0800 (PST), (e-mail address removed) wrote:


I wrote a simple program to test the switch command with FILE (called file3 & file4). If I put the "file4" out of "switch" command, compiler OK & executable. If I put the "file4" after the "switch" command, compiler fail & couldn't generate execute file.

More people might be willing to help if you deleted unneeded text from
you message and limited you line length to something less than 80.

Since you seem unwilling to tell us what the error message is, one
might think you are not really interested in solving the problem. This
is reinforced since you have not corrected any of the errors you were
previously told about (e.g., you are still closing file 4 twice).
Below is my simple test program.
#include <stdio.h>
#include <string.h>
#include <time.h>

main ()
{
char Ext_001[512];
int IWHi, IWLo;
int ImgLenHi, ImgLenLo;
int FCount = 1, R200=0, R300=0, Rot=0;
int Res;
FILE *file3 = fopen("d:\\mtif2stif\\transout\\ORGgotfile21381.ok","rb");
FILE *file4 = fopen("d:\\mtif2stif\\transout\\ORGgotfile21381_001.tif","rb"); //Put here compiler OK & execuateable
if ( file3 != NULL )
{
printf ("Found : d:\\mtif2stif\\transout\\gotfile21381.ok\n");
fclose(file3);
for (FCount; FCount <=3; FCount++)
{
switch (FCount)
{
case 1:
break;
default:
// FILE *file4 = fopen("d:\\mtif2stif\\transout \\ORGgotfile21381_001.tif","rb"); //Put here compiler failed
if ( file4 != NULL )
{
printf("Open getfileHHMMSS_001.tif\n");
if (fseek(file4,0,SEEK_SET) == 0)
{
if (fseek(file4,31,SEEK_SET) == 0)
{
printf("Check point 1 ...\n");
IWLo = fgetc(file4);
printf("IWLo = %X\n",IWLo);
fclose(file4);
}
}
fclose(file4);
}
break;
}
}
return 0;
}
printf("Can't open file\n");
return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top