INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Using ftell and fseek

Using ftell and fseek

(OP)
Hi,

I am having trouble reading a file on DOS. It all worked OK, but we have been presented with files where the records are terminated with (LF)(CR)(LF) instead of the usual (CR)(LF). I am trying to amend it to cope with this.

The program loops over the file as follows:

ftell to get the file position.

fgetc until '\n'(LF) is found.

fseek to get back to the start of the record

fgets to read the record for processing

Start the loop again.

This works fine if the records are terminated with (CR)(LF), but when terminated with (LF)(CR)(LF) the program starts to go backwards in the file for a few iterations and then just stays in the same place. (This is based on printing out what is returned by various calls to ftell.)

Am I doing something fundamentally wrong here or do I just need to debug my code?

One possible way I can see round this is to manipulate the position returned by ftell to point to where it should be. Is this feasible or just totally mad? (It is opened as a text file.)

Any advice would be most welcome.

Thanks.

RE: Using ftell and fseek

Why can't you use fgets throughout instead of running up and down with ftell and fseek?

fgets will read until \n or the buffer size is reached. Just get it into the buffer and strip off a \r if one exists.

RE: Using ftell and fseek

(OP)
Thanks for the reply.

The reason, rightly or wrongly, is that it uses fgetc until it finds \n so that it knows how big the record is so that it can allocate an appropriate amount of memory for use by the fgets.

The routine has no knowledge of what the file might contain. All it knows is that it is a stream of bytes.

I have come round to thinking that perhaps I just allocate a large amount of memory (1,000 characters perhaps) and throw an error if the fgets has reached this limit.

Given that the routine has worked perfectly for 17 years common sense tells me to make any amendments with care. The fact that it does something in a seemingly odd way doesn't mean it is wrong, it could just mean that I don't understand (a likely possibility where C language programs are concerned.

Having said all that, I return to my original question of "Am I doing something fundamentally wrong here or do I just need to debug my code?".



RE: Using ftell and fseek

Nothing wrong. I'd do the allocation method - far easier and a lot faster than multiple fgetc and backspacing. Memory is cheap so why not just allocate 4096.

RE: Using ftell and fseek

(OP)
That is what I have done. After spending about two days trying to figure out what was going on it has taken me less than a day to rewrite it without the fgetc.

RE: Using ftell and fseek

That is normally the case. Sometimes, it takes longer to get an old piece of code to work than to completely rewrite it.

I've also taken 3 days battling like you did, 5 days to write the proposal and convince the bosses that it needed to be done and 1 day to code it. The most difficult is convincing the bosses because they will say "It has been working for the last 20 years so why do you need to change it?". You can offend by saying "It is badly written" or "The author used an unmaintainable solution" so you have to come up with all sorts of reasons why it needs rewriting. Far easier when there is no traceability or if you are the sole person in charge, answerable to no one for your changes.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close