Solved

Write to file 5 bytes before EOF

Posted on 2004-08-17
8
408 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 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