Solved

Determine FileSize and Date with Visual C++

Posted on 2000-04-23
4
1,074 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 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