上午的四个coding问题

 

VS2010 && MFC

1,如何读取一个文件夹中的所有文件?

有代码如此:

#include <fstream.h>
#include <afx.h>
#include <string.h>
struct File{
char FileName[20];
char FilePath[50];
int Length;
char r[1000];
};
File * FileFind()
{
CFileFind finder;
File fp[100];
int i=0,number;
BOOL bWorking = finder.FindFile("D:\\a\\*.txt");
/*如果想要文件夹中还有别的文件夹,可以用BOOL IsDots() const;
该函数用来判断查找的文件属性是否是“.”,“..”,非0表示是,0表示不是。必须在执行了FindNextFile()后该函数才能执行成功进行判断 */
while (bWorking)
{
  bWorking = finder.FindNextFile();
  strcpy(fp[i].FileName, (LPCTSTR) finder.GetFileName());
  strcpy(fp[i].FilePath,(LPCTSTR) finder.GetFilePath());
  cout<<fp[i].FileName<<'\n'<<fp[i].FilePath<<endl;
  i++;
}
number=i;
cout<<number<<endl;
finder.Close();

int j;

for(i=0;i<number;i++)
{
  ifstream file(fp[i].FilePath);
  j=0;
  while(!file.eof())
  {
    file>>fp[i].r[j];
  }
  j--;
  fp[i].r[j]='\0';
  fp[i].Length=j;
  cout<<fp[i].Length<<endl;
  cout<<fp[i].r<<endl;

  file.close();
 }
}

2,按上述代码初写,编译通不过,会有关于MFC dll相关的问题

解决办法:

  project->settings->general->Use MFC in a shared DLL(#include <afx.h>的原因)

3,上述设置好了之后,如若#include <afx.h>的位置不对,会出现一下错误
  WINDOWS.H already included

  这个错误产生的原因,是afx.h文件必须在WINDOWS.H前面编译才能正常完成,如若无其它情况,将其放在最上方,即最先#include

4,这些好了之后,有可能还会出现以下warning

error : Please use the /MD switch for _AFXDLL builds 

解决办法是:工程(Project)->属性(Properties)->配置属性(Configuration Properties)->c/c++->代码生成(Code Generation)->运行时库(Use run-time library)->多线程调试DLL(/MDd)(Multithreaded DLL/Debug Multithreaded DLL)

posted @ 2012-09-06 11:30  Moondark  阅读(368)  评论(0编辑  收藏  举报