Solved

Write to file 5 bytes before EOF

Posted on 2004-08-17
8
416 Views
Last Modified: 2010-04-15
_file = fopen("../src/bleh.lst", "a+");
      char buf[256];
                fseek(_file, -4, SEEK_END);
      fprintf(_file, buf);
      fclose(_file);


Long story short, it works fine but I need it to append to the file in a different initial position..
Someone help me out?
0
Comment
Question by:BangorCC
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 175 total points
ID: 11822593
That should be more like

 _file = fopen("../src/bleh.lst", "a+");
    char buf[256];
               fseek(_file, 5, SEEK_END);
    fprintf(_file, buf);
    fclose(_file);

Alternatively, you could

struct stat st;

stat("../src/bleh.lst", &st);

_file = fopen("../src/bleh.lst", "a+");
    char buf[256];
               fseek(_file, st.st_size - 5, SEEK_SET);
    fprintf(_file, buf);
    fclose(_file);
0
 
LVL 22

Accepted Solution

by:
grg99 earned 75 total points
ID: 11822753
???

I don't see anywhere where anything is put into "buf".

I don't see where you check that "_file" did receive a valid file ptr.

What if the file is less than 4 chars long?  Does the right thing happen?

The info one gets from stat() is always out of date.  Using its info to seek on a growing file isnt going to work well every time.

Using fprintf() in this fashion is mighty dangerous... What if buf happens to contain some %-sign format directives?

0
 
LVL 86

Expert Comment

by:jkr
ID: 11822981
>>The info one gets from stat() is always out of date.  

Interesting. I wonder why that isn't regarded a bigger problem than Y2k....
0
 

Author Comment

by:BangorCC
ID: 11823911
Like I said, it works fine
I figured out my problem, here's your points, have a nice day
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:BangorCC
ID: 11823929
+a argument _always_ starts at the end of a file when you write to it... solution is to open in +r mode, and fseek, SEEK_END it :-) Thanks though.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11824005
May I ask why you *accept* a comment as an answer  which does not address your question at all but maily states some unrelated (and partially incorrect) information?
0
 
LVL 22

Expert Comment

by:grg99
ID: 11825293
Which part is partially incorrect?   :)

{  stat() can only tell you what the stats *were*, not what they are right now. )



0
 
LVL 86

Expert Comment

by:jkr
ID: 11825417
If you use 'stat()' before you open the file to wite 5 bytes before the end, the information will be accurate. That's what I suggested. However, I'll take that back since I apparently missed the "growing file" part :o)

Oh, and the "unrelated part" also, since the remark addressing 'fprintf()' is a good point.

Sorry about being a bit hasty *g*
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now