[ create a new paste ] login | about

Link: http://codepad.org/l5qPZ3B3    [ raw code | fork ]

kabhwan - C++, pasted on Jul 8:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INPUT_FILE_NAME		"input.txt"
#define OUTPUT_FILE_NAME	"output.txt"

void sort_string(char ** strResult, int count);

int get_line_cnt(char * strContent);
int split_contents_to_array(char * strContent, char *** ptrStrArray);

char * load_from_file(char * strFilePath);
int write_result_to_file(char * strFilePath, char ** strResult, int count);

// string sort
void sort_string(char ** strResult, int count)
{
	int i, j;
	char * temp;

	for(i = 0 ; i < count - 1 ; i++) {
		for(j = i+1 ; j < count ; j++) {
			if(strcmp(strResult[i], strResult[j]) > 0)
			{
				temp = strResult[i];
				strResult[i] = strResult[j];
				strResult[j] = temp;
			}
		}
	}
}

// split all string lines to array
int split_contents_to_array(char * strContent, char *** ptrStrArray)
{
	int line_cnt;
	int str_len;
	int curr_line_idx = 0;
	char * p = strContent, * q = strContent;

	line_cnt = get_line_cnt(strContent);
	if(line_cnt < 0)
		return -1;

	*ptrStrArray = new char *[line_cnt];

	while(1) {
		p = strchr(p, '\n');

		if(!p)
		{
			str_len = strlen(q);
			*((*ptrStrArray) + curr_line_idx) = new char[str_len + 1];

			strcpy(*((*ptrStrArray) + curr_line_idx), q);
			curr_line_idx++;
			break;
		}

		*p = '\0';
		p++;

		str_len = strlen(q);
		*((*ptrStrArray) + curr_line_idx) = new char[str_len + 1];

		strcpy(*((*ptrStrArray) + curr_line_idx), q);
		curr_line_idx++;

		q = p;		
	}

	return line_cnt;
}

// get line count
int get_line_cnt(char * strContent)
{
	int line_cnt = 1;

	if(!strContent)
		return -1;

	while(*strContent) {		// NOT NULL
		if(*strContent == '\n')
			line_cnt++;

		strContent++;
	}

	return line_cnt;
}


char * load_from_file(char * strFilePath)
{
	char * pRet = NULL;
	long file_length = 0, i;
	int ch;

	FILE * fp = fopen(strFilePath, "r");

	if(!fp)
		return NULL;

	//fstat()

	if( fseek(fp, 0, SEEK_END) )
		return NULL;

	file_length = ftell(fp);

	fseek(fp, 0, SEEK_SET);
	
	pRet = new char[file_length + 1];

	for (i = 0 ; i < file_length ; i++)
	{
		ch = fgetc(fp);
		if(ch == -1)		// EOF?
			break;

		pRet[i] = (char)ch;
	}

	pRet[i] = '\0';

	fclose(fp);

	return pRet;
}

int write_result_to_file(char * strFilePath, char ** strResult, int count)
{
	int i;
	FILE * fp = fopen(strFilePath, "w");

	if(!fp)
		return 0;

	for(i = 0 ; i < count ; i++)
		fprintf(fp, "%s\n", strResult[i]);

	fclose(fp);

	return 1;
}

int main(void)
{
	char * strInputContent;
	char ** strResult;
	int line_cnt;

	strInputContent = load_from_file(INPUT_FILE_NAME);

	if(!strInputContent)
	{
		delete [] strInputContent;

		printf("Error Reading!\n");
		return -1;
	}

	line_cnt = split_contents_to_array(strInputContent, &strResult);

	if(line_cnt < 0)
	{
		delete [] strInputContent;

		printf("Error Splitting Line!\n");
		return -2;
	}

	sort_string(strResult, line_cnt);

	if(!write_result_to_file(OUTPUT_FILE_NAME, strResult, line_cnt))
	{
		delete [] strInputContent;
		delete [] strResult;

		printf("Error Writing Result to File!\n");
		return -3;
	}

	delete [] strInputContent;
	delete [] strResult;

	return 0;
}

// woong.lee@lge.com


Create a new paste based on this one


Comments: