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



I'm using files that can potentially be several terabytes. My first
test file was over 232GB. I had to compile with the -
D_FILE_OFFSET_BITS=64 flag just to be able to open it. I'm getting
relatively good performance though, over 1GB a minute. Here's an
example of the output.

Record 201 exceeds maximum length:3192066904
Record 301 exceeds maximum length:4009622504

Yes the value provided to argv[3] will fit into an int. Our products
have a max length of a record so it'll never be bigger than 102400
bytes. This app should really help. Try searching through 500GB for a
single bad record. :)

Here's the actual "finished" code Maybe someone else can get some use
out of it or tell me some way to optimize it further.

=cut

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

/* Unsure if these are really needed */
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE

/* you must use the compiler flag -D_FILE_OFFSET_BITS=64 on linux
so that it is possible to open/parse files larger than 2GB

2008 March 13
*/


int debug = 0;

void help();

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

int main(int argc, char *argv[])
{
char* infile = NULL;
char* outfile = NULL;
giv_len = 0;

if(debug) {
int i = 0;
for(i=0;i<argc;i++) {
printf("%s\n",argv[i]);
}
}

if(argc == 1) {
help();
exit(0);
}

int option;
/* Parse Our options
***********************************************************/
while( (option=getopt(argc,argv,"i:o:l:h")) != -1 ) {
switch(option) {
case 'i':
infile = optarg;
break;
case 'o':
outfile = optarg;
break;
case 'l':
giv_len = atoi(optarg);
break;
case 'h':
help();
break;
case '?':
printf("Unknown argument:%c\n",optopt);
exit(2);
break;
case ':':
printf("Option %c requires an argument\n",optopt);
exit(2);
break;

}
}

FILE* i;
FILE* o;

if( (( i = fopen(infile,"r") ) == NULL ) || ( o =
fopen(outfile,"w") ) == NULL) {
printf("You must specify a valid input and output file\n");
perror("Error");
exit(2);
}

if(giv_len == 0) {
printf("You must give a [maximum] record length\n");
exit(2);
}

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

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

if(cur_len > giv_len) {
printf("Record %lu exceeds maximum length:%lu
\n",record,cur_len);
fprintf(o,"Record %lu exceeds maximum length:%lu
\n",record,cur_len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);


return 0;
}

void help() {
printf("\n");
printf("Variable Length File Verifier:\n");
printf("-i file The name of the input file\n");
printf("-o file The name of the output file\n");
printf("-l length The max number of bytes per record\n");
printf("-h This screen\n");
printf("\n");
printf("Example: variable.exe -i 99999.SO01 -o badrecords.txt -l
102400\n");
printf("\n");
printf("\n");
}

.



Relevant Pages