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

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?

Comments

  • matL_2
    matL_2 Posts: 222 Forumite
    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 file
    And 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 baby
  • Chippy_Minton
    Chippy_Minton Posts: 3,339 Forumite
    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).
  • IvanOpinion
    IvanOpinion Posts: 22,136 Forumite
    Part of the Furniture 10,000 Posts Name Dropper Combo Breaker
    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

    Ivan
    I don't care about your first world problems; I have enough of my own!
  • movieman
    movieman Posts: 383 Forumite
    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.
  • tewboss
    tewboss Posts: 23 Forumite
    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.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

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

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.