Exemple #1
0
int main (int argc, char *argv[])
{
	/* ifp: input file pointer*/
	FILE *ifp;
	/* ofp: output file pointer*/
	FILE *ofp;
	/*number: an integer number to be translated*/
	int number;
	/*output: will hold a pointer of the translated string output*/
	char *output;
	/*the number of command line argument received*/
	int num_of_arguments = argc-1;



	/*this switch case will start the relevant sub program based on the number of argument we received*/
	switch (num_of_arguments) {
		/*2 arguments: the first one will be an input file
		the second one will be the output file*/
		case 2 :
			/*if we were not able to open the input file in read mode*/
			if ((ifp=fopen(argv[1], "r")) == NULL)
				fprintf(stderr ,"Cannot Open file %s\n", argv[1]);
				/*if we were not able to open the output file in write mode*/
			else if ((ofp=fopen(argv[2], "w"))== NULL)
				fprintf(stderr, "Cannot Open file %s\n", argv[2]);
				/*we succeeded to open both files*/
			else
			{
				/*scan numbers from input file to number until reaching EOF*/
				while (fscanf(ifp, "%d", &number) != EOF)
				{
					/*assign the output of translate_number to output*/
					output = translate_number (number);
					/*print the output which contains the translated number to the output file*/
					fprintf(ofp, "%s\n", output);
				}
			}
			/*after we done, close both files*/
			fclose(ifp);
			fclose(ofp);
			break;

			/*we received only one file, this will be the input file, the output will
            be redirected to the standard output*/
		case 1 :
			/*Similar to "case 2", instead writing the output to an output file
			we printing it to the standard output*/
			if ((ifp=fopen(argv[1], "r")) == NULL)
				fprintf(stderr, "Cannot Open file %s\n", argv[1]);
			else
			{
				/*scan numbers from input file to number until reaching EOF*/
				while (fscanf(ifp, "%d", &number) != EOF)
				{
					/*assign the output of translate_number to output*/
					output = translate_number (number);
					/*print the output to the standard output*/
					printf("%s\n", output);
				}
			}
			/*close file*/
			fclose(ifp);
			break;

			/*we didn't received output and input files, the input number will be taken
            directly from the standard input and the output will be redirected to the
            standard output*/
		case 0 : printf("\nUsage:  Hitting Enter will show the result and wait for another input.\n\tCtrl-D will terminate the program.\n\tEntering Characters is not supported!\n\nEnter numbers to be translated:\n");


			/*scan numbers into c while no End-Of-File received*/
			while (scanf("%d", &number) != EOF)
			{
				/*if translate return a 0 value, this means that the entered number is >= 100 and number is < 0*/
				if (*translate_number(number) == '0')
					/*redirect the error message to the system's standard error*/
					fprintf(stderr, "Not Supported Number\n" );
				else
					/*translate didn't retrun '0', then print the string*/
					printf("%s\n", translate_number(number));
			}
			break;

			/*we received more that 2 arguments, print the usage*/
		default : fprintf(stderr, "Not Supported\nUsage:  ./numbers <input file> <output file>\nOr\t./numbers <input file>\nOr\t./numbers\n");
			break;
	}
	return OK;
}
Exemple #2
0
int main (int argc, char *argv[])
{
	/* ifp: input file pointer*/
	FILE *ifp;
	/* ofp: output file pointer*/
	FILE *ofp;
	/*number: an integer number to be translated*/
	int number;
	/*output: will hold a pointer of the translated string output*/
	char *output;
	/*c: a character to be entered - meant to recieve EOF*/
	char c;
	/*line: i a line in the input file (ifp)*/
	char line[MAX_LINE];
	/*the number of command line argument recieved*/
	int num_of_arguments = argc-1;


	/*this switch case will start the relevant sub program based on the number of argument we recienved*/
	switch (num_of_arguments) {
		/*2 arguments: the first one will be an input file
		the second one will be the output file*/
		case 2 : 
			/*if we were not able to open the input file in read mode*/
			if ((ifp=fopen(argv[1], "r")) == NULL)
				fprintf(stderr ,"Cannot Open file %s\n", argv[1]);
			/*if we were not able to open the output file in write mode*/
			else if ((ofp=fopen(argv[2], "w"))== NULL)
				fprintf(stderr, "Cannot Open file %s\n", argv[2]);
			/*we succeded to open both files*/
			else 
			{
				/*while the input file still has line*/
				while (fgets(line, MAX_LINE, ifp)!= NULL)
				{
				/*replance the enter \n with \0 as string termination*/
				*(strchr(line, '\n'))='\0';
					/*convert the line to integer and assigne to number*/
					number = atoi (line);
					/*translate the number to a string and assigne to output*/
					output = translate_number (atoi(line));
					/*write output to the ouptu file*/
					fprintf(ofp, "%s\n", output);
				}
			}
			/*after we done, close both files*/
			fclose(ifp);
			fclose(ofp);
		break;

		/*we recieved only one file, this will be the input file, the output will
		be redirected to the standart output*/
		case 1 : 
			/*Similiar to "case 2", instead writing the output to an output file
			we printing it to the standart output*/
			if ((ifp=fopen(argv[1], "r")) == NULL)
				fprintf(stderr, "Cannot Open file %s\n", argv[1]);
			else 
			{
				while (fgets(line, MAX_LINE, ifp)!= NULL)
				{
					*(strchr(line, '\n'))='\0';
					number = atoi (line);
					output = translate_number (atoi(line));
					printf("%s\n", output);
				}
			}
			fclose(ifp);
		break;

		/*we didnt recieve output and input files, the input number will be taken
		directly from the standart input and the output will be redirected to the 
		standart output*/
		case 0 : printf("Enter a number to be translated:\n");
			do 
			{	
				/*scan the number with scanf and assign the value to number variable*/
				scanf ("%i", &number);
				/*if translate return a 0 value, this means that the entered number is >= 100 and number is < 0*/
				if (*translate_number(number) == '0')
					/*redirect the error message to the system's standart error*/
					fprintf(stderr, "Not Supported Number\n" );
				else
					/*translate didn't retrun '0', then print the string*/
					printf("%s\n", translate_number(number));
				/*loop while we didn't recieve EOF*/
			} while ((c = getchar()) != EOF);
		break;

		/*we rcieved more that 2 arguments, print the usage*/
		default : fprintf(stderr, "Not Supported\nUsage:  ./numbers <input file> <output file>\nOr\t./numbers <input file>\nOr\t./numbers\n");
		break;
	}
return OK;	
}