digitalmars.D.learn - Check whether a file is empty.
- Vino (8/8) Dec 08 2017 Hi All,
- Kagamin (3/3) Dec 08 2017 Other functions that can be used for this are
- FreeSlave (4/12) Dec 08 2017 Was it the same file on Windows 7 and Windows 2003?
- vino (8/23) Dec 08 2017 Hi,
- codephantom (33/40) Dec 08 2017 It certainly sounds like some kind of encoding issue.
- FreeSlave (6/13) Dec 09 2017 What do you mean exactly by empty log file? If the file looks
Hi All, Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes. From, Vino.B
Dec 08 2017
Other functions that can be used for this are GetFileInformationByHandle, GetFileSizeEx, SetFilePointerEx or File.size in phobos.
Dec 08 2017
On Friday, 8 December 2017 at 09:40:18 UTC, Vino wrote:Hi All, Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes. From, Vino.BWas it the same file on Windows 7 and Windows 2003? Maybe the file on Windows 2003 had a byte order mark or carriage return + newline characters.
Dec 08 2017
On Friday, 8 December 2017 at 12:25:19 UTC, FreeSlave wrote:On Friday, 8 December 2017 at 09:40:18 UTC, Vino wrote:Hi, The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty. From, Vino.BHi All, Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes. From, Vino.BWas it the same file on Windows 7 and Windows 2003? Maybe the file on Windows 2003 had a byte order mark or carriage return + newline characters.
Dec 08 2017
On Friday, 8 December 2017 at 19:13:20 UTC, vino wrote:Hi, The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty. From, Vino.BIt certainly sounds like some kind of encoding issue. I guess you don't have much choice other than to read the contents of the file, and determine what it actually contains. Assuming these log files of yours are 'ascii text' files, then if the file is less than say .. 5 bytes.. you could check if it *only* contained *non-printable* characters, in which case it's 'likely' an empty file. something very silly, like this, might do it ;-) // ------- bool isFileLikelyEmpty(string filename) { import std.exception, std.file, std.ascii, std.conv; File f = filename; enforce(f.size < 5, "This file is not likely to be empty." ~ " No point in continuing."); // 5 seems reasonable cutoff ;-) bool result = false; int charCount = 0; auto str = readText(filename); foreach(c; str) { // https://dlang.org/phobos/std_ascii.html#isPrintable if( !isPrintable(c) ) charCount++; } if(charCount == str.length) result = true; // file is likely empty, as all characters are non-printable. return result; } // ---------
Dec 08 2017
On Friday, 8 December 2017 at 19:13:20 UTC, vino wrote:Hi, The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty. From, Vino.BWhat do you mean exactly by empty log file? If the file looks blank in notepad it does not mean it's empty. What are those 2 bytes? Which program does create this file? It may work differently depending on the system or different versions are installed.
Dec 09 2017