TRUNCATING OPENED TEXT FILE 
Author Message
 TRUNCATING OPENED TEXT FILE

I need a c function that can truncate an open text file via a file pointer.
There are two relevent issues at hand here: 1) my callback function only has
access to the file pointer, 2) my callback function cannot access the name
(path) of the opened file.

Also, the fucntion/solution has to be platfrom independent (at least a
mechanism that'll work for Unix & NT).

Thanks for you help.

-Massis



Sun, 19 Oct 2003 05:05:45 GMT  
 TRUNCATING OPENED TEXT FILE

Quote:

> I need a c function that can truncate an open text file via a file pointer.
> There are two relevent issues at hand here: 1) my callback function only has
> access to the file pointer, 2) my callback function cannot access the name
> (path) of the opened file.

> Also, the fucntion/solution has to be platfrom independent (at least a
> mechanism that'll work for Unix & NT).

There is no solution that meets all of your requirements.
--
Just another C hacker.


Sun, 19 Oct 2003 05:25:51 GMT  
 TRUNCATING OPENED TEXT FILE
Massis Isagholian a crit dans le message ...

Quote:
>I need a c function that can truncate an open text file via a file pointer.
>There are two relevent issues at hand here: 1) my callback function only
has
>access to the file pointer, 2) my callback function cannot access the name
>(path) of the opened file.

>Also, the fucntion/solution has to be platfrom independent (at least a
>mechanism that'll work for Unix & NT).

I'm afraid you are cooked. There is no portable way to truncate a text file,
specially if you don't have the name and path of it. Are you sure you don't
have an anonymous pointer aka void * as a paremeter, pointing to some user
data where you could put the file name?

If you have no way to redesign the callback call, there is no solution, at
last for me.

Last hope: would please give more details about the calback prototype, and
the way you open the file and pass it's pointer to the (supposed) system (or
at last embedded) call.

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Sun, 19 Oct 2003 05:21:09 GMT  
 TRUNCATING OPENED TEXT FILE
The way the system is designed at the moment, I don't have access to
anything but the file pointer.  The function prototype is as follows:

    bool writeEOFSeq(FILE *fp);

As much as I hate to do this, I would use a global variable to access the
file pointer.  Given the file name is there anyway to truncate a text file
past the starting point.  I'm already aware that I can copy the contents to
a temp file and rewrite the original file.

Thanks,
 - Massis


Quote:
> Massis Isagholian a crit dans le message ...
> >I need a c function that can truncate an open text file via a file
pointer.
> >There are two relevent issues at hand here: 1) my callback function only
> has
> >access to the file pointer, 2) my callback function cannot access the
name
> >(path) of the opened file.

> >Also, the fucntion/solution has to be platfrom independent (at least a
> >mechanism that'll work for Unix & NT).

> I'm afraid you are cooked. There is no portable way to truncate a text
file,
> specially if you don't have the name and path of it. Are you sure you
don't
> have an anonymous pointer aka void * as a paremeter, pointing to some user
> data where you could put the file name?

> If you have no way to redesign the callback call, there is no solution, at
> last for me.

> Last hope: would please give more details about the calback prototype, and
> the way you open the file and pass it's pointer to the (supposed) system
(or
> at last embedded) call.

> --
> -hs-    "spaces, not tabs" email: emdel at noos.fr
> CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> ISO-C Library: http://secure.dinkumware.com/htm_cl/
> FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Sun, 19 Oct 2003 05:40:02 GMT  
 TRUNCATING OPENED TEXT FILE

Quote:
> I need a c function that can truncate an open text file via a file pointer.
> There are two relevent issues at hand here: 1) my callback function only has
> access to the file pointer, 2) my callback function cannot access the name
> (path) of the opened file.

> Also, the fucntion/solution has to be platfrom independent (at least a
> mechanism that'll work for Unix & NT).

From the C FAQ:

19.13:  How can a file be shortened in-place without completely clearing
        or rewriting it?

A:      BSD systems provide ftruncate(), several others supply chsize(),
        and a few may provide a (possibly undocumented) fcntl option
        F_FREESP.  Under MS-DOS, you can sometimes use write(fd, "", 0).
        However, there is no portable solution, nor a way to delete
        blocks at the beginning.  See also question 19.14.

--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm



Sun, 19 Oct 2003 05:41:26 GMT  
 TRUNCATING OPENED TEXT FILE
Massis Isagholian a crit dans le message ...

Quote:
>The way the system is designed at the moment, I don't have access to
>anything but the file pointer.  The function prototype is as follows:

>    bool writeEOFSeq(FILE *fp);

I see.

Quote:

>As much as I hate to do this, I would use a global variable to access the

At last, try to protect it against oddities...

Quote:
>file pointer.  Given the file name is there anyway to truncate a text file

No.

Quote:
>past the starting point.  I'm already aware that I can copy the contents to
>a temp file and rewrite the original file.

This is what I was thinking of. Not to mention that it is the safest way.
(can be undoed if you save the old version of the file with a different name
(adding a .bak extension is a common way of doing it)

file.txt -> file.bak or file.txt.bak

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Sun, 19 Oct 2003 05:51:14 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to truncate a file opened in text mode

2. Truncating an open file on UNIX

3. Truncating a binary file opened for read and write

4. Open a file with std::ofstream without truncate it

5. How do I truncate an already opened file?

6. How to truncate file opened r+?

7. Truncating large text files in C

8. Append from text file & Truncate Table

9. Easy way to truncate a text file

10. how to truncate a text file

11. Opening a text file/Displaying text line by line

12. Open a text file and search the file for a STRING of characters

 

 
Powered by phpBB® Forum Software