Re: Declared global variable isn't being seen by main.



On Thu, 13 Mar 2008 18:44:34 -0700 (PDT), Tristin.Colby@xxxxxxxxx
wrote:

I think i figured it out. I'm on a windows box(using minGW). maybe it
doesn't support long long b/c when i change it to just unsigned long
and change the format accordingly, it works.


If you compiler doesn't support long long, your object declarations
should result in a compile time diagnostic. Have you set the warning
level to the appropriate level?

Any way I can work around this and get the long long?

Why? unsigned long is guaranteed to support a value of at least 2G.
Are you planning on testing with a larger file?



On Mar 13, 8:40 pm, Tristin.Co...@xxxxxxxxx wrote:
changed to the following:

printf("Record %lu wrong length:%llu Should be %d
\n",record,cur_len,giv_len);

to no avail

It would be nice if you told us what output you were expecting and
what you actually received.


On Mar 13, 8:32 pm, Lew Pitcher <lpitc...@xxxxxxxxxxxx> wrote:

Tristin.Co...@xxxxxxxxx wrote:
Can someone tell me why giv_len isn't being seen in this statement
below "printf("Record %d wrong length:%d Should be %d
\n",record,cur_len,giv_len)"

The answer is "because you lied to printf() by telling it that the cur_len
variable was an int".

Correct your printf() statement format string to reflect the fact that the
second variable value is an unsigned long long, and your problem should go
away.

=cut

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

char delimiter = '\n';
unsigned long long int cur_len = 0L;
int giv_len;
unsigned long int record = 0L;

int main(int argc, char *argv[])
{
char* infile = argv[1];
char* outfile = argv[2];
giv_len = atoi(argv[3]);

Will the value you provide in argv[3] fit in an int?

printf("giv_len is :%d\n",giv_len);

FILE* i = fopen(infile,"r");
FILE* o = fopen(outfile,"w");

Don't you think you ought to check to make sure both streams are
actually open?

int ch;
while((ch=fgetc(i) ) != EOF) {
++cur_len;

if(ch == delimiter) {
++record;

You planning on supporting a file with 2G records?

/*printf("%d\n",cur_len); */
if(cur_len != giv_len) {
printf("Record %d wrong length:%d Should be %d
\n",record,cur_len,giv_len);

You changed this one -

fprintf(o,"Record %d wrong length:%d\n",record,cur_len);

but you forgot to change this one. Neither record nor cur_len are of
type int as promised by the %d specifications.

}
cur_len=0;
}
}
fclose(i);
fclose(o);

return 0;
}


Remove del for email
.



Relevant Pages