Solved

Write to file 5 bytes before EOF

Posted on 2004-08-17
8
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

705 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