We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
C programming
shutton54321
Posts: 921 Forumite
in Techie Stuff
Can anyone help me with this problem i am currently doing a project in college for c programming but need help on one bit, how can i check if an id for example is unique consiting of a letter number number number which is using structures? and how do i read or write from a file?
0
Comments
-
This is C++ but I assume its legal C as well, you use fopen and fscanf to read a file... Not sure about the other stuff, but im pretty sure theres similar thing for writing... I'll have a look
Mat
EDIT: just tested it and it is correct in C as well...
Another EDIT: I'm assuming fprintf therefore writes to a fileAnd when you're in need of someone
My heart won't deny you
Cus so many seem so lonely
With noone left to cry too baby0 -
See the file i/o, structures and arrays sections of this C Programming Tutorial.
Note that the comparison operators (==, <=, etc.) don't work on structs. To compare structs, you have to compare individual fields, e.g.:
if (s1.x==s2.x && s1.y==s2.y) ...
It is not possible to write if (s1==s2).0 -
I haven't done C in about 15 years but do you have to specifically use structures, to me it sounds something that you should be useing regular expressions for
IvanI don't care about your first world problems; I have enough of my own!0 -
To compare structs, you have to compare individual fields,
Or you can be clever and use a union and bitfields. I forget the precise syntax offhand.
For the unique part I guess you'll have to keep a list of all the IDs you've read and compare against that when you read in a new one. With a letter and three digits there's only 26,000 possibilities so you could just allocate an array with 26,000 entries (ints, bools or even bits if you want to save space), clear it at the start and set the appropriate entry when you read that value.0 -
IvanOpinion wrote:to me it sounds something that you should be useing regular expressions for
I'd agree as its what regular expressions are for. You just need to make sure that whatever C library you are using has a regular expression package.0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 352.5K Banking & Borrowing
- 253.7K Reduce Debt & Boost Income
- 454.5K Spending & Discounts
- 245.5K Work, Benefits & Business
- 601.4K Mortgages, Homes & Bills
- 177.6K Life & Family
- 259.4K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards