How to set EOF of a file?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > How to set EOF of a file?




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How to set EOF of a file?  
vansky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-06 01:11 PM

hi all,

im implementing the following func:

CFile.SetLength(int len)
{
...

Seek(len, begin);

fputc(EOF, m_hFile);

}

but after runing, i use vi to open SetLengthed file and find it didnt
work!

Why and How?

thx!

Best,






[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-06 01:11 PM

In article <1137661085.764923.95930@g47g2000cwa.googlegroups.com>,
"vansky" <huangms_van@hotmail.com> wrote:

> hi all,
>
> im implementing the following func:
>
> CFile.SetLength(int len)
> {
> ...
>
> Seek(len, begin);
>
> fputc(EOF, m_hFile);
>
> }
>
> but after runing, i use vi to open SetLengthed file and find it didnt
> work!
>
> Why and How?

EOF isn't a real character, you can't write it to a file.  It's a
special value that fgetc(3) returns when it has reached the end of the
file.

Use ftruncate(2).

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***





[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-06 01:11 PM

"vansky" <huangms_van@hotmail.com> wrote:
# hi all,
#
# im implementing the following func:
#
# CFile.SetLength(int len)
# {
# ...
#
# Seek(len, begin);
#
# fputc(EOF, m_hFile);
#
# }
#
# but after runing, i use vi to open SetLengthed file and find it didnt
# work!

Because a Unix end of disk file is not a particular character or mark
within the file. The system keeps tracks of the current number of bytes
in the file and any read beyond that returns an end of file indication.

You set the disc file length with an ftruncate or similar call that
lets you tell the system what the current length is.

Other kinds of files have different of end of file conditions, or
(like a serial port) they never have an end of file. For example a
terminal file returns an end of file on control-D, a tape drive on
a tape mark, etc.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.





[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
vansky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-06 01:11 PM

hey,

thanks!

i cound not find these funcs such as ftruncate, fstat in all my
references!

could u recommend some great references about programming on linux?

thx a lot!


Best Regards!






[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
Nils O. Selåsdal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-06 01:11 PM

vansky wrote:
> hey,
>
> thanks!
>
> i cound not find these funcs such as ftruncate, fstat in all my
> references!
>
> could u recommend some great references about programming on linux?

http://www.opengroup.org/onlinepubs...s/contents.html

http://www.kohala.com/start/apue.html







[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-06 11:24 PM

"vansky" <huangms_van@hotmail.com> writes:

> hey,
>
> thanks!
>
> i cound not find these funcs such as ftruncate, fstat in all my
> references!
>
> could u recommend some great references about programming on linux?

If:

man ftruncate

doesn't produce the manual page of ftruncate on your unix system, then
you need to install the package containing the manual pages.  They
usually come with the developer tools...


--
__Pascal Bourguignon__                     http://www.informatimago.com/

"Debugging?  Klingons do not debug! Our software does not coddle the
weak."





[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
vansky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-06 01:50 AM

thx!! Sel=E5sdal

hi Bourguignon,

thx,  i love comm "man",

the problem is:

i dont know the func "fruncate" is provided at all !=20








[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-06 01:50 AM

"vansky" <huangms_van@hotmail.com> writes:

> thx!! Selåsdal
>
> hi Bourguignon,
>
> thx,  i love comm "man",
>
> the problem is:
>
> i dont know the func "fruncate" is provided at all !
>
> 

The Linux ftruncate manual page indicates that ftruncate is:

CONFORMING TO
4.4BSD,  SVr4  (these  function calls first appeared in BSD 4.2).
POSIX 1003.1-1996 has  ftruncate.   POSIX  1003.1-2001  also  has
truncate, as an XSI extension.


So if your system is not 4.4BSD, 4.5BSD, SVr4, or POSIX 1003.1-1996
compliant, perhaps you should specify more precisely what OS you're
using?


--
__Pascal Bourguignon__                     http://www.informatimago.com/

HANDLE WITH EXTREME CARE: This product contains minute electrically
charged particles moving at velocities in excess of five hundred
million miles per hour.





[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
vansky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-06 01:50 AM

hi Bourguignon,

the OS is fedora 4, and the designed SW is supposed to run well ON
RedHad 7.2 - Fedora 4, RH Enterprise 3, SuSE 8 - SuSE 9.2,  and Debian
3.

And i havnt got enough time to focus on Compatibility up to now. and i
know little about  4.4BSD,  SVr4, etc.

do u have any advice for me?

btw, i used like turncate("/test/tmp", LEN),

coz func: ftruncate(int fd, off_t len),  i dont know how to get the
para "fd",  :-(

thx.






[ Post a follow-up to this message ]



    Re: How to set EOF of a file?  
vansky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-06 07:50 AM

Hi Bourguignon,

Thanks! You are definitely a great teacher!

And sorry for careless typing.

I have to work over 12 hours per day recently. So busy and so tired !

Final code is:

void CMyFile::SetLength(DWORD dwNewLen)
{
ASSERT(m_hFile != (HNDFILE)hFileNull);

Seek((LONG)dwNewLen, (UINT)begin);

#ifdef __LINUX_VER__
truncate(m_strFileName, dwNewLen);
fflush(m_hFile);
#else
::SetEndOfFile((HANDLE)m_hFile);
#endif
}


Best Regards!






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:35 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register