CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2013

    [RESOLVED] Cipher code doesn't outputs content

    I have attached my code below. It worked fine , until I introduced the part to print out what's in the input file. After I did this, it says it's completed, but I doesn't copy the content in the output file.
    What should I change? Thanks a lot

    /*Program to read from a text file, encrypts the content with an offset 
    key selected by the user and outputs the encrypted content in another file (name of input file: in.txt)*/
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h> 
    int main(void) 
    	FILE *file_input, *file_out; //this statement creates a pointer to a stream called file_input for the input and a stream for the output file
    	char input_name[20], output_name[20]; //We are declaring an array called fileName that can contain 20 characters. 
    	char content; //declaring our char variable that will hold a string of text
    	int off_key; //declaring our int variable for the offset key
    	file_input = 0; // we are providing "file_input" a value to start 
    	while  (file_input == 0)
    			printf("Please write the name of the file you want to open and get the input from: \n"); // asks the user for input file name
    			gets(input_name); //gets the file name
    			file_input = fopen(input_name, "r"); //will open the defined file and read from it
    	printf("\nThis file consists of the following message:\n"); 
    	while(!feof(file_input)) //is gonna do the content of this while loop until it reaches end of file
    		fgets(input_name,sizeof(input_name),stdin); //gets text from file
            printf("%c",content);  // prints to screen the text from file
    	printf("\n\n Please write the name of the file you want to output the encypted content: \n");
    	gets(output_name); //asks the user for the output file name
    	file_out = fopen(output_name, "w"); /*Opens the stream for the output text file and writes in it*/
    			printf("Enter key offset (1-25): \n"); //asks the user for the offset key
    			scanf("%d", &off_key); //gets the value of the key
    	while (off_key <1 || off_key > 25);
    	while ((content = getc(file_input)) != EOF) //the loop below will be done until end of file is reached
    			if ((content >= 'A') && (content <= 'Z')) //checking for uppercase 
    			content = ((content - 'A') + off_key) % 26 + 'A'; // because we have 26 English letters, so we will use mod 26 in our cipher 
    			else if ((content >= 'a') && (content <= 'z'))// checking for lowercase 
    			content = ((content - 'a') + off_key) % 26 + 'a'; //same as above
    			fprintf(file_out, "%c", content); // prints the encryted content the output file
    	fclose(file_out); // closes the output file
    	fclose(file_input); //closes the input file
    	printf("Your message has been encrypted and outputed in the specified file.\n\n");
    	return 0; 
    thanks a lot

  2. #2
    Join Date
    Oct 2004
    Rocket City

    Re: Cipher code doesn't outputs content

    This doesn't look like C# code. But anyway, do you need to do a rewind on file_input after the 'part to print out'?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy