Thread: how to read files from directory path given in console window of VC++ compiler

  1. #1
    Registered User
    Join Date
    Dec 2011

    how to read files from directory path given in console window of VC++ compiler


    how can i read each and every files in a folder one by one.Using VC++ 2008 compiler to run C program.I want to specify the folder path form console window as input then its goes to the function and will take one file from folder and should perform conversion .it should process all the files in folder path.If u have any idea to do this please help me and examples too.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Exactly how you do this, may depend on your OS. This is a rather generic approach for Windows and with a bit of touch up perhaps, for *nix as well.

    This program was written in Pelles C (a Windows only compiler), and requires that the "Define compatibility names option" be checked in the project options. I have no idea how that translates into VC++.

    It searches for a file in a directory, but doesn't print them out - which is trivial to change.

    Note that non-text files should not be opened and printed out!

    /*Remember to go into project options and check the "Define compatibility names options box. 
    That gets rid of the "Target Architect not defined", error.
    #include <dirent.h>    
    #include <stdio.h>
    #include <string.h>
    int main(void) {
       int i=0;
       DIR *dir;
       struct dirent *direntry; //could be a file, or a directory
       dir = opendir("C:/Users/Adak");
       if(!dir) {
          printf("Error: directory did not open!\n");
          return 1;
       while((direntry=readdir(dir))!=NULL) {
          if(++i < 20) 
          if((strcmp(direntry->d_name, "test.txt"))==0) {
             printf("\nThe %s file has been found\n",direntry->d_name);
             i=-99;  //just a flag value to show the file was found
          printf("\nThe test.txt file was not found\n");
       return 0;
    You'll need to read up on using the command line options as input for a C program, and make some other changes as well. Don't think of this as the program you want, served up on a platter - it is just a starting point for you.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Thanks the file is binary file ...want to open one file and want to perform the below operation.
    but how to mention take direntry->d_name value (file.txt)..and want to perform the below logic #include

      main( ) 
     FILE* fp;
     char c;   fp=fopen(
    "21-11-11.txt","r");//open  the specific file in read mode  
     if(fp==NULL)//if the file doesnt conatins  any thing or nullvalues return the below statement {
     "File not Found!!");//file not fonde in console  window. }
     else {
     do {
     // get each character from file putchar(c);
     // print each character   
     //printf("%c",c); }
     while(c!=EOF); //check until the end of file  fclose(fp);//close the file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking if char *path; is a valid directory.
    By b1nd3r in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 02:55 PM
  2. How to sequentially read all the files in a directory
    By Olaveson in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2010, 02:54 PM
  3. directory path coding
    By mr_empty in forum C++ Programming
    Replies: 30
    Last Post: 11-20-2007, 05:02 AM
  4. Accessing the 'home' directory path?
    By smoothdogg00 in forum C Programming
    Replies: 3
    Last Post: 04-30-2006, 10:51 AM
  5. include files that are'nt in compiler's directory
    By vaibhav in forum C++ Programming
    Replies: 10
    Last Post: 03-25-2006, 11:45 AM

Tags for this Thread