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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to display specific file in a folder using c program in ubuntu?

how to display specific file in a folder using c program in ubuntu?

Scheduled Pinned Locked Moved C / C++ / MFC
linuxhelptutorialquestion
10 Posts 2 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.
  • M Offline
    M Offline
    mybm1
    wrote on last edited by
    #1

    #include // for scandir
    #include // for alphasort and structure dirent
    #include // for MAXPATHLEN and getwd
    #include #define FALSE 0
    #define TRUE !FALSE

    extern int alphasort();// extern extend the visiblity of the function alphasort
    //alphasort used as the comparison function for the scandir() line no 24 function to sort the directory

    char pathname[MAXPATHLEN];

    main() { int count,i;
    struct direct **files;//pointer to a pointer ,which point to the object call files here
    int file_select();

    			if (getwd(pathname) == NULL ) // return absolute file name of current directory
    					{ printf("Error getting path\\n");
    							exit(0);
    					}
    			printf("Current Working Directory = %s\\n",pathname);
    			count =
    			  scandir(pathname, &files, file\_select, alphasort);//scandir returns the current directory (.) and the 									directory above this (..) as well as all files so we need to check
    
    			/\* If no files found, make a non-selectable menu item \*/
    			if 		(count <= 0)
    					{		 printf("No files in this directory\\n");
    							exit(0);
    					}
    			printf("Number of files = %d\\n",count);
    			for (i=1;id\_name);
    			printf("\\n"); /\* flush buffer \*/
    	}
    

    int file_select(struct direct *entry)
    { char *ptr;
    char *rindex(char *s, char c);
    if ((strcmp(entry->d_name, ".") == 0) ||
    (strcmp(entry->d_name, "..") == 0))
    return (FALSE);
    // checking of its extension
    ptr = rindex(entry->d_name, '.');
    if ((ptr != NULL) &&
    ((strcmp(ptr, ".c") == 0) ||
    (strcmp(ptr, ".txt") == 0) ||
    (strcmp(ptr, ".m") == 0) ))
    return (TRUE);
    else
    return (FALSE);
    }

    debina

    M 1 Reply Last reply
    0
    • M mybm1

      #include // for scandir
      #include // for alphasort and structure dirent
      #include // for MAXPATHLEN and getwd
      #include #define FALSE 0
      #define TRUE !FALSE

      extern int alphasort();// extern extend the visiblity of the function alphasort
      //alphasort used as the comparison function for the scandir() line no 24 function to sort the directory

      char pathname[MAXPATHLEN];

      main() { int count,i;
      struct direct **files;//pointer to a pointer ,which point to the object call files here
      int file_select();

      			if (getwd(pathname) == NULL ) // return absolute file name of current directory
      					{ printf("Error getting path\\n");
      							exit(0);
      					}
      			printf("Current Working Directory = %s\\n",pathname);
      			count =
      			  scandir(pathname, &files, file\_select, alphasort);//scandir returns the current directory (.) and the 									directory above this (..) as well as all files so we need to check
      
      			/\* If no files found, make a non-selectable menu item \*/
      			if 		(count <= 0)
      					{		 printf("No files in this directory\\n");
      							exit(0);
      					}
      			printf("Number of files = %d\\n",count);
      			for (i=1;id\_name);
      			printf("\\n"); /\* flush buffer \*/
      	}
      

      int file_select(struct direct *entry)
      { char *ptr;
      char *rindex(char *s, char c);
      if ((strcmp(entry->d_name, ".") == 0) ||
      (strcmp(entry->d_name, "..") == 0))
      return (FALSE);
      // checking of its extension
      ptr = rindex(entry->d_name, '.');
      if ((ptr != NULL) &&
      ((strcmp(ptr, ".c") == 0) ||
      (strcmp(ptr, ".txt") == 0) ||
      (strcmp(ptr, ".m") == 0) ))
      return (TRUE);
      else
      return (FALSE);
      }

      debina

      M Offline
      M Offline
      mybm1
      wrote on last edited by
      #2

      the output is displaying file and folder from desktop but i want to display the file inside the folder with specific extension...

      L 1 Reply Last reply
      0
      • M mybm1

        the output is displaying file and folder from desktop but i want to display the file inside the folder with specific extension...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You have not told us what path is returned from getwd so we cannot answer that. You should also read the notes at http://linux.about.com/library/cmd/blcmdl3_getwd.htm[^] on why this function is deprecated.

        M 1 Reply Last reply
        0
        • L Lost User

          You have not told us what path is returned from getwd so we cannot answer that. You should also read the notes at http://linux.about.com/library/cmd/blcmdl3_getwd.htm[^] on why this function is deprecated.

          M Offline
          M Offline
          mybm1
          wrote on last edited by
          #4

          I am not able to get you path means? but this program is displaying the file present in desktop..

          L 1 Reply Last reply
          0
          • M mybm1

            I am not able to get you path means? but this program is displaying the file present in desktop..

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            What is the full directory path that gets stored in pathname by the getwd function?

            M 1 Reply Last reply
            0
            • L Lost User

              What is the full directory path that gets stored in pathname by the getwd function?

              M Offline
              M Offline
              mybm1
              wrote on last edited by
              #6

              /home/saikat/Desktop

              L 1 Reply Last reply
              0
              • M mybm1

                /home/saikat/Desktop

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Well that seems clear. How exactly do you think that using that path for your scandir will return files from a different folder?

                M 1 Reply Last reply
                0
                • L Lost User

                  Well that seems clear. How exactly do you think that using that path for your scandir will return files from a different folder?

                  M Offline
                  M Offline
                  mybm1
                  wrote on last edited by
                  #8

                  Actually that was my problem of not working with different location ...that y I uploaded to find solution over here.. :(

                  L 1 Reply Last reply
                  0
                  • M mybm1

                    Actually that was my problem of not working with different location ...that y I uploaded to find solution over here.. :(

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I don't see a problem; you get the path of the desktop and list the files in it. If you want to list the files of a different folder then get the correct path for the folder in question. And don't use getwd (or getcwd) unless you are certain that you are rooted in the correct directory to start with. Use a different method to select the folder you wish to list.

                    M 1 Reply Last reply
                    0
                    • L Lost User

                      I don't see a problem; you get the path of the desktop and list the files in it. If you want to list the files of a different folder then get the correct path for the folder in question. And don't use getwd (or getcwd) unless you are certain that you are rooted in the correct directory to start with. Use a different method to select the folder you wish to list.

                      M Offline
                      M Offline
                      mybm1
                      wrote on last edited by
                      #10

                      thank you ...counted I will kip in mind...

                      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