首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

看似一个简单的有关问题。关于文件指针

2013-01-07 
看似一个简单的问题。。。。。。。关于文件指针本帖最后由 setoy 于 2012-12-13 20:12:53 编辑怎么输出文件指针对

看似一个简单的问题。。。。。。。关于文件指针
本帖最后由 setoy 于 2012-12-13 20:12:53 编辑 怎么输出文件指针对应文件的文件名呢?

是linux,命令提示符下编程,文件名仅为英文字母



void output_file_name (FILE *f1, FILE *f2)
{
 //在这里输出f1的文件名,怎么搞?
}




[解决办法]
这个真没见过。
[解决办法]
f1不是通过文件名的得到的指针么?
[解决办法]
查一下有木有什麽系統函數可用,
[解决办法]
typedef struct{
fstream fs;
string file_name;
}my_fstream;
[解决办法]
GetFileInformationByHandleEx
[解决办法]
这个题目很。。。。。。
[解决办法]
这个还真搞不定。。。。
[解决办法]
引用:
引用:win7下用
GetFinalPathNameByHandle

linux下的,纯字符界面

Linux下本人没搞过。楼主参考7楼代码?
再来一个windows XP下的供参考:
//Obtaining a File Name From a File Handle
//
//GetFinalPathNameByHandle, introduced in Windows Vista and Windows Server 2008, will return a path from a handle.
//If you need to do this on earlier releases of Windows, the following example obtains a file name from a handle
//to a file object using a file mapping object. It uses the CreateFileMapping and MapViewOfFile functions to create
//the mapping. Next, it uses the GetMappedFileName function to obtain the file name. For remote files, it prints
//the device path received from this function. For local files, it converts the path to use a drive letter and prints
//this path. To test this code, create a main function that opens a file using CreateFile and passes the resulting
//handle to GetFileNameFromHandle.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <psapi.h>
//#include <strsafe.h>

#define BUFSIZE 512

BOOL GetFileNameFromHandle(HANDLE hFile)
{
  BOOL bSuccess = FALSE;
  TCHAR pszFilename[MAX_PATH+1];
  HANDLE hFileMap;

  // Get the file size.
  DWORD dwFileSizeHi = 0;
  DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);

  if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
  {
     _tprintf(TEXT("Cannot map a file with a length of zero.\n"));
     return FALSE;
  }

  // Create a file mapping object.


  hFileMap = CreateFileMapping(hFile,
                    NULL,
                    PAGE_READONLY,
                    0,
                    1,
                    NULL);

  if (hFileMap)
  {
    // Create a file mapping to get the file name.
    void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);

    if (pMem)
    {
      if (GetMappedFileNameA (GetCurrentProcess(),
                             pMem,
                             pszFilename,
                             MAX_PATH))
      {

        // Translate path with device name to drive letters.
        TCHAR szTemp[BUFSIZE];
        szTemp[0] = '\0';

        if (GetLogicalDriveStrings(BUFSIZE-1, szTemp))
        {
          TCHAR szName[MAX_PATH];
          TCHAR szDrive[3] = TEXT(" :");
          BOOL bFound = FALSE;
          TCHAR* p = szTemp;

          do
          {
            // Copy the drive letter to the template string
            *szDrive = *p;

            // Look up each device name
            if (QueryDosDevice(szDrive, szName, MAX_PATH))
            {
              size_t uNameLen = _tcslen(szName);

              if (uNameLen < MAX_PATH)
              {
                bFound = _tcsnicmp(pszFilename, szName, uNameLen) == 0
                         && *(pszFilename + uNameLen) == _T('\\');



                if (bFound)
                {
                  // Reconstruct pszFilename using szTempFile
                  // Replace device path with DOS path
                  TCHAR szTempFile[MAX_PATH];
                  sprintf(szTempFile,
                            TEXT("%s%s"),
                            szDrive,
                            pszFilename+uNameLen);
                  strcpy(pszFilename, szTempFile);
                }
              }
            }

            // Go to the next NULL character.
            while (*p++);
          } while (!bFound && *p); // end of string
        }
      }
      bSuccess = TRUE;
      UnmapViewOfFile(pMem);
    }

    CloseHandle(hFileMap);
  }
  _tprintf(TEXT("File name is %s\n"), pszFilename);
  return(bSuccess);
}

int _tmain(int argc, TCHAR *argv[])
{
    HANDLE hFile;

    if( argc != 2 )
    {
        _tprintf(TEXT("This sample takes a file name as a parameter.\n"));
        return 0;
    }
    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        _tprintf(TEXT("CreateFile failed with %d\n"), GetLastError());
        return 0;
    }
    GetFileNameFromHandle( hFile );
}


[解决办法]
纠正一下,直接定位/proc/pid/fd目录下的符号链接文件就可以了。

void output_file_name(FILE* fp)


{
    char target_path[256];
    char file_path[256];
    int fd=fileno(fp);
    pid_t pid=getpid();

    sprintf(file_path,"/proc/%d/fd/%d",pid,fd);
    int len=readlink(file_path,target_path,256);
    if(len==-1){
        printf("readlink error!");
        return;
    }
    target_path[len]='\0';
    printf("Path=%s\n",target_path);

}


[解决办法]
真能绕,你不能说:“我需要个文件名,不要FILE*”吗?

因为你需要啊。。。
[解决办法]
暂且不论这个问题能不能实现,大家有没有觉得这个题目有问题。
莫非调用fopen的时候你不传文件名,能得到文件指针吗?
你如果确实想要文件名,加个参数char* filename难道不可以吗?
[解决办法]
定义成类吧,重载一下运算符。你CString的(LPCTSTR)
重截operator FILE
这样就可以了。  

热点排行