help

I

i

HELLO SIR,
void debit_card(int n_,
int m_,
char ch_)
{
int de, pin, j, f;
long int g;
f=0;
g=100000;
//size_t i;
if((fp = fopen("debitCard.dat", "r+t"))==NULL)
{
printf("\nFile does not exist");
//exit(0);
}

printf("\nEnter debit card number and pin number");
scanf("%d%d", &de, &pin);
printf("\nConnecting to your Bank Server....");
do
{
g--;
}while(g!=0);
if(ch_ == 'b')
{
j = n_ * 6;
while(!feof(fp)&&!f)
{
fread(&d1, sizeof (struct debit), 1, fp);
if((de == d1.dc_n) && (pin == d1.p))
{
f=1;
if(d1.bal > j)
{
printf("\nTransaction Completed");
d1.bal =d1.bal-j;
fwrite(fp, sizeof (struct debit), 1, fp);
/*fread(&d1, sizeof (struct debit), 10, fp);
printf("%d",d1.bal);
fclose(fp);*/
clear(n_, m_, ch_);
receipt(n_, j);

break;
}
else if(d1.bal < 0)
{
printf("\nBalance below zero");
break;
}
else if(d1.bal < j)
{
printf("\nThere is no enough money");
break;
}

}

}
if(f==0)
{
printf("\nIncorrect format");
printf("\nRestart Transaction");
}
fclose(fp);
}
I can't update the files data.I get d1.bal from the file and do some
modification and update that .But the same data is retrieved from me
when i again run this program. Please solve this.
2nd question is.....
I want to display two answers .When i press the Enter key first
answer should be displayed and few seconds later the second answer
should be displayed. How can i do it?
 
P

per9000

[...]
I can't update the files data.I get d1.bal from the file and do some
modification and update that .But the same data is retrieved from me
when i again run this program. Please solve this.

I could be wrong but it seems you open the file, read at a particular
position (and then forget to rewind to the same position you started
from) and then overwrite *the next value* on the file.

Perhaps fseek is what you need?
http://en.wikibooks.org/wiki/C_Programming/File_IO

A classic debug approach would be to make a function that displays the
contents of the file in a nice format. This way you can read the
status of all accounts on it before and after a transaction.

HTH,
Per

--

Per Erik Strandberg
..NET Architect - Optimization
Tomlab Optimization Inc.
http://tomopt.com/tomnet/
 
O

osmium

i said:
I want to display two answers .When i press the Enter key first
answer should be displayed and few seconds later the second answer
should be displayed. How can i do it?

You can create short delays by using the clock() function, see <time.h>.
 
R

Richard Bos

osmium said:
You can create short delays by using the clock() function, see <time.h>.

You can, but you shouldn't. If you do this on a multi-user system, you
risk getting banned by the sysadmin for hogging the CPU. A much better
solution is to use a system-specific function for this more or less
system-specific problem; ask in a newsgroup that deals with the OS
and/or compiler you're using, and see also (as usual!) the FAQ:
<http://c-faq.com/osdep/subsecond.html>.

Richard
 
O

osmium

Richard Bos said:
You can, but you shouldn't. If you do this on a multi-user system, you
risk getting banned by the sysadmin for hogging the CPU. A much better
solution is to use a system-specific function for this more or less
system-specific problem; ask in a newsgroup that deals with the OS
and/or compiler you're using, and see also (as usual!) the FAQ:
<http://c-faq.com/osdep/subsecond.html>.

The code posted by the OP suggested, to me, a beginning coder - very likely
a student.

There are two possible answers that survive the very vigilant net nannies on
this NG.
o A C way
o You can't do that in standard C.

I thought the first approach was more helpful, considering who was asking
the question. You approach would not only be more difficult for the OP to
use but it violates the net nannies threshold for NG abuse.
 
F

Flash Gordon

osmium wrote, On 09/05/07 16:23:
The code posted by the OP suggested, to me, a beginning coder - very likely
a student.

There are two possible answers that survive the very vigilant net nannies on
this NG.
o A C way
o You can't do that in standard C.

I thought the first approach was more helpful, considering who was asking
the question.

I would be mildly surprised if the instructor did not expect the OP to
use a delay/sleep/wait type function provided by the implementation
rather than a loop, so I would consider Richard's suggestion to be more
helpful. It is especially helpful as if the instructor is good the OP
might even get some bonus points for saying that it is a system specific
solution.
> You approach would not only be more difficult for the OP to
use but it violates the net nannies threshold for NG abuse.

Saying you should (or can only) do something using a system specific
method and saying where to go to find the solution does not as far as
I've seen upset even the strictest interpreters of this groups purpose.
It is when the discussion is not redirected to an appropriate place that
the complaints start.
 
O

osmium

Flash Gordon said:
osmium wrote, On 09/05/07 16:23:

I would be mildly surprised if the instructor did not expect the OP to use
a delay/sleep/wait type function provided by the implementation rather
than a loop, so I would consider Richard's suggestion to be more helpful.
It is especially helpful as if the instructor is good the OP might even
get some bonus points for saying that it is a system specific solution.

In retrospect, I can see that I *might* have avoided some of this
tendentious bickering if I would have added a sentence to my original post
"There are better ways but they are more difficult and system specific"
Unfortunately, my posts are rarely of time capsule quality.

To get back to the point. First of all, none of the responders even know if
this is a school assignment But, if it is, I, in contrast with you, would
be disgusted if the instructor wanted or expected a system specific
solution. I had hoped that the bad old days of teaching a computer language
along with a mish-mash of vendor specific extensions was behind us. I am
thinking of courses such as Turbo Pascal and Microsoft Basic, and such like.
Such courses make it extremely hard to know where the language ends and
something else begins. I would expect better than that for a college
course, it might be OK for a trade school where the object of the course is
to get up and running really fast. .

Your way, makes me think of signing up for a course in German and the class
is actually about German as spoken in the Ruhr valley. Which may be
mentioned *once*, in passing, the first day of the course.
 
F

Flash Gordon

osmium wrote, On 09/05/07 22:30:
In retrospect, I can see that I *might* have avoided some of this
tendentious bickering if I would have added a sentence to my original post
"There are better ways but they are more difficult and system specific"

On at least other systems they are easier ;-)
Unfortunately, my posts are rarely of time capsule quality.

