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



changed to the following:

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

to no avail


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]);
printf("giv_len is :%d\n",giv_len);

FILE* i = fopen(infile,"r");
FILE* o = fopen(outfile,"w");
int ch;
while((ch=fgetc(i) ) != EOF) {
++cur_len;

if(ch == delimiter) {
++record;
/*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);
fprintf(o,"Record %d wrong length:%d\n",record,cur_len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);

return 0;
}

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

.



Relevant Pages

  • Re: Can somebody help...
    ... a number of unsafe constructs and non-standard extensions. ... That value is too large to portably store in an int. ... This results in undefined behavior because you lied to printf about what ...
    (comp.lang.cpp)
  • Re: Is this correct..??
    ... long int i; ... Almost everthing is defined in assembler. ... Easily defeated by compiler optimisations. ... printf and that the first assignment to i is never used and decides to ...
    (comp.lang.c)
  • Semaphores Block when they Shouldnt and Dont when they Should
    ... took the votes (encoded in the first character of the transmitted mes- ... int socketAccess; ... write(voteSocket, voteBuffer, 2); ... {printf("Couldn't make a socket.\n"); ...
    (comp.unix.programmer)
  • RSA encrypt/decrypt c program
    ... Encrypt or Decrypt? ... int i,number; ... printf;} ... scanf; ...
    (comp.programming)
  • Re: The illusion of "portability"
    ... int main ... know if our printf did write something to stdout, ... The error code returned by printf is nowhere specified. ... the return value is EOF if an error occurs, ...
    (comp.lang.c)