Hello ,

i have in a director c:\files_t several files t1 t2 t3...t300
i need to analize each on-the analize operation is not importent
how can i open each file in a loop and analize them?-in Ccode

thanks ahead

You could use sprintf() to make a string which contains your filename.
Example:

char acFilename[5];
FILE* FFile;
 
for (iCount = 1; iCount <= 300; iCount++)
{
   sprintf(acFilename, "T%d", iCount);
   if (FFile = fopen(acFilename, r) != NULL)
   {
   // analyse your file here
   fclose(FFile);
   }
}

Didn't test it, but it should get you in the right direction.

Edited 5 Years Ago by Nick Evan: n/a

on MS-Windows os, use FindFirstFile() and FindNextFile() to iterate through all the files in a folder. *nix has opendir() and readdir(). boost libraries have os-independent c++ classes.
useing these functions you don't have to know anything about the file names or how many files there are. Just check the file's attributes (also returned by those functions) to insure the file name is not a folder/directory.

thanks for the quick replay !

one more question the file is t1.txt , t2.txt....
can i add thx txt like this:sprintf(acFilename, "T%d.txt", iCount);

Thanks again

Yep, but be sure to make acFilename big enough ( so worst case: T300.txt = 8 chars + \0 = 9 elements at least) so char acFilename[9]

on MS-Windows os, use FindFirstFile() and FindNextFile() to iterate through all the files in a folder. *nix has opendir() and readdir(). boost libraries have os-independent c++ classes.

Ancient Dragon is right, if you didn't know the names of the files or how many files there are, you should use one of the mentioned functions!

Edited 5 Years Ago by Nick Evan: n/a

thanks again
but i have this problem when im trying to direct the access to the file i get en error opennig the file when i add the \
sprintf(acFilename, "c:\files\T%d.txt",iCount);


Thanks

You need to escape the \ character since the it is seen by the compiler as the language construct.

You can do something like: sprintf( acFilename, "c:\\a%d.txt", count ) ; Use \ to escape the special \ character. In C and C++ \ is the escape character used to escape some special characters like ", ' and so on..

Also I think it is not a good practice to use \\ since its not portable. If possible try using sprintf( acFilename, "c:/a%d.txt", count ) ; which is a much better way to do things the right way.;)

This question has already been answered. Start a new discussion instead.