Solved

Determine FileSize and Date with Visual C++

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copying WordPress Pages 5 88
White board coding practice 3 92
trigs fail! I thought I knew how to do trignometry 3 76
Finding Divisors 5 20
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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