|
|
|
|
|
|
| Author |
Message |
Michael Mair *nix forums Guru
Joined: 19 Feb 2005
Posts: 963
|
Posted: Thu Jul 13, 2006 7:00 am Post subject:
Re: How to detect an empty file?
|
|
|
ritesh schrieb:
| Quote: | While going through this thread and writting some of my own code I
noticed this -
1. Suppose I create a text file. Write some data to it. Then close
the FILE* pointer to this file.
2. Since I have the path for the file (as char *), I can always open
the file in append mode to write more data to it.
3. What if I want to open the file in "edit" mode, and need to insert
text at the beginning of the file?
|
Then I'd rather open it in "r+" mode.
Note that you cannot "insert" text but that you have to read
all the file into a buffer, write the part to be inserted into
the file and then the buffer contents.
If you want to overwrite something of the same length, then
this of course is possible.
| Quote: | If I open using -
filePtr = fopen(filePath, "wt");
|
There is no 't' in standard C. You can leave it out.
| Quote: | then the file is truncated to zero length and I loose the previous
data.
If I open using
filePtr = fopen(filePath, "at");
then the file pointer is placed at the end of the file. If I try to
this -
rewind(filePtr);
now the filePtr moves to end of the file, since it was opened in append
mode and it allow me to move back anymore.
|
Nonsense. Read your C standard library reference of choice.
rewind() is in terms of positioning equivalent to
(void)fseek(stream, 0L, SEEK_SET)
| Quote: | I guess the only option left is to -
long int length = someFunctionToGetLengthOfFile(filePtr);
fseek(filePtr, -length, SEEK_SET);
|
Not at all. Read past threads on this issue.
| Quote: | Could someone please tell me which function would give me the length of
the text file?
|
C works with streams which are not only files.
There is no standard C function to do so as a stream could
have "infinite" length and/or could not be evaluated for length
as this would discard the actual "content".
| Quote: | Portable solutions would be best, non-portable would do
as long as they work on Linux, Unix and Solaris.
|
Have a look at POSIX functions and/or ask in comp.unix.programmer.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. |
|
| Back to top |
|
 |
av *nix forums beginner
Joined: 07 Jun 2006
Posts: 37
|
Posted: Thu Jul 13, 2006 9:25 am Post subject:
Re: How to detect an empty file?
|
|
|
On Mon, 10 Jul 2006 14:26:27 -0700, "Dann Corbit" wrote:
| Quote: | What happens if someone else appends data after you fseek()?
I don't think that the question has an answer.
|
size of file is a function of time
for example find in the time "t" the size of a file and allocate with
malloc memory == the size of file, then load that file in that memory
could be a bug if the file is more long in the time t+1 |
|
| Back to top |
|
 |
Gordon Burditt *nix forums Guru
Joined: 02 Mar 2005
Posts: 773
|
Posted: Fri Jul 14, 2006 1:05 am Post subject:
Re: How to detect an empty file?
|
|
|
| Quote: | 1. Suppose I create a text file. Write some data to it. Then close
the FILE* pointer to this file.
2. Since I have the path for the file (as char *), I can always open
the file in append mode to write more data to it.
3. What if I want to open the file in "edit" mode, and need to insert
text at the beginning of the file?
|
There is no mode to write text to the beginning of the file *AND SHOVE
THE REST OF IT TO THE BACK OF THE FILE*. If you write a line of
50 characters, you overwrite the first 50 or so [*] bytes of the
file. If you want to prepend text to the whole file, read the file,
rewind, write the new text, then write all the old stuff.
[*] Due to differences in line endings, a line might occupy more
bytes in the file than it might appear. Consider Windows and
MS-DOS \r\n line endings.
There is no mode "wt" or "at" in standard C. Not very many systems
support on-the-fly tab expansion and compression or translation to
or from Turkish, and hopefully none of them support thermonuclear
file writes.
I think the mode you are looking for is "r+" (not "r+t"), which does
not truncate the file, but will permit fseeking and writing.
| Quote: | If I open using -
filePtr = fopen(filePath, "wt");
then the file is truncated to zero length and I loose the previous
data.
If I open using
filePtr = fopen(filePath, "at");
then the file pointer is placed at the end of the file. If I try to
this -
rewind(filePtr);
now the filePtr moves to end of the file, since it was opened in append
mode and it allow me to move back anymore.
I guess the only option left is to -
long int length = someFunctionToGetLengthOfFile(filePtr);
fseek(filePtr, -length, SEEK_SET);
|
If you're thinking of doing that after opening the file in "a" mode,
it may not work. All writes may be forced to the end of the file.
| Quote: | Could someone please tell me which function would give me the length of
the text file? Portable solutions would be best, non-portable would do
as long as they work on Linux, Unix and Solaris.
|
Non-standard: the return value of ftell() after fseek(filePtr, 0L,
SEEK_END) may return the current length of the file if the seek
offset is counted in bytes (rather than, say, bytes, sectors, tracks,
cylinders, and trains, packed into one integer in bitfields). This
should work on UNIX-like systems. It may get confusing on systems
with \r\n line endings.
Non-standard: Use fstat(fileno(filePtr), &statbuffer). This should
work on UNIX-like systems.
Gordon L. Burditt |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sun Nov 23, 2008 11:32 am | All times are GMT
|
|
Mortgage Calculator | Internet Advertising | Secured Loans | Cingular Ringtones | Free Ringtone
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|