0

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
        }
}
3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by T&T
0

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);
    }
}
0

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
    // ...
}
0

Thank you so much ,

Ancient Dragon
mitmkar

I appreciate your help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.