To get back to the point. First of all, none of the responders even know if
this is a school assignment But, if it is, I, in contrast with you, would
be disgusted if the instructor wanted or expected a system specific
solution. I had hoped that the bad old days of teaching a computer language
along with a mish-mash of vendor specific extensions was behind us.

If I was the instructor then maximum points would be for using a system
specific solution and providing a good reason why a system specific
solution should be used and also say what the solution using standard C is.
> I am
thinking of courses such as Turbo Pascal and Microsoft Basic, and such like.
Such courses make it extremely hard to know where the language ends and
something else begins. I would expect better than that for a college
course, it might be OK for a trade school where the object of the course is
to get up and running really fast. .

Your way, makes me think of signing up for a course in German and the class
is actually about German as spoken in the Ruhr valley. Which may be
mentioned *once*, in passing, the first day of the course.

Students need to learn the limitations of standard C and when it is
advisable to move beyond it. They also need to be taught to isolate such
code. Teaching that involves teaching some stuff beyond standard C.

Also, if the instructor is giving assignments that for a decent solution
require going beyond standard C then I think it is best for us to help
the students get a good mark by doing what the instructor probably
expects whilst also teaching them where the boundary is between standard
C and system specific extensions.

As part of this I consider all questions of the type, "can you do X in
C?" to be topical even where the answer is no. Of course, if the answer
is in the FAQ then the question should not be asked because the FAQ
should be checked first, but that is dealt with by pointing them at the
FAQ so they know to check next time.
 
K

Keith Thompson

osmium said:
In retrospect, I can see that I *might* have avoided some of this
tendentious bickering if I would have added a sentence to my original post
"There are better ways but they are more difficult and system specific"
Unfortunately, my posts are rarely of time capsule quality.

The problem being discussed is causing a program to pause for a
specified time interval.

There are *no* good portable ways to do this. A busy loop using
clock() will, at best, delay for a specified interval of CPU time,
which is hardly ever what's wanted. A busy loop using time() has no
guaranteed resolution; on a most real-world systems, the resolution is
1 second, which is too coarse for many purposes. And using a busy
loop on a shared or multi-processing system, which someone just
learning C is almost certainly using, is antisocial behavior.

By contrast, the system-specific solutions that actually exist are
much *easier* to use than the portable kludges; it's typically a
single call to a function with an obvious name and a single argument
whose meaning is quite clear.

Not all C code can be, or should be, portable. Much of the purpose of
the C language is to enable the use of system-specific extensions
where they're appropriate. The fact that the details of those
extensions are off-topic in this newsgroup doesn't mean we should be
giving people bad advice for the sake of maintaining 100% portability.
 
O

osmium

Keith Thompson said:
And using a busy
loop on a shared or multi-processing system, which someone just
learning C is almost certainly using, is antisocial behavior.

