Thread: unable to process multiple files

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    unable to process multiple files

    Hi all,

    Following is the function which takes the path & the file name to be checked.

    When there is 1 file in the path it works fine but when there are more than one file in the path after the 1st file is processed successfully for the 2nd file the path variable is coming as path/1st file_name2nd file_name whereas it should come as path/1st file_name & path/2nd file_name.

    So in this case i cant process multiple files.

    Dont know where im going wrong, need you people to point out what i am doing wrong here.

    Code:
    int ValidateFile(char *cFnm, char *rawPath)
    {
    
    	FILE* f_read;
    	int c = 0, i = 0;
    	char buf[MAX_LINE_LEN+1];
    	int lenPerline = 0;
    	int numOfComma = 0;
    	int lineNo = 0, numOfErr = 0;
    	
    	strcat(rawPath,cFnm);
    	f_read = fopen(rawPath, "r");
    	if (!f_read) { 
    		perror("Failed opening file for reading:");	
    		LogFile("ERROR : Failed opening file for reading:",rawPath,-1,0);
    		return -1;
    	}
    	
    	LogFile("Successful opening file :",rawPath,-1,0);
    		
    	while (fgets(buf, MAX_LINE_LEN+1, f_read)){
    		if (lineNo == 0){
    			lenPerline = strlen(buf);
    			for (i = 0; i < lenPerline; i++){
    				if (buf[i] == ',') {
    					numOfComma++; 
    				}
    			}
    			if (numOfComma == 8){
    				LogFile("File is Valid!!, proceed to data formatting process....","",-1,0);				
    				lineNo++;				
    			}else{
    				/*Generate error file*/
    				LogFile("ERROR : Number of colunms are not tally in this file....","",-1,0);
    				fclose(f_read);
    				return -1;
    			}
    		} else 
    		{
    			LogFile("Loading data to DB...","",-1,0);
    			if (LoadDataToDb(buf,cFnm) == -1){
    				return -1;
    			}							
    			lineNo++;
    		}
    	}	
    		
    	if (CheckRecord(cFnm) == -1){
    		return -1;
    	}
    	
    	if (FormatFile(cFnm) == -1){
    		return -1;
    	}
    
    	numOfErr = ReportFile(cFnm);
    
    	if (numOfErr > 0){
    		if (ErrorFile(cFnm) == -1){
    			return -1;
    		}
    	}else 
    	{
    		if (numOfErr == -1){
    			return -1;
    		}else {
    			LogFile("No error detected. No file to be generated..","",-1,0);
    		}
    	}
    
    	LogFile("Moving File to done directory..","",-1,0);
    	MoveFileToDir(1, cFnm); 
    	
    	numOfComma = 0;
    	lineNo = 0;	
    	fclose(f_read);
    	return 0;
    }
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > strcat(rawPath,cFnm);
    Well you keep appending paths....

    > fclose(f_read);
    You should probably do this before you move the file.
    or indeed before you return -1 because of errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    I tried what you said but its not working.

    Is there any way to overwrite the last file name in the rawPath variable or clear the last value of the variable rawPath so when it encounters the next file the previous file name will not be there.

    Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Yes, several.
    And you've pretty much described what you need to do about it as well.
    So give it a shot.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Thanks Salem, the problem is resolved now :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  2. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  3. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM