printing "\n" every ten characters and also at the end of a file.

This is a discussion on printing "\n" every ten characters and also at the end of a file. within the C Programming forums, part of the General Programming Boards category; Hi guys! I am working on another assignment and I need your help. I am having trouble printing a \n ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    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 //

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You can put the conditional evaluation right inside the fprintf() statement as in
    Code:
    for (count = 0; count < fileSize; count++) //i is less than the fileSize, increment i, and add 1 to i//
    {
         fprintf(outputFile, (count % 10 ? "%03d " : "\n"), *(memBlock+count)); //prints out the ASCII values ome-by-one//
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    That does not work right. The letter a (ASCII 97) does not print out in the output file.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your posted code seems very close, what problem do you have when you run it?

    Code:
    	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//
    		}
    		
                    //this for loop is indented wrong. It should be one level further to the left.
                    //like so. Also, there is no i variable in this loop.
    	        for (count = 0; count < fileSize; count++) //i is less than the fileSize, increment i, and add 1 to i//
    			{
    				fprintf(outputFile, "&#37;03d ",*(memBlock+count)); //prints out the ASCII values one-by-one//
    			}
                            //you're testing after the 10th char has already been printed. 
                            //That's too late! Test *before* the 10th char has printed, instead.
    			if (count % 10) //change it to if(count % 10 == 0 && count > 0)
    			{
    				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 //
    That will fix it, I believe.
    Last edited by Adak; 12-05-2008 at 10:22 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Sgemin_001 View Post
    That does not work right. The letter a (ASCII 97) does not print out in the output file.
    When count is zero just printout the ASCII value as in
    Code:
    for (count = 0; count < fileSize; count++)
    {
         if (count == 0)
             fprintf(outputFile, "%03d ", *(memBlock+count));
         else
             fprintf(outputFile, (count % 10 ? "%03d " : "\n"), *(memBlock+count));
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Other way around, I believe.

    Code:
    if(count &#37; 10 == 0 && count > 0) 
          fprintf the newline.
    else
          fprintf the ascii value.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    By the way, malloc() and free() are available via <stdlib.h>, not <malloc.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Thanks for the replies. That part works fine now. But one other thing, I my program crashes if you d not have anything as the file name for input, and I don't know how to fix it, Any ideas?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    That's expected behavior since fopen() can't deal with a NULL filename. To avoid crashing check if argv[1] is not NULL else exit the program cleanly.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21