Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking for advice about bit operators and string handling

Status
Not open for further replies.

robk3

Programmer
Jul 3, 2006
11
US
I came to this forum for the first time yesterday looking for help with what seems to have been a compiler bug. Having received great help, I have decided to stick around here for a little while and join the community.

In the mean time, I was wondering if anyone had any good reccomendations on textx on the following 2 topics:

A good reference on string operations and reading files.
A good reference on bit operations.

I have the Kochan book, which explains the bit operations and how they work, just not why I'd ever use them. I'd like some information about why this is useful.

As for the strings and files, I read in a lot of files in my everyday programming as a psychometric researcher, and I rely on fgets and fscanf, and feel like every technique I use must have a better option. My code feels really amateurish in this area.

thanks for any help.

rob
 
By bit operations do you mean tips and tricks that you can do with bit operators or just how to do bit operations?
 
I am looking for tips and tricks that can be done with bit operators. The kochan book clearly shows how to perform the bit operations, just gives no reasoning behind why they exist or when I would ever use them.

rob
 
Well, as a global theory, I wouldn't mind about using something I don't really need to use.

To my understanting, there are few things you can't do without bit operations.

The common use, appart from the cases where I'm dealing with real binary data, like simulations and so on, is for flags.

You can use an integer as a combination of boolean variables, asigning a meaning to each of it bits, and using bit operations (masks) to access that value. But that's equivalent to have one int per value acting as a boolean you just save space.

Cheers,
Dian
 
I believe you said you were interested in learning C++. In that case, you may want to have a look at the STL string class (in the <string> header, note the lack of .h). The string class does all the memory allocation for you and lets you append data to the end, middle, beginning... You can find substrings, delete substrings... Much safer and cleaner than the C way of using strings.
 
actually, I have little to no interest in C++. I went through one of those C++ books a couple years back, and went with objc instead. Of course, I am primarily a mac programmer, so ObjC makes a lot of sense for me.

So I am still looking for a nice treatise on string handling methodologies and techniques if there is one for C.

rob
 
I'm not sure how much you already know about C strings, but regarding the nice string handling techniques... I don't believe there are any techniques in C that I'd consider 'nice'.
There are two kinds of strings:
1. Regular char arrays. Ex. char str[80];
Those get deleted automatically, but they can't be enlarged.
2. Dynamically allocated strings.
Ex. char* str = malloc(80);
The size of those are enlargable, but you need to remember to free() that memory in every possible scenario where your code might exit from the function you're in (assuming you're not returning the pointer).

I won't bore you with other info you probably already know, but if you have any specific questions about what you'd like to do with strings, I can probably give a better response.
 
I am mostly looking for ellegant ways at reading in data from files and parsing out the useful data.

I soetimes use fgets to read in the beginning of a line, test to see if it has the indicator that the line contains the info I need, and then fscanf in the data I want from the remainder of the line.

I am probably going to start using fgets to read in the whole line, test to see if it has the data I want and then sscanf the data that was read in.

Is there a better way than this?
 
If you know the max length of each line, that will work.
If the lines can be any length, you can't guarantee that fgets() will read a whole line. In that case, you might have to use fgets() to read up to n characters, check for a newline, and if there's no newline, allocate a char* buffer and copy in one chunk at a time until fgets() hits the end of the line...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top