473,545 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declared global variable isn't being seen by main.

Can someone tell me why giv_len isn't being seen in this statement
below "printf("Re cord %d wrong length:%d Should be %d
\n",record,cur_ len,giv_len)"

=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,"Reco rd %d wrong length:%d\n",re cord,cur_len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);
return 0;
}
Mar 14 '08 #1
22 1485
Tr***********@g mail.com wrote:
Can someone tell me why giv_len isn't being seen in this statement
below "printf("Re cord %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,"Reco rd %d wrong length:%d\n",re cord,cur_len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);
return 0;
}
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Mar 14 '08 #2
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...@teksa vvy.comwrote:
Tristin.Co...@g mail.com wrote:
Can someone tell me why giv_len isn't being seen in this statement
below "printf("Re cord %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,"Reco rd %d wrong length:%d\n",re cord,cur_len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);
return 0;
}

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digital freehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Mar 14 '08 #3
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.

Any way I can work around this and get the long long?
On Mar 13, 8:40 pm, Tristin.Co...@g mail.com wrote:
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...@teksa vvy.comwrote:
Tristin.Co...@g mail.com wrote:
Can someone tell me why giv_len isn't being seen in this statement
below "printf("Re cord %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,"Reco rd %d wrong length:%d\n",re cord,cur_len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);
return 0;
}
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digital freehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Mar 14 '08 #4
Tr***********@g mail.com 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.

Any way I can work around this and get the long long?
<top post snipped>

This is a well known "interoperabili ty issue" between MinGW and
Microsoft's C runtime library. MinGW's compiler, gcc, supports long
long while the C library it links to by default (Microsoft's CRT) does
not support almost any C99 features, including long long. Thus printf
(which is part of CRT) displays the value incorrectly.

The fix is to use a compiler and C library that agree with each other.
An alternative to MinGW on Windows may be Cygwin, or DJGPP.

Mar 14 '08 #5
On Thu, 13 Mar 2008 18:44:34 -0700 (PDT), Tr***********@g mail.com
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...@g mail.com wrote:
>changed to the following:

printf("Reco rd %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...@teksa vvy.comwrote:
Tristin.Co...@g mail.com wrote:
Can someone tell me why giv_len isn't being seen in this statement
below "printf("Re cord %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,"Reco rd %d wrong length:%d\n",re cord,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
Mar 14 '08 #6
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_B ITS=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:31920669 04
Record 301 exceeds maximum length:40096225 04

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_BI TS 64
#define _LARGEFILE_SOUR CE

/* you must use the compiler flag -D_FILE_OFFSET_B ITS=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",a rgv[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",opt opt);
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,"Reco rd %lu exceeds maximum length:%lu
\n",record,cur_ len);
}
cur_len=0;
}
}
fclose(i);
fclose(o);
return 0;
}

void help() {
printf("\n");
printf("Variabl e 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");
}

Mar 14 '08 #7
On Mar 13, 8:44 pm, Tristin.Co...@g mail.com 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.
http://www.mingw.org/MinGWiki/index.php/long%20long
Mar 14 '08 #8
santosh <sa*********@gm ail.comwrites:
Tr***********@g mail.com 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.

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

<top post snipped>

This is a well known "interoperabili ty issue" between MinGW and
Microsoft's C runtime library. MinGW's compiler, gcc, supports long
long while the C library it links to by default (Microsoft's CRT) does
not support almost any C99 features, including long long. Thus printf
(which is part of CRT) displays the value incorrectly.

The fix is to use a compiler and C library that agree with each other.
An alternative to MinGW on Windows may be Cygwin, or DJGPP.
Really? I've heard that MinGW has an inconsistency in the
representation of long double supported by the compiler vs. the
representation expected by the (MS-provided) runtime library. I
hadn't heard that there was an issue with long long. You could well
be right, of course.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 14 '08 #9
Barry Schwarz <sc******@dqel. comwrites:
On Thu, 13 Mar 2008 18:44:34 -0700 (PDT), Tr***********@g mail.com
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?
If the compiler supports long long, but the runtime library doesn't
(in particular, if printf doesn't support the "%llu" format), then you
could have problems that don't show up as compiler error messages. I
know that MinGW, which combines a compiler and a runtime library from
two different sources, has this kind of problem with long double; it
might have a similar problem with long long.

[snip]

Tristin, please don't top-post. Read the following:

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 14 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5859
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding...
2
5174
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and second name for better readable. After optimization, global variables might be misaligned because each global variables must be converted to 32 bits,...
10
1947
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this philosophy is from object orieted world but is it applicable for c?
1
17580
by: Roy Gourgi | last post by:
Hi, I am new to C#. I am trying to create some variables and arrays that can be seen throughtout the whole program. I have no choice as they have to be seen by the whole program. Where and how do I declare them? I tried putting the variable gnSum and gaArray in my Main() method but the rest of the program does not see them. class...
6
2418
by: calcop | last post by:
Hello everyone, I hope I can write this question clearly. Anyway, it is about global variables. I have several files in my C++ project. One of which is main.cpp. This file comtains my main() function. I have a few other files in my project. They are server.cpp, server.h, strings.h, includes.h and functions.h. My main.cpp function #includes...
2
1702
by: robin.bruce | last post by:
Hi guys, I just tried to give advice to a colleague who has a problem program in C, and I was interested to hear the thoughts of this forum on the subject. I've been an occasional programmer in C for about a year now, so by no means an expert, so it would be good to know if I'm leading my colleague down the wrong path.
8
3439
by: yinglcs | last post by:
Hi, I read this article about global variable in c: http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html But I have a few questions 1. how can I declare the global variable so that it can be set/get by other files? 2. If I have multiple instance of the same program running in the same machine (linux), will each program...
2
1599
by: Florian Lindner | last post by:
Hello, I have a little problem with the global statement. def executeSQL(sql, *args): try: import pdb; pdb.set_trace() cursor = db.cursor() # db is <type 'NoneType'>. except: print "Problem contacting MySQL database. Please contact root."
0
1432
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no* program wide globals. There are only module wide globals. Also, the "global isglobal" is absolutely meaningless as anything declared there is a...
0
7434
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.