Program help

This is a discussion on Program help within the C Programming forums, part of the General Programming Boards category; I am to write a program that opens a file (input.txt) that contains unsigned values. The program has to ask ...

Program help

I am to write a program that opens a file (input.txt) that contains unsigned values. The program has to ask the user for a couple of commands. The first command is "f <value>". This will mean that the user inputs "f" and then a inputs a value 0-3. The value inputted pretains to a certain nibble value. if user inputs "f" and a value, the program searches through the file for which numbers have that inputted value and in which nibble. The program then outputs which item it was and which nibble it was. for example, the file contains 271, 17767, 35243, 52719, 257.

My question is how do I get the program to check the items in the file to see which nibbles match the inputted value. So far, I have gotten the program to actually read the file for the items and just output them in 4 digit hexadecimal. I know I am to use a for loop for the "f <value>". I just don't know how to actually get the program to check and mask for the nibble value that is inputted and which items actually contain that value in which nibble. If anything is confusing in my explanation, please let me know. Thank you in advance for any help.

oh, ok. Understood. But how do I get the program to check nibble values? Is this where I would have to mask the items in the file so the program can check and compare each nibble value with the user input?

That's why I asked if you knew how to get nibble values. You have a number that you read from the file. It has some nibbles in it. You need to get those nibble values, check each of them against the target, and if they match print it out.

well I kind of know how to get the nibble values. I am still confused on it though. The whole masking thing is still strange to me. Am I correct to use the & operator to mask? If so, how would I go about doing that?

You will need to get each of the nibbles n1, n2, n3, and n4 by removing/masking out the other nibbles, and then moving/shifting the bits you have to the appropriate place (i.e., in the example above n1 is 5, not 20480 or 0x5000).

alright, thank you so much. I understand the masking and it is working on my program right now. Now i have to do that for all the nibbles in the value and then check to see if it matches the user input and print it if it does. I'm sure ill run into another problem so I'll be back when I do. Again, you have been great help to me, thank you.