snip<=-
snip<=-
However, the program isn't reading the file properly, and also not
saving new records properly.
So, which errors are you getting? And how is the file not being read properly? And how is it being written improperly?
And can you get a debugger on it to see what's in your variables shortly before doing a write, or shortly after reading it in?
I've been working on learning how to program in C, and I've run into a roadblock...
For the last week, I've been trying to get my program to read a binary file, which contains (will contain) the user records for the game. However, the program isn't reading the file properly, and also not
saving new records properly.
if ((fp = fopen(PlyrFile,"wb")) == NULL)
But first glance I notice this line in save player
if ((fp = fopen(PlyrFile,"wb")) == NULL)
That will truncate the file, so anything saved will be overwritten.
Note how I use open instead of fopen, that uses flags, it also returns
the file number instead of an fptr.
You could possibly use fopen(PlyrFile, "a+b") Not sure about that one.
I'm using Code::Blocks 16.01 under Linux, and the debugger setup kinda sucks... The only issue that I'm seeing, is when it tries to access the file, I get:
Cannot open file: iofopen.c
At iofopen.c:85
I had looked for some information on using open, but what I found
wasn't very helpful. Is the file number it returns the status of
opening the file?
So, which errors are you getting? And how is the file not being read properly? And how is it being written improperly?
I'm using Code::Blocks 16.01 under Linux, and the debugger setup kinda sucks... The only issue that I'm seeing, is when it tries to access the file, I get: Cannot open file: iofopen.c At iofopen.c:85 Cannot
open file: iofopen.c At iofopen.c:86 Cannot open file: iofopen.c At iofopen.c:58
Note how I use open instead of fopen, that uses flags, it also return the file number instead of an fptr.
I had looked for some information on using open, but what I found wasn't very helpful. Is the file number it returns the status of opening the file?
`open()` comes from the Unix system call interface. It returns an
integer called a "file descriptor", which the operating system uses
to connect the program's notion of an open file with the operating system's internal representation of a file.
I probably won't have time to do any work with it tonight, but I
hope tomorrow night I'll be able to dive in work this out. I did
adopt Andrew's file saving, but I'm thinking the file reading is
still messed up, as I'm still getting the unable to open file
message.
Thanks again for the explanation. It's greatly appreciated. :)
Feel free to dissect anything on my gitlab if it helps, sometimes I find looking at how others did certain things helps me work backwards and figure out where I'm going wrong. I'm not an expert by any means but
most of my stuff seems to do what it's supposed to (even if it took a
few goes to get it right :P)
problems. I was finally able to figure out where the problem was...
if (fno == -1)
is not the same as what I typed...
if (fno = -1)
For some reason, if you make it a comparison instead of an assignment, it works! Go figure... :)
A common workaround for this silliness is to avoid comparing the
return value against any specific value. In particular, if you
know that the lowest-valued legal file descriptor is 0, then use
an inequality comparison to probe for the error condition:
Then you can't accidentally write, `if (foo = -1)`.
Many modern compilers will also issue a warning if you have an
assignment in a conditional. In general, I recommend turning
on all the warnings you can and making sure your code compiles
cleanly with them enabled. Under GCC or Clang, I usually use
`${CC} -Wall -Wextra -Werror`.
tenser wrote to Black Panther <=-...
On 08 Feb 2020 at 10:06p, Black Panther pondered and said...
if (fno == -1)
if (fno = -1)
A common workaround for this silliness is to avoid comparing the
return value against any specific value. In particular, if you
know that the lowest-valued legal file descriptor is 0, then use
an inequality comparison to probe for the error condition:
I've never heard of that. In "Writing Solid Code" they recommended:
if ( -1 == fno ) // OK!
if ( -1 = fno ) // compiler error!
Put the constant value first, and if you happen to forget an =, you
get a compiler error.
I've never heard of that. In "Writing Solid Code" they recommended:
if ( -1 == fno ) // OK!
if ( -1 = fno ) // compiler error!
Put the constant value first, and if you happen to forget an =, you get a compiler error.
Black Panther wrote to Bugz <=-
On 09 Feb 2020, Bugz said the following...
I've never heard of that. In "Writing Solid Code" they recommended:
if ( -1 == fno ) // OK!
if ( -1 = fno ) // compiler error!
Put the constant value first, and if you happen to forget an =, you
get a compiler error.
That's a good idea. I never thought of doing it that way. I guess,
you'd get the compiler error because it's trying to assign fno to an signed integer...
I never thought coming from Pascal to C would be such a challenge... :)
I never thought coming from Pascal to C would be such a challenge... :)
I never thought coming from Pascal to C would be such a challenge...
Did you forget that C was designed by some hiddeously evil people, to be fiendishly difficult to use? :)
The compiler error is somewhat cryptic, unfortunately:
error: lvalue required as left operand of assignment
But basically saying: Hey! I can't assign that a value!
Pascal was a long time ago for me (high school). I'm lazy, Pascal wants too much typing. :P
A common workaround for this silliness is to avoid comparing the return value against any specific value. In particular, if you
know that the lowest-valued legal file descriptor is 0, then use
an inequality comparison to probe for the error condition:
I've never heard of that.
In "Writing Solid Code" they recommended:
if ( -1 == fno ) // OK!
if ( -1 = fno ) // compiler error!
That's a good idea. I never thought of doing it that way. I guess, you'd get the compiler error because it's trying to assign fno to an signed integer...
I never thought coming from Pascal to C would be such a challenge... :)
Did you forget that C was designed by some hiddeously evil people, to be fiendishly difficult to use? :)
I think you're right on that one. In a lot of ways, Pascal was so much simpler to use, and was also less cryptic.
The compiler errors/warnings can be cryptic. I have one now that says:
warning: suggest parentheses around assignment used as truth value
The line it's indicating is:
for (y=x;y=20;y--)
for (y=x;y=20;y--)
This is the same problem as your earlier `if (fno = -1)`. The error
here is in the conditional part of the `for` loop. I suggest breaking
Did you forget that C was designed by some hiddeously evil
people, to be
That's actually really, really not true. Dennis Ritchie was the main
Pascal was a long time ago for me (high school). I'm lazy, Pascal wants too much typing. :P
Of course both are Turing complete, and so in some sense each is
strictly as powerful as the other (e.g., one can write a C
interpreter in Pascal a Pascal interpreter in C).
Apologies to Dennis, I'll hereafter think of him as Dennis the Menace instead of Dennis the hiddeously evil.... on the other hand... I'm sure there must be something evil in there.... C is a truly infernal contraption :)
I have a recollection of Borland writing early versions of C in pascal. Not sure where it tipped over and they started using C for it.
Not sure
of the validity but pascal seems to have predated c by some margin.
Apologies to Dennis, I'll hereafter think of him as Dennis the Menace instead of Dennis the hiddeously evil.... on the other hand... I'm
sure there must be something evil in there.... C is a truly infernal
contraption :)
He wasn't a menace at all (again, he was a genuinely nice person). We
lost a giant when he died in 2011.
Apologies to Dennis, I'll hereafter think of him asDennis the Menace Sp> instead of Dennis the hiddeously
evil.... on the other hand... I'm Dp> sure there must be
something evil in there.... C is a truly infernal Sp>
contraption :)
He wasn't a menace at all (again, he was a genuinely nice
person). We lost a giant when he died in 2011.
<Shrug> we all have our own thoughts... I'm not trying to say he's
the devil
personified... perhaps some crazed masochist, definitely a menace
to me :) .. obviously we see him in a very different light.
<Shrug> we all have our own thoughts... I'm not trying to say he's the devil personified... perhaps some crazed masochist, definitely a menace
to me :) .. obviously we see him in a very different light.
Black Panther wrote to Bugz <=-
The compiler errors/warnings can be cryptic. I have one now that says: warning: suggest parentheses around assignment used as truth value
The line it's indicating is:
for (y=x;y=20;y--)
I'm not sure what it's trying to tell me needs parentheses...
Pascal was a long time ago for me (high school). I'm lazy, Pascal wants too much typing. :P
That's when I originally learned it as well. I picked it up again a few years ago. Pascal did seem to be more typing, but it was also less cryptic, in my opinion...
tenser wrote to Bugz <=-
On 09 Feb 2020 at 01:07p, Bugz pondered and said...
A common workaround for this silliness is to avoid comparing the return value against any specific value. In particular, if you
know that the lowest-valued legal file descriptor is 0, then use
an inequality comparison to probe for the error condition:
I've never heard of that.
Really? How interesting. There's a school of thought
says that you should always use the inequality instead
of and equality operator unless you're _sure_ that the
return value is exactly some number, or more generally,
that some number exactly terminates some range, etc.
equality is fine. But I can't see why NOT to use the
inequality, and further, it may help the compiler
optimize your code since it can do a comparison against
zero (a single instruction) instead of a comparison
against another number (probably two instructions).
In "Writing Solid Code" they recommended:
if ( -1 == fno ) // OK!
if ( -1 = fno ) // compiler error!
That's another technique; those are called "yoda
conditionals". Those have fallen out of favor
since compilers can warn on the `foo = bar` pattern
in a conditional, and best practice has evolved
away from treating assignment as an expression.
Spectre wrote to Bugz <=-
All I had at High School was an Apple IIe and basic... I didn't know anything about Pascal till I was in my 20's somewhere... was a good fit for me.. but I've probably long forgotten more than I ever knew now...
Really? How interesting. There's a school of thought
says that you should always use the inequality instead
of and equality operator unless you're _sure_ that the
return value is exactly some number, or more generally,
that some number exactly terminates some range, etc.
Sorry, no, I agree if you are checking a range for errors, don't
use equality to check every possibility. (*)
(Use the right tool for the job).
(*) But if you're checking for just -1, using < 0 (while correct),
makes other future readers of your code say "Why did you do that?
What other errors -2, -3, etc, do you know about that we don't?"
Should we be looking specifically for these? Instead of saying
"File not found" when the file does exist, we should instead say,
"File is locked" or "Network error" so our error messages can be
clearer, rather than a vague "We failed".
I think code readability is king. *FUTURE* code readability++
Good error messages are great too. Your users will appreciate it.
But also keep in mind that it's not a compiler error if you
reverse your alligator.
or if you reverse the compares:
If you can have the compiler help you warn or error on simply wrong
code, I think you should do that.
Because compilers can only optimize against zero comparisons?
Really?! :P
I wouldn't make any bets on what gets optimized how these days.
The only time I'd worry about instruction sizes, is if I'm writing
for ardrino, where small, tight, hard to read code would be allowed.
Otherwise, clearly write what you mean.
While I agree that compilers are much better then they were --
and they rarely get the line numbers wrong these days (thank you
modern compilers) -- I'm not sure you should depend upon them to catch everything.
Code defensively. If you have to write C code, use whatever
techniques you can find to make it harder for "it compiles,
without warnings, but it isn't ever going to do what I want
correctly" to occur.
All I had at High School was an Apple IIe and basic... I didn't knowHigh School was TRS-80 Model III's. And when I was leaving, they were getting IBM PCs.
On 02-09-20 16:30, Black Panther wrote to tenser <=-
On 10 Feb 2020, tenser said the following...
for (y=x;y=20;y--)
This is the same problem as your earlier `if (fno = -1)`. The error
here is in the conditional part of the `for` loop. I suggest breaking
I noticed this right after posting it... I gotta get out of Pascal
mode...
On 02-10-20 10:09, Spectre wrote to Bugz <=-
Pascal was a long time ago for me (high school). I'm lazy, Pascal wants too much typing. :P
All I had at High School was an Apple IIe and basic... I didn't know anything about Pascal till I was in my 20's somewhere... was a good fit for me.. but I've probably long forgotten more than I ever knew now...
for (y=x;y=20;y--)
I'm not sure what it's trying to tell me needs parentheses...
The compiler is trying to say, I'm expecting a comparison, but I see an assignment.
It's all relative. Assembly for the cryptic win! (Or brainf*** for
a runner up.)
I noticed this right after posting it... I gotta get out of Pascal mode...
Oops. :D
I'm finding this thread interesting learning. :)
I started with Applesoft Basic on the Apple II+ and IIe at school. Not
I knew him. I'm sorry, he's just none of the things you're
describing.
On 02-10-20 18:41, Black Panther wrote to Vk3jed <=-
Oops. :D
Yep. My forehead was so sore from me smacking it... :) (smacking my forehead, just in case that didn't sound right...)
I'm finding this thread interesting learning. :)
Me too. I just hope I didn't start a battle between people who know C better than I do...
On 02-10-20 18:44, Black Panther wrote to Vk3jed <=-
On 11 Feb 2020, Vk3jed said the following...
I started with Applesoft Basic on the Apple II+ and IIe at school. Not
I also started with Apple Basic on the II+ and IIe. I think I was a
senior when the school got 2 Apple IIgs systems. They were fast!
We then went on to Apple Pascal. I really didn't care for it much
then...
Black Panther wrote to Bugz <=-
Yep. I caught that right after I posted it here... I've given up on smacking myself on the forehead... :)
We've all done the same mistakes. Keep at it, you'll get it. ;)
When I try to use srand() and rand(), it will compile just fine. When I run the program, it will segfault though.
I try to run in debug, and I'm getting unable to open file random.c.
I have run into another issue, but it might be something with Code::Blocks.
When I try to use srand() and rand(), it will compile just fine. When I run the program, it will segfault though.
I try to run in debug, and I'm getting unable to open file random.c.
The line that's giving me the problems is:
srand(time(0));
tmp=(rand() % 100)+1;
I do have #include <stdlib.h>, but it's acting as though the functions
are not available. When I try to 'find the declaration of srand()', it states it is unable to find...
Not sure what I'm doing wrong on this one...
I do have another question. I have a data file that was written by a program in Pascal. Is it possible to read this file with C, or should I just recreate the file? The file is an array of records.
Sysop: | sneaky |
---|---|
Location: | Ashburton,NZ |
Users: | 31 |
Nodes: | 8 (0 / 8) |
Uptime: | 191:00:31 |
Calls: | 2,082 |
Calls today: | 5 |
Files: | 11,137 |
Messages: | 947,707 |