My God man, how old are you? The use of main frames in schools went out with
high button shoes. Or at least the 8-track player.
 
K

Keith Thompson

osmium said:
My God man, how old are you? The use of main frames in schools went out with
high button shoes. Or at least the 8-track player.

I didn't mention mainframes.

Even on a single-user system (say, a PC running Windows or Linux), a
busy loop will consume CPU resources that could otherwise be used by
other processes.

Other than in some embedded systems where the CPU is running one and
only one program, using a busy loop to wait for a specified period of
time is, as far as I can tell, *never* a good idea.
 
R

Richard Tobin

And using a busy
loop on a shared or multi-processing system, which someone just
learning C is almost certainly using, is antisocial behavior.
[/QUOTE]
My God man, how old are you? The use of main frames in schools went out with
high button shoes. Or at least the 8-track player.

True, but:

On our home Mac, I often find that the fans are noisy and my programs
run slowly because my children have left web browsers running with
pages covered in animated pictures and the like. For some reason the
browsers continue to process these images even when the window is not
visible. Not quite an idle loop, but evidently it would be better if
today's programmers learnt early that resource sharing is important
even on "personal" computers.

Back in the 70s, I remember being very worried when I realised that I
had left a pi-calculating program running on the PDP-11 that our
school used - it belonged to the local hospital. Fortunately no-one
died :)

-- Richard
 
C

Christopher Benson-Manica

osmium said:
My God man, how old are you? The use of main frames in schools went out with
high button shoes. Or at least the 8-track player.

That's actually not true at all. When I began my days as a Georgia
Tech student - in 1998, mind you - three central machines (acmex,
acmey, and acmez) - were the staple of every student's life. It was
quite common for even the non-computer-inclined to muddle through the
tedium of reading school e-mail with mutt or pine, and of uploading
files for one's personal web page. Computer science students did much
of their development work on these three machines, and programs were
tested on these machines. One had to be careful not to hog the CPU,
since the system tracked "bananas" used, and CPU time was very
expensive in terms of "bananas" - use up your "bananas" for a
semester, and every subsequent run of tin would cost you money.

The moral of this anecdote is that at least one school kept its
mainframes around past the age of slap bracelets and well into the age
of CD's, and furthermore expected their students to use them.
 
C

Christopher Benson-Manica

osmium said:
I had hoped that the bad old days of teaching a computer language
along with a mish-mash of vendor specific extensions was behind us.

Alas, Pangloss, I am afraid you're mistaken, at least in my
experience. I like to think I got a pretty good C education courtesy
of one Jim Greenlee (of Georgia Tech infamy), but even that course was
inextricably bound up with the whims of *nix. I imagine few
instructors are anywhere near as familiar with the standard language
as many posters here are, and fewer would mark down an assignment for
correct use of system-specific extensions to achieve a task
(excluding, of course, using "sort" to print out a sorted list of
numbers, and the like).
 
B

Bart van Ingen Schenau

Keith said:
Other than in some embedded systems where the CPU is running one and
only one program, using a busy loop to wait for a specified period of
time is, as far as I can tell, *never* a good idea.
The only system where a busy loop can be acceptable would be a
mains-powered embedded device running a single-threaded program.
I think those devices are becoming vanishingly rare.

For battery-powered embedded devices, a busy loop is an extremely bad
idea, because it will significantly reduce the time before the
batteries have to be replaced/recharged.

Bart v Ingen Schenau
 
F

Flash Gordon

Bart van Ingen Schenau wrote, On 10/05/07 19:51:
The only system where a busy loop can be acceptable would be a
mains-powered embedded device running a single-threaded program.

Even then it is a bad idea since it increases power consumption and thus
heat production so you have to design the system to dissipate more heat
(possibly needing a bigger heat sink of fan) thus adding a couple of
pence to the manufacture cost of a unit you will be producing
100,000,000 of, thus costing 2000,000 pounds. This is why car
manufacturers will try to shave a few pence off the cost of making a car ;-)
I think those devices are becoming vanishingly rare.

I've got at least one (probably 2 or more) in each of the old TVs in my
house, several in my desktop computer (the graphics card probably has
the most powerful, the TV card probably has another...), one in my DTV
box and a number of others scattered through my house. Embedded
processors powered off the mains are very common, you just don't think
about most of them.
For battery-powered embedded devices, a busy loop is an extremely bad
idea, because it will significantly reduce the time before the
batteries have to be replaced/recharged.

That is true, but sometimes the heat dissipation issue is still the
bigger issue.

Power consumption and heat dissipation are also big issues for the types
of avionics systems I used to work on.
 
