Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1

    [C] - Numero e nome di files presenti in una cartella

    Salve a tutti,

    ho questo problema risolto in codice java... ma in C come lo risolvo?

    esempio 8. “realizzare un programma che acquisisca due stringhe corrispondenti
    al nome di una Directory e all’estensione dei file da ricercare su disco C:/.. e stampi
    la lista dei file della directory che hanno l’estensione desiderata.” Richieste:
    Risolverlo acquisendo le due stringhe come argomento del main().

    Codifica:
    Codice PHP:
     
     import java
    .io.*;
     
     class 
    cap10_es_08
     
    {
       public static 
    void main(String arg[])
       {
          
    String nome="",est=""// (1)
          
    if (arg.length>=1
             
    nome=arg[0]; // (2)
          
          
    if (arg.length>=2
             
    est=arg[1]; // (3)
       
          
    File f=new File(nome); // (4)
          
    File a[]=f.listFiles(); // (5)
          
    for (int i=0i<a.length;i++)
          {
             
    String s=a[i].toString(); // (6)
             
    s=s.substring(s.length()-4); // (7)
             
    boolean b=(s.equalsIgnoreCase(est)); // (8)
          
             
    if ((a[i].isFile()) && b// (9)
                
    System.out.println(a[i].getName());
          }
       }
     } 
    Grazie a tutti... Non è che sia proprio ignorante di C, ma non conosco nessuna funzione standard ansi che permetta di ottenere i nomi dei files presenti in una directory...

  2. #2
    In effetti per elencare i file e le cartelle non c'è nulla di standard ANSI... su che sistema operativo stai lavorando?
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    in effetti lavoro su windows, ma mi interesserebbe anche per linux...

    Grazie

  4. #4

  5. #5
    Per Windows ci sono queste api (con esempi) :
    FindFirstFile
    FindNextFile
    FindClose
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  6. #6

  7. #7
    ragazzi ho preso il codice di esempio delle msdn...ma non compila, utilizzo il dev-cpp
    Qualcuno più esperto sulle API mi dirà dove sbaglio...

    Codice PHP:
    #define _WIN32_WINNT 0x0501

    #include <windows.h>
    #include <stdio.h>
    #include <malloc.h>
    #include <tchar.h> 
    #include <wchar.h> 
    #include <strsafe.h>

    #define BUFSIZE MAX_PATH

    int _tmain(int argcTCHAR *argv[])
    {
       
    WIN32_FIND_DATA FindFileData;
       
    HANDLE hFind INVALID_HANDLE_VALUE;
       
    DWORD dwError;
       
    LPTSTR DirSpec;
       
    size_t length_of_arg;

       
    DirSpec = (LPTSTRmalloc (BUFSIZE);
       
       
    // Check for the directory to query, specified as 
       // a command-line parameter; otherwise, print usage.
       
    if(argc != 2)
       {
           
    _tprintf(TEXT("Usage: Test <dir>\n"));
          return 
    2;
       }

       
    // Check that the input is not larger than allowed.
       
    StringCbLength(argv[1], BUFSIZE, &length_of_arg);
       if (
    length_of_arg > (BUFSIZE 2))
       {
          
    _tprintf(TEXT("Input directory is too large.\n"));
          return 
    3;
       }

       
    _tprintf (TEXT("Target directory is %s.\n"), argv[1]);

       
    // Prepare string for use with FindFile functions.  First, 
       // copy the string to a buffer, then append '\*' to the 
       // directory name.
       
    StringCbCopyN (DirSpecBUFSIZEargv[1], length_of_arg+1);
       
    StringCbCatN (DirSpecBUFSIZETEXT("\\*"), 2*sizeof(TCHAR));

       
    // Find the first file in the directory.
       
    hFind FindFirstFile(DirSpec, &FindFileData);

       if (
    hFind == INVALID_HANDLE_VALUE
       {
          
    _tprintf (TEXT("Invalid file handle. Error is %u.\n"), 
                    
    GetLastError());
          return (-
    1);
       } 
       else 
       {
          
    _tprintf (TEXT("First file name is: %s\n"), 
                    
    FindFileData.cFileName);
       
          
    // List all the other files in the directory.
          
    while (FindNextFile(hFind, &FindFileData) != 0
          {
             
    _tprintf (TEXT("Next file name is: %s\n"), 
                       
    FindFileData.cFileName);
          }
        
          
    dwError GetLastError();
          
    FindClose(hFind);
          if (
    dwError != ERROR_NO_MORE_FILES
          {
             
    _tprintf (TEXT("FindNextFile error. Error is %u.\n"), 
                       
    dwError);
             return (-
    1);
          }
       }

       
    free(DirSpec);
       return (
    0);


  8. #8
    Che errore ti dà?
    Amaro C++, il gusto pieno dell'undefined behavior.

  9. #9
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Con Visual C funziona perfettamente, a parte questa

    TEXT("\*")

    che deve essere

    TEXT("\\*"),

  10. #10
    Forse il tuo dev-c++ non ha (come non aveva il mio) questo importante file di include strsafe.h
    Scaricalo e mettilo nella cartella del tuo progetto, o nella cartella "\Dev-Cpp\include\"

    PS. Postare esattamente gli errori che hai, ci aiuterebbe ad aiutarti.
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.