Solved

Write to file 5 bytes before EOF

Posted on 2004-08-17
8
434 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to design, and implement simple interface 5 137
List out all word 7 316
How to creat good software interface ? 1 67
What is sub-make ? 2 71
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 structures in the C programming language.

792 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