int main(int argc, char *argv[]){ int returnError = 0; FILE *IfilePointer = NULL; int fileSize = 0; int wrapLine = 40; unsigned char* pAllocatedMem = NULL; int argumentCounter = 1; int charCounter = 0; int linePosition = 0; char garbage[10] = ""; char optionInupt[7] = ""; int counter = 0; if (argv[1][0] == '-'){//first argument is a option //get argument value if (argv[1][2] == ' '){//space between -w and number if (sscanf(argv[1], "%s %d %s", garbage, &wrapLine, garbage) < 2){ returnError = 2; printf("Usage: wrapfile [-w width] file ..."); } if (strcmp(garbage, "-w") != 0){ printf("Usage: wrapfile [-w width] file ..."); returnError = 2; } } else{//no space between -w and number while (argv[1][counter+2] != '\0'){ if (isdigit(argv[1][counter + 2])){ optionInupt[counter] = argv[1][counter + 2]; counter++; } else{ returnError = 2; printf("Usage: wrapfile [-w width] file ..."); break; } } wrapLine = atoi(optionInupt); } if (wrapLine > 65535 || wrapLine < 0){ returnError = 2; } argumentCounter = 2; } while (argc > argumentCounter && returnError == 0){ //open file / check files existance if ((IfilePointer = fopen(argv[argumentCounter], "rb")) == NULL){ printf("File not openable"); returnError = 1; break; } //get file size else{ //find size of input file fileSize = getSmallFileLength(argv[argumentCounter]);//works for file size up to 2,147,483,647 bytes in size if (fileSize <= 0){ returnError = 4; } else{ //allocate memory for input file pAllocatedMem = malloc(fileSize); //read entire input file into memory block if (fread(pAllocatedMem, 1, fileSize, IfilePointer) != fileSize){//read in data printf("Could not allocate memory"); returnError = 4; } else{ //do all wraping here charCounter = 0; linePosition = 0; while (charCounter < fileSize){//while there are still characters printf("%c", *(pAllocatedMem + charCounter));//print the next character linePosition++; charCounter++; if (*(pAllocatedMem + charCounter) == '\n'){//newline in text linePosition = 0; } else if (linePosition == wrapLine){//if reached line wrap printf("\n"); linePosition = 0; } } if (*(pAllocatedMem + charCounter) != '\n' && wrapLine != 10){//if not a new line at EOF, add one printf("\n"); } } //free allocated memory free(pAllocatedMem); } //close input file fclose(IfilePointer); //move to next file in arguments argumentCounter++; } } return returnError; }
int main(int argc, char *argv[]) { //file pointers FILE *pFile1 = NULL; FILE *pFile2 = NULL; //variables unsigned char * buffer = NULL; int howManyRead = 0; int counter = 0; long lSize = 0; //take in argmuments not more then 2 // arv[0] and arv[1] if( argc != 2 ) { printf("ERROR No Input File has been found\n"); return 0; } else { //opens the file received from argc onto argv pFile1 = fopen(argv[1], "rb"); //if the file does not exists if( pFile1 == NULL ) { //printf error printf("Can't open file\n"); } // if the size of the file is less then kFalse quit the program //file has to be smaller then 2,147,483,647 bytes else if((lSize = getSmallFileLength(argv[1])) <= kFalse) { //quiting the program return 0; } else { // assign a block of memory for the file buffer = (char*) malloc((sizeof(char)*lSize)+1); pFile2 = fopen("context.txt", "w"); if( pFile2 == NULL ) { //error print printf("Can't open output file\n"); if( fclose(pFile1) != kFalse ) { printf("Error closing input file\n"); } } else { howManyRead = fread(buffer, sizeof (char), lSize, pFile1); if(howManyRead != lSize) { printf("cant read the file"); } for(counter=1; counter < lSize ; counter++) { fprintf(pFile2,"%.3u%c",buffer[counter],space); if((counter % 10) == kFalse ) { fprintf(pFile2,"%c%c",carriageReturn,newLine); } if(!feof(pFile2)) { //put a new line fprintf(pFile2,"%c",newLine); } } } if( fclose (pFile1) != kFalse ) { printf("Error closing input file\n"); }//ckeck if the file closed properly if( fclose (pFile2) != kFalse ) { printf("Error closing output file\n"); } ///free the memory free(buffer); } } return 0; }