?
Solved

Determine FileSize and Date with Visual C++

Posted on 2000-04-23
4
Medium Priority
?
1,088 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…
Suggested Courses

840 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