How to use the C++ "strcpy" and "strcat"?

Asked

Viewed 1,169 times

-2

How to use the strcpy and the strcat to take the names of the txt files in a directory inside the application in a folder called value.

  • 2

    Can you show something you already have? Your question seems too wide, there are several ways to read text files, for example. Which compiler is used? Using an IDE?

  • have some files txt and I would like to take that name by the method quoted in the question.

1 answer

2

The functions (strcpy and strcat) and the listing of the files of a directory are very different subjects, strcpy and strcat are just functions to copy and contact strings and are used in this case only to generate the directory where the files will be listed and not to list the files in a directory.

Both string management and directory listing can be done in many ways, in most cases "dirent is used. h" to perform a directory listing, but I consist of using the Win32 API itself to accomplish this task.

This is the way I find it easier when using the Win32 API:

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

using namespace std;

vector<wstring> GetFilesFromDirectory(wstring strDir)
{
   vector<wstring> AllFiles;
   WIN32_FIND_DATA ffd;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   //
   strDir += L"\\*";
   //
   hFind = FindFirstFile(strDir.c_str(), &ffd);
   //
   do
   {
      if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
      {
         AllFiles.push_back(wstring(ffd.cFileName));
      }
   }
   while (FindNextFile(hFind, &ffd) != 0);
   //
   FindClose(hFind);
   return AllFiles;
}

int main()
{
    // Lista os arquivos da pasta "C:\A"
    vector<wstring> files = GetFilesFromDirectory(L"C:\\A");

    for (int C = 0; C < files.size(); C++)
    {
        // Mostra na tela
        _tprintf(L" %s\n", files[C].c_str());
    }

    system("PAUSE");

}

As shown in the above example, at no time was strcpy or strcat used because the wstring iterator was used that facilitated the work of concatenating the string.

To use char instead of wstring just do it as follows:

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

using namespace std;

vector<string> GetFilesFromDirectory(wstring strDir)
{
   vector<string> AllFiles;
   WIN32_FIND_DATA ffd;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   //
   strDir += L"\\*";
   //
   hFind = FindFirstFile(strDir.c_str(), &ffd);
   //
   do
   {
      if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
      {
         wstring wideStr(ffd.cFileName);
         AllFiles.push_back(string(wideStr.begin(), wideStr.end()));
      }
   }
   while (FindNextFile(hFind, &ffd) != 0);
   //
   FindClose(hFind);
   return AllFiles;
}

int main()
{
    // Lista os arquivos da pasta "C:\A"
    vector<string> files = GetFilesFromDirectory(L"C:\\A");

    for (int C = 0; C < files.size(); C++)
    {
        // Mostra na tela
        printf(" %s\n", files[C].c_str());
    }

    system("PAUSE");

}

This is the result of the execution and the folder listed: inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.