Solved

Determine FileSize and Date with Visual C++

Posted on 2000-04-23
4
1,071 Views
Last Modified: 2013-12-14
I have the following source code.  When ran it returns (NULL) for nFileSizeHigh, and it gives a GPF error with nFileSizeLow.  How do I make it work so that it shows the size of the file?  Also, How do I show the date for the file?  I know this should be easy, but I am stumped.

--Code--
#include <stdio.h>
#include <io.h>
#include <time.h>
#include <string.h>
#include <process.h>
#include <windows.h>

FILE *stream;
void use_file(char *p,char *f, char *a, unsigned long s)
{
      fprintf(stream,"%s, %s, %s, %s\n",p,f,a,s);
      printf("%s, %s, %s, %s\n",p,f,a,s);
}
void process_dir(char *DIR)
{
      WIN32_FIND_DATA   wfd;
      char path[255];
      strcpy(path,DIR);
      strcat(path,"*.*");
      HANDLE FF=FindFirstFile(path,&wfd);
      if (FF == INVALID_HANDLE_VALUE) return;
      if (strcmp(wfd.cFileName,".") == 0 || strcmp(wfd.cFileName,"..") == 0 ) FindNextFile(FF,&wfd);
      if (strcmp(wfd.cFileName,".") == 0 || strcmp(wfd.cFileName,"..") == 0 ) FindNextFile(FF,&wfd);
      use_file(DIR,wfd.cFileName,wfd.cAlternateFileName, wfd.nFileSizeHigh);
      while(FindNextFile(FF,&wfd)){
            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                  char newpath[255];
                  strcpy(newpath,DIR);
                  strcat(newpath,wfd.cFileName);      
                  strcat(newpath,"\\");
                  process_dir(newpath); // go into subdirs            
            }
      use_file(DIR,wfd.cFileName,wfd.cAlternateFileName, wfd.nFileSizeHigh);
      }
}

void main()
{
      stream=fopen("c:\\tmp.dat","w");
      fprintf(stream, "DIR, FILENAME, MSDOSNAME, SIZE, TIME\n");
      process_dir("c:\\");
      fclose(stream);
}
0
Comment
Question by:vanberge
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 50 total points
Comment Utility
Try this:

void use_file(char* pDir, WIN32_FIND_DATA& wfd)
{
  SYSTEMTIME st;
  char dateStr[11];

  if (!FileTimeToSystemTime(&wfd.ftLastWriteTime, &st))
    strcpy(dateStr, "[unknown]");
  else
    sprintf(dateStr, "%d/%d/%d", st.wMonth, st.wDay, st.wYear);

  fprintf(stream, "%s, %s, %s, %s, ", pDir, wfd.cFileName, wfd.cAlternateFileName,
    dateStr);
  printf("%s, %s, %s, %s, ", pDir, wfd.cFileName,wfd.cAlternateFileName,
    dateStr);
           
#if defined(_X86_) || defined(_ALPHA_)
  __int64 nSize = wfd.nFileSizeLow + (wfd.nFileSizeHigh << 32);

  fprintf(stream, "%I64u\n", nSize);
  printf("%I64u\n", nSize);
#else
  // Ignore high size
  fprintf(stream, "%lu\n", (unsigned long) nFileSizeLow);  
  printf("%lu\n", (unsigned long) nFileSizeLow);  
#endif
}

You were formatting your printf's using a "%s" for the file size. You might just want to ignore the high portion of the file size (MFC does).

Good Luck,
Steve
0
 

Author Comment

by:vanberge
Comment Utility
SteveGTR thanks for you help.  One question though.  on this segment of code, how does it totaly work.  I sorta follow it, but I think I am missing something.  

#if defined(_X86_) || defined(_ALPHA_)
  __int64 nSize = wfd.nFileSizeLow + (wfd.nFileSizeHigh << 32);

  fprintf(stream, "%I64u\n", nSize);
  printf("%I64u\n", nSize);
#else
  // Ignore high size
  fprintf(stream, "%lu\n", (unsigned long) nFileSizeLow);    
  printf("%lu\n", (unsigned long) nFileSizeLow);    
#endif

Thanks again for your answer.

Eric
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
The file size is stored as a 64 bit integer. The low order 32 bits are stored in nFileSizeLow. The high order 32 bits are stored in nFileSizeHigh. The 64 bit unsigned value can be as high as 18,446,744,073,609,551,615. The 32 bit low order has a max of 4,294,967,295. This code will use the high order 32 bits if your machine is capable of handling it (Intel or Alpha). MFC code just ignores the high order bits in the CFile class code (filest.cpp).
0
 

Author Comment

by:vanberge
Comment Utility
Steve, cool thanks man.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now