printing "\n" every ten characters and also at the end of a file.
Hi guys! I am working on another assignment and I need your help. I am having trouble printing a \n every ten characters in the output file and also at the end of the output file. The reason you see all of those include files is because that this is a multi-file project.
Code:
#include <stdio.h>
#include <windows.h>
#include "proto.h"
#include <stdlib.h>
#include <malloc.h>
int main(int argc, char *argv[]) //check for command line arguments//
{
FILE *inputFile; //pointer to a file//
char * memBlock; //memory pointer//
int count; //counter variable//
int fileSize = 0; //file size variable//
FILE *outputFile; //output file pointer//
inputFile = fopen(argv[1], "rb"); //open a binary file for reading//
if (inputFile == NULL)
{
printf("Cannot open file for reading.\n"); //display error if file is not found//
return -1;
}
fileSize = getSmallFileLength(argv[1]); //input filename as a parameter//
if (argv[1] == NULL)
{
return -2; //if size is less than 1 quit the program//
}
memBlock = (char*) malloc(fileSize); //memory allocated for file//
if (memBlock == NULL) //if no memory avalible display error message//
{
printf("Error: Not Enough Memory.\n");
}
else
{
fread(memBlock, sizeof(char*), fileSize, inputFile); //read in the entire file//
fclose(inputFile); //done with file//
outputFile = fopen("contents.txt", "w"); //open empty file for wrting//
if (outputFile == NULL)
{
printf("Cannot open file for writing.\n"); //if file not avalible display error//
}
for (count = 0; count < fileSize; count++) //i is less than the fileSize, increment i, and add 1 to i//
{
fprintf(outputFile, "%03d ",*(memBlock+count)); //prints out the ASCII values ome-by-one//
}
if (count % 10)
{
fprintf(outputFile, "\n"); //print a \n every ten characters//
}
free(memBlock); //free allocated memory block//
}
fprintf(outputFile, "\n"); //print a \n at the end of the file//
fclose(outputFile); //done with output file//
return 0;
} // end of main //