Hello,

I need your help in updating the loop in this code.

the purpose of this loop is to open and read all data file in the programme area that has a name (passenger xxxx) where x is any number from 0 to 9 .

please help me,

void Print_allPass()
{
    FILE *infile;

    char ID[5]="1000",temp[20],line[100],*status;

            
        while( ( strcmp(ID,"0000") > 0 ) && ( strcmp(ID,"9999") < 0) ){

        
        strcpy (temp ,"passenger");
        strcat (temp ," ");
        strcat (temp , ID);
    
        infile = fopen (temp,"r");
        if(infile != NULL ){
            status = fgets(line, 100, infile);
         while (status != NULL) {
          
                                     puts(line);
                                      status = fgets(line, 100, infile);
        }} 

        // how can I update this loop
        }
}

You should use the functions on MS-DOS _find_first_file() and _find_next_file(), or MS-Windows win32 api FindFirstFile() and FindNextFile(). *nix use opendir() and readdir(). There are examples of those functions all over the net and in DaniWeb's Code Snippets board.

If you don't want to use those then why not something like this?

int id = 0;
for(id = 0; id < 9999; id++)
{
    char filename[255];
    char line[100];
    sprintf(filename,"passenger %04d", id);
    ifile = fopen( ... );
    if(ifile != NULL)
    {
            while( fgets(line, sizeof(line), ifile) )
                  fprintf("%s", line);
            fclose(ifile);
    }
}

Maybe sprintf() would be of use ...

char filename[20];

for(int ii = 0; ii < 10000; ii ++)
{
    sprintf(filename, "passenger %04d", ii);

    // display the file name
    puts(filename);

    // try opening and processing the file here
    // ...
}

Thank you so much ,

Ancient Dragon
mitmkar

I appreciate your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.