• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Write to file 5 bytes before EOF

_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
BangorCC
Asked:
BangorCC
  • 4
  • 2
  • 2
2 Solutions
 
jkrCommented:
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
 
grg99Commented:
???

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
 
jkrCommented:
>>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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BangorCCAuthor Commented:
Like I said, it works fine
I figured out my problem, here's your points, have a nice day
0
 
BangorCCAuthor Commented:
+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
 
jkrCommented:
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
 
grg99Commented:
Which part is partially incorrect?   :)

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



0
 
jkrCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now