So, 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

FILE *file_input, *file_out; 
char input_name[20], output_name[20]; 
char content; 
int off_key; 
file_input = 0; 

while (file_input == 0) 
{ 
printf("Please write the name of the file you want to open and get the input from: \n"); 
gets(input_name); 
file_input = fopen(input_name, "r"); 
} 

printf("\nThis file consists of the following message:\n"); 

while(!feof(file_input)) 
{ 
content = getc(file_input); 
printf("%c",content); 

} 

printf("\n\n Please write the name of the file you want to output the encypted content: \n"); 
gets(output_name); 

file_out = fopen(output_name, "w"); 


do 
{ 
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) / 
{ 
if ((content >= 'A') && (content <= 'Z')) 
content = ((content - 'A') + off_key) % 26 + 'A'; 
else if ((content >= 'a') && (content <= 'z')) 
content = ((content - 'a') + off_key) % 26 + 'a'; 
fprintf(file_out, "%c", content); 
} 


fclose(file_out); 
fclose(file_input); 

printf("Your message has been encrypted and outputed in the specified file.\n\n"); 
return 0; 

Thanks for help

Recommended Answers

All 3 Replies

Try reopening the input file or setting the file position back to the beginning with fseek() on line 22.

Member Avatar for Mouche

Gerard is right. After the loop from lines 16 to 21, you've read the entire file. It stops on EOF.

When you try to read the input file again on line 37, you're just going to get EOF again.

Ok, thank you. I fixed now, and it works fine. cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.