C编程 - FindFirstFile返回Invalid_handle_value

时间:2014-11-28 14:23:48

标签: c++ visual-studio-2013 handle filesize

在过去的几个小时里我试图找到一个答案,但是我有一个函数用于获取我的一个命令行参数的文件大小,但这样做我的句柄一直在返回一个无效的值。如果有人愿意帮助我,我将不胜感激。我的功能代码如下:

int getSmallFileLength(const char *fileName, WIN32_FIND_DATA data)
{
HANDLE handle = FindFirstFile(&fileName, &data);
int fileSize = data.nFileSizeLow;

if (handle == INVALID_HANDLE_VALUE)
{
    printf("\nyou got an error.");
    return -1;
}
else
{
    printf("\nyour file size is %s", fileSize);
}

return fileSize;
}

如果主要代码有助于解决这个问题,我会发布它,但同时我正在清理它使它可读的混乱。它传入我的Win32_find_data变量和文件。

我也在使用Visual Studio 2013。

现在是主要代码:

   #pragma warning (disable:4996)
   #include <Windows.h>
   #include <stdio.h>
   #include <string.h>
   #include <stdlib.h>
   #include "cA5_proto.h"

   int main(int argc, char *argv[])
   {
     int x;
     int argNum = 1;
     int counter = 0;

printf("Command: %s\n", argv[0]);
for (argNum = 1; argNum < argc; argNum++)
{
    printf("arg #%d: %s\n", argNum, argv[argNum]);
}

if (argc != 2) /* argc should be 2 for correct execution */
{
    /* We print argv[0] assuming it is the program name */
    printf("usage: %s filename", argv[0]);
}
else
{

    // We assume argv[1] is a filename to open
    FILE *file = fopen(argv[1], "rb");
    FILE *fileOutput = fopen("contents.txt", "a");
    WIN32_FIND_DATA fileData = { 0 };
    getSmallFileLength(file,fileData);

    /* fopen returns 0, the NULL pointer, on fnailure */
    if (file == 0)
    {
        printf("Could not open file\n");
    }
    else
    {


            /* read one character at a time from file, stopping at EOF, which
            indicates the end of the file.  Note that the idiom of "assign
            to a variable, check the value" used below works because
            the assignment statement evaluates to the value assigned. */

        //getSmallFileLength(argv[1]);              //getSmallFileLength outputs either the file size or an error

        printf("\nThe contents are: \n");
        while ((x = fgetc(file)) != EOF)
        {
                                                //IN A FOR LOOP FOR THE LENGTH OF THE FILE SIZE HELPS OUTPUT A \N EVERY 10 CHARACTERS

            if (counter > 0)
            {
                if (counter == 10 || counter % 10 == 0)  //SEPERATE LINES WITH A \N EVERY TEN
                {
                    fprintf(fileOutput, "\n");

                }
            }

            if (x>10 && x<100)                  //puts a zero in front of two digit numbers in the contents output file
            {
                fprintf(fileOutput, "0");
            }
            fprintf(fileOutput, "%d ",x);



            printf("%c", x);
            counter++;

        }
        printf("\n");

        //CLOSE FILES HERE
        fprintf(fileOutput, "\n");              //PUT A \N AT THE END OF THE FILE
        fclose(fileOutput);
        fclose(file);
    }
}
return 0;

}

我从vsb得到的唯一警告如下:警告
1警告C4133:'function':不兼容的类型 - 从'FILE *'到'const char *'

2 个答案:

答案 0 :(得分:0)

尝试小测试代码

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[]){
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    printf ("Target file is %s.\n", argv[1]);
    hFind = FindFirstFile(argv[1], &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE) {
        printf ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
    } else {
        printf ("The first file found is %s\n", FindFileData.cFileName);
        printf ("The first file size is %d\n", FindFileData.nFileSizeLow);
        FindClose(hFind);
    }
    return 0;
}

FindFirstFile

答案 1 :(得分:0)

per this link:
<http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx>
the returned value, when equal to INVALID_HANDLE_VALUE means the file was not found.

in looking at your code, your passing the address of the address of the filename string 
(a meaningless value)
what should be passed is 'filename' not '&filename'
相关问题