Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to retieve files from a folder and pass it in a functio called in a loop

How to retieve files from a folder and pass it in a functio called in a loop

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiodata-structurestutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Faez Shingeri
    wrote on last edited by
    #1

    Hi, I have an array of strings which contains the file names and I call a function in a loop.

    char myFiles [20][50];

    for(i=0;i<20;i++)
    myFunc( myFiles[i]);

    The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call

    myFunc( myFiles[i]);

    which I am unable to do. I used

    mkdir("input");

    to create a directory and move all my files into it also using

    system("move *gen.txt input");

    But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie

    for(i=0;i<=20;i++)
    myFunc("C:\\input\\myFiles[i]"); //which is not possible....

    I am using eclipse IDE. Thanks in advance, Faez

    J enhzflepE 2 Replies Last reply
    0
    • F Faez Shingeri

      Hi, I have an array of strings which contains the file names and I call a function in a loop.

      char myFiles [20][50];

      for(i=0;i<20;i++)
      myFunc( myFiles[i]);

      The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call

      myFunc( myFiles[i]);

      which I am unable to do. I used

      mkdir("input");

      to create a directory and move all my files into it also using

      system("move *gen.txt input");

      But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie

      for(i=0;i<=20;i++)
      myFunc("C:\\input\\myFiles[i]"); //which is not possible....

      I am using eclipse IDE. Thanks in advance, Faez

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Have a look at the str... functions. To create a full file name, you may use strcpy and strcat:

      char PathName[_MAX_PATH]; // _MAX_PATH is defined in stdlib.h
      for (i = 0; <= 20; i++)
      {
      strcpy(PathName, "C:\\input\\");
      strcat(PathName, myFiles[i]);
      myFunc(PathName);
      }

      F 1 Reply Last reply
      0
      • F Faez Shingeri

        Hi, I have an array of strings which contains the file names and I call a function in a loop.

        char myFiles [20][50];

        for(i=0;i<20;i++)
        myFunc( myFiles[i]);

        The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call

        myFunc( myFiles[i]);

        which I am unable to do. I used

        mkdir("input");

        to create a directory and move all my files into it also using

        system("move *gen.txt input");

        But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie

        for(i=0;i<=20;i++)
        myFunc("C:\\input\\myFiles[i]"); //which is not possible....

        I am using eclipse IDE. Thanks in advance, Faez

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #3

        It sounds to me like you need to concatenate the filename onto the foldername. //In that case, you may choose to do something similar to this: // 1. Get the filenames // 2. Get the folder name // 3. copy foldername to fullPath // 4. concatenate (append) myFiles[i] onto fullPath // 5. If curFileNum < maxFiles goto 3.

        char filenames[4][50] = { "001.txt", "002.txt", "003.txt", "004.txt" };
        char inputFolder[] = "inputFolder";
        int i, maxFiles=4;`

        for (i=0; i<maxFiles; i++)
        {
        strcpy(fullPath, inputFolder); // fullPath = "inputFolder"
        strcat(fullPath, "\\"); // fullPath = "inputFolder\"
        strcat(fullPath, myFiles[i]); // fullPath = "inputFolder\001.txt" - first iteration only
        myFunc(fullPath); // myFunc("inputFolder\001.txt");
        }

        [EDIT: oops! Too slow - someone has kindly furnished an answer already]

        1 Reply Last reply
        0
        • J Jochen Arndt

          Have a look at the str... functions. To create a full file name, you may use strcpy and strcat:

          char PathName[_MAX_PATH]; // _MAX_PATH is defined in stdlib.h
          for (i = 0; <= 20; i++)
          {
          strcpy(PathName, "C:\\input\\");
          strcat(PathName, myFiles[i]);
          myFunc(PathName);
          }

          F Offline
          F Offline
          Faez Shingeri
          wrote on last edited by
          #4

          That worked :-) BTW I came up another method as below

          system("copy input/*.txt "); // This line copies all the text files to pwd

          /* After manipulations with all the required files.... */

          system("del *.txt");

          Anyways, thanks for the nice logic :) Regards, Faez

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups