|
I need to read mulptiple files like test1,test2,...test50. how can i do that in c++?
thanks
|
|
|
I wrote this to process a sequence of bitmaps used in an animation
sequence. It begs to be put into a class but should be a good
starting point for you and is a working solution as it is.
The filenames must be in the following convention:
Filename + number with constant number of digits + optional extension
With a size from 0 to 4 characters beginning with character ‘.’.
e.g somefile0004.cu to somefile0401.cu will work.
Single files with no numbers are also accepted.
This is the format I have run into. Other formats will need recoding.
Written in c++ Visual Studio 2005, Win32 console application.
/////////////////////////////////////////////////////////////
//// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>
/////////////////////////////////////////////////////////////
// OpenMultiFiles.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h> //for getch.
//A struct to hold information about the file sequence.
typedef struct MultiFilename_s
{
char first_filepath[260]; //The complete name of the file and number and path.
char extension[6]; //e.g ".bmp"
long num_digits; //The number of digits in the number string
char fp_without_numbers[260]; //The file path without numbers and extensions.
long first_num,last_num;
long num_files_in_sequence; //e.g 0000 to 0005 would be 6 files.
char first_number_str[12];
char cur_number_str[12]; // a string representing the number, e.g "0021"
char cur_filename[260];
}MultiFilename_t;
bool mfnProcessFile(char * filename);
bool mfnDoesFileExist(char * fp);
void mfnSeekFirstFile(MultiFilename_s * mfn);
void mfnSeekNextFile(MultiFilename_s * mfn);
bool mfnGetFileInfo(char * fp,MultiFilename_t *mfn);
void PrintFileInfo(MultiFilename_t *mfn);
//........................................
int _tmain(int argc, _TCHAR* argv[])
{
char file_path[160];
MultiFilename_t mfn;
bool ret;
/* Make a name of a file that we want to test program on.
* Normally this will be input from somewhere else like cin or command line argument.
* For now I will just hard code a filename and store it in 'file_path'.
*/
memset(file_path,0,160);
strcpy_s(file_path,"/forum/Ctempsome_file001.txt");
// First get info about the file. Is it a sequence, number of files
// in seq, ect.
ret = mfnGetFileInfo(file_path,&mfn);
// If no files in sequence, we don't have a file.
if(!mfn.num_files_in_sequence) return 0;
// Print the file info for purposes of this example.
PrintFileInfo(&mfn);
//process the files
long j,num_files_processed;
num_files_processed = 0;
mfnSeekFirstFile(&mfn);
for(j = mfn.first_num; j <= mfn.last_num; j++)
{
ret = mfnProcessFile(mfn.cur_filename);
if(!ret) break; //user cancel or error.
mfnSeekNextFile(&mfn);
num_files_processed++;
}
printf("Done.\r\n");
printf("press any key to end...");
_getch();
return 0;
}
void PrintFileInfo(MultiFilename_t *mfn)
{
printf("\r\nPrintFileInfo....\r\n");
printf("Filename of first file selected is <%s>\r\n",mfn->first_filepath);
printf("Extension is <%s>\r\n",mfn->extension);
printf("Filename without numbers or extension is <%s>\r\n",mfn->fp_without_numbers);
printf("There are %d files in this sequence.\r\n",mfn->num_files_in_sequence);
printf("Sequence numbers from %d to %d\r\n",mfn->first_num,mfn->last_num);
printf("There are %d digits in the sequence number.\r\n",mfn->num_digits);
printf("The first number string is %s\r\n\r\n",mfn->first_number_str);
}
//
// this function is called for each file in the sequence.
// If processed ok, should return true, else false.
//
bool mfnProcessFile(char * filename)
{
printf("processing...<%s>...ok\r\n",filename);
/* Add code here to open and process file, then close.
* If user abort or other error, return false.
*/
return true;
}
//
bool mfnDoesFileExist(char * fp)
{
FILE *stream;
errno_t err;
if((err = fopen_s(&stream,fp, "r" )) !=0 ) return false;
fclose(stream);
return true;
}
//
void mfnSeekFirstFile(MultiFilename_s * mfn)
{
memcpy(mfn->cur_number_str,mfn->first_number_str,12);
memset(mfn->cur_filename,0,260);
sprintf_s(mfn->cur_filename,"%s%s%s",mfn->fp_without_numbers,mfn->cur_number_str,mfn->extension);
}
//
void mfnSeekNextFile(MultiFilename_s * mfn)//also updates cur_filename
{
long val;
char temp[12];
val = atol(mfn->cur_number_str);
val++;
memset(temp,0,12);
_ltoa_s(val,temp,10);
long len = (long)strlen(temp);
long offset,i;
offset = mfn->num_digits - len;
memset(mfn->cur_number_str,0,12);
for(i=0; i < offset; i++)
{
mfn->cur_number_str = '0';
}
memcpy(&mfn->cur_number_str[offset],temp,len);
memset(mfn->cur_filename,0,260);
sprintf_s(mfn->cur_filename,"%s%s%s",mfn->fp_without_numbers,mfn->cur_number_str,mfn->extension);
}
// Returns the extension in to the array... extension[5]
// The extension may have a length from 0 to 4 characters with the
// first character being '.'
void GetFileExtension(char * fn,char extension[5])
{
long k,len,start;
memset(extension,0,5);
len = (long)strlen(fn);
//find start
k = 0;
for(start = len; start > (len-4); start--,k++)
{if (fn[start] == '.') break;}
if(k > 4) return; //extension not found.
memcpy(extension,&fn[start],k);
}
//
bool mfnGetFileInfo(char * fp,MultiFilename_t *mfn)
{
long len;
char extension[5]; // .nnn
//prepare struct.
memset(mfn,0,sizeof(MultiFilename_t));
strcpy_s(mfn->first_filepath,fp);//store the name of the first file here
//See if file exists. If not, no point in proceeding.
if(!mfnDoesFileExist(fp))
{
printf("Cannot find file <%s>\r\n",fp);
mfn->num_files_in_sequence = 0;
return false;
}
//get extension, format = ".nnn"
GetFileExtension(mfn->first_filepath,mfn->extension);
//remove extension and store in fp_without_numbers, then remove number
memcpy(mfn->fp_without_numbers,mfn->first_filepath,strlen(mfn->first_filepath)-strlen(mfn->extension));
long starti,i;
//extract and remove number from name
starti = (long)strlen(mfn->fp_without_numbers) - 1;
char c;
long num_digits = 0;
char temp[12];
i = 0;
memset(temp,0,12);
for(starti; starti > 0; starti--)
{
c = mfn->fp_without_numbers[starti];
if(c < '0' || c > '9') break;
temp = c;
i++;
mfn->fp_without_numbers[starti] = 0;//remove digits from filename
}
if(!i)
{
memset(mfn->cur_filename,0,260);
memcpy(mfn->cur_filename,fp,strlen(fp));
return false;
}
//reverse order
mfn->num_digits = i;
starti = i - 1;
i = 0;
for(starti; starti >= 0; starti--,i++)
{
mfn->cur_number_str = temp[starti];
}
mfn->first_num = atol(mfn->cur_number_str);
memcpy(mfn->first_number_str,mfn->cur_number_str,12); //for use later when pasting
//Find how many files there are.
long numFiles = 0;
bool do_again;
char cfp[260];
do_again = true;
memset(cfp,0,260);
sprintf_s(cfp,"%s%s%s",mfn->fp_without_numbers,mfn->first_number_str,mfn->extension);
mfnSeekFirstFile(mfn);
while(do_again)
{
if(mfnDoesFileExist(mfn->cur_filename))
{mfnSeekNextFile(mfn);
}else
{do_again = false;}
}
mfn->last_num = atol(mfn->cur_number_str)-1;
mfn->num_files_in_sequence = mfn->last_num - mfn->first_num + 1;
return true;
}
///////////////////////////////////////////////////////
|
|
|
|
|
|
|
// |