int ArchivoRegistrosLongitudFija :: open (std::string dir, std::string fileName, std::string mode)
{
	// Abro el archivo
	file = fopen((dir+fileName).c_str(),mode.c_str());
	if (file == NULL) {
		std::cout << "File doesn't exist." << std::endl;
		return RES_ERROR;
	}
	
	// Leo el file header para obtener info del archivo
	struct header_arlf head;
	if (getFileHeader(head) == RES_ERROR)
		return RES_ERROR;
	
	recordSize = head.h_recordSize;
	maxRecs = head.h_maxRecords;
		
	return RES_OK;
}
示例#2
0
int main (int argc, char** argv)
{
	// for storing results of argument processing
	// general
	bool displayInfo = true;
	char* inFileName = "";

	// threshold filter
	bool runThreshold = false;
	float threshold = THRESH_ERROR;

	// crop
	bool runCrop = false;
	int cropWidth = CROP_DIM_ERROR;
	int cropHeight = CROP_DIM_ERROR;
	int cropStart[] = {0,0};

	// resize
	bool runResize = false;
	float scaling = SCALING_ERROR;
	char* resType = "";

	// default output file name
	char* outFileName = "out.bmp";
	
	// process arguments, checking for errors
	if (processArguments(argc, argv, &runThreshold, &runCrop, &runResize, 
		&displayInfo, &inFileName, &outFileName, &threshold, &cropWidth, 
		&cropHeight, cropStart, &scaling, &resType) != EXIT_SUCCESS)
	{
		fprintf(stderr, "Unable to process arguments.\n");
		return EXIT_FAILURE;
	}

	// open the file
	FILE *inFile = fopen(inFileName, "r");

	// check that file opened okay
	if (inFile == NULL)
	{
		fprintf(stderr, "Unable to open file: %s\n", inFileName);
		return EXIT_FAILURE;
	}

	// read in the file header and check that it went okay
	struct bmpFileHeader *fileHead = getFileHeader(inFile);

	if (fileHead == NULL)
	{
		fprintf(stderr, "Unable to set up file header.\n");
		return EXIT_FAILURE;
	}

	struct bmpInfoHeader *infoHead = getInfoHeader(inFile);

	if (infoHead == NULL)
	{
		fprintf(stderr, "Unable to set up info header.\n");
		return EXIT_FAILURE;
	}

	// check the format of the file
	if (formatCheck(fileHead, infoHead) != EXIT_SUCCESS)
	{
		fprintf(stderr, "Program cannot handle this format.\n");
		return EXIT_FAILURE;
	}


	// if we're displaying info, do so
	if (displayInfo)
	{
		// print out header information
		printInfo(fileHead, infoHead);
	}

	// move file pointer on to start of image, if necessary
	if (fileHead->offset != (sizeof(struct bmpFileHeader) + 
		sizeof(struct bmpInfoHeader)))
	{
		fseek(inFile, 
			((int)fileHead->offset - (sizeof(struct bmpFileHeader) + 
				sizeof(struct bmpInfoHeader))), SEEK_CUR);
	}

	// if we're running a threshold filter, do so
	if (runThreshold)
	{
		// apply threshold filter to image, checking for errors
		if (applyThreshold(inFile, fileHead, infoHead, outFileName, threshold) 
			== EXIT_FAILURE)
		{
			fprintf(stderr, "Unable to apply threshold.\n");
			return EXIT_FAILURE;
		}
	}

	// if we're cropping the image, do so
	if (runCrop)
	{
		// crop the image, checking for errors
		if (cropImage(inFile, fileHead, infoHead, outFileName, cropWidth,
			cropHeight, cropStart) == EXIT_FAILURE)
		{
			fprintf(stderr, "Unable to crop image.\n");
			return EXIT_FAILURE;
		}
	}

	// if we're resizing the image do so
	if (runResize)
	{
		// resize image checking for errors
		if (resizeImage(inFile, fileHead, infoHead, outFileName, scaling, resType) 
			== EXIT_FAILURE)
		{
			fprintf(stderr, "Unable to resize image.\n");
			return EXIT_FAILURE;
		}
	}

	// free up allocated memory
	free(infoHead);
	free(fileHead);

	// close input file, checking for errors
	if (fclose(inFile) != 0)
	{
		fprintf(stderr, "Unable to close input file.\n");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}