Solved

Determine FileSize and Date with Visual C++

Posted on 2000-04-23
4
1,072 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
ID: 2741890
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
ID: 2742518
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
ID: 2742815
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
ID: 2742888
Steve, cool thanks man.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

895 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

19 Experts available now in Live!

Get 1:1 Help Now