U

Urs Beeli

Bart van Ingen Schenau wrote, On 10/05/07 19:51:
^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
I've got at least one (probably 2 or more) in each of the old TVs in my
house, several in my desktop computer (the graphics card probably has
the most powerful, the TV card probably has another...), one in my DTV
box and a number of others scattered through my house. Embedded
processors powered off the mains are very common

But most of them would not be single-threaded anymore...

Cheers
/urs
 
C

Chris Hills

[...]
I can't update the files data.I get d1.bal from the file and do some
modification and update that .But the same data is retrieved from me
when i again run this program. Please solve this.

I could be wrong but it seems you open the file, read at a particular
position (and then forget to rewind to the same position you started
from) and then overwrite *the next value* on the file.

Perhaps fseek is what you need?

Have you actually read the statements in the code of the person you are
trying to help?

if((fp = fopen("debitCard.dat", "r+t"))==NULL)
printf("\nEnter debit card number and pin number");
printf("\nConnecting to your Bank Server....");

This code is for one of two things.
1 Homework
2 Hacking.

Neither of which we should be doing here.
 
R

Richard Heathfield

Chris Hills said:

if((fp = fopen("debitCard.dat", "r+t"))==NULL)
printf("\nEnter debit card number and pin number");
printf("\nConnecting to your Bank Server....");

This code is for one of two things.
1 Homework
2 Hacking.

Neither of which we should be doing here.

s/Hacking/Cracking/please.
 
O

osmium

Chris Hills said:
[...]
I can't update the files data.I get d1.bal from the file and do some
modification and update that .But the same data is retrieved from me
when i again run this program. Please solve this.

I could be wrong but it seems you open the file, read at a particular
position (and then forget to rewind to the same position you started
from) and then overwrite *the next value* on the file.

Perhaps fseek is what you need?

Have you actually read the statements in the code of the person you are
trying to help?

if((fp = fopen("debitCard.dat", "r+t"))==NULL)
printf("\nEnter debit card number and pin number");
printf("\nConnecting to your Bank Server....");

This code is for one of two things.
1 Homework
2 Hacking.

Neither of which we should be doing here.

I don't see why not. The OP posted code showing a serious attempt at a
solution. Helping in such a case is no different than the help offered by a
lab instructor, which may not be available to the poster. Perhaps it is
3:00 AM and he is at home, for example.

I must admit, I never read his main problem. I saw two questions and an
answer to one of them which I pretty much ignored and answered the second
question, an answer that was received with much scorn, but that is neither
here nor there.. The thread then drifted to excessive power consumption in
battery
powered embedded systems and I lost interest. Apparently modern embedded
systems have some way of separating useful instructions from non-useful
instructions and hibernating while it waits for something useful to do.

But I did find the post about how
far things are behind in Georgia (US not USSR) of interest, however. The
consensus answer of
the group was that I should have said "go away, that is not topical here", a
response of which I am not a big fan.

I any event. I agree with you that the answer posted by per9000 does not
correspond to the question. As I understand it, there were *two* runs on
the data base.

I revisited the original question and the first thing I note is that the OP
doesn't bother to look at the status returned by fwrite(). I suggest he fix
that, although I think it unlikely that is his root problem; it is simply
good practice.

To the OP: If you want substantive help with a *program*, post a *program*.
You posted an uncompilable *function* that depends on global variables fp
and d1 and perhaps more. The only variable names you use that I can
decipher are fp and pin. I am sure there is a very good reasons that g has
an initial value of 100000 but it eludes me at the moment. Was Einstein
working on this when he died? Or what? ch is apparently obtained from a
menu entry, but it is associated with a code we can not see. You have code
commented out, apparently failed attempts at something or other. You don't
have to demonstrate what you tried, if it didn't work, take it out.. I can
see why per9000 gave the answer he did. He was reading the commented out
read as being part of the program. But your *narrative* says

"But the same data is retrieved from me
when I again run this program. "

Note the word *again*. IOW, a second, *separate* run.

If you want to do a "write-check" (as hinted in your code) back up the file
pointer as suggested by per9000. As a first cut, for a write check do

o ftell
o fwrite
o fseek
o fread

There may be better ways to do this. ISTM like a good approach for a
student.

In short, post a *program* without any abandoned code. If you can remove
stuff that is not germane, all the better.
 

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

Similar Threads

error 7
Fibonacci 0
error correcting 9
URGENT 1
Help with EXT3 Filesystem work 1
Need help in debugging tic tac toe (Beginner) 0
Command Line Arguments 0
Help with Loop 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top