Hello everybody, ive only been learning C for about a month so please forgive me if my code is messy or unclear! I have written a simple program which creates a text file, reads from it and then writes the output backwards to another file; it seems to work apart from in the output file i get a load of gibberish after the original reversed text. the code is listed bellow.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
	char file_name[100];			/*declare char arrays to hold file name and text*/
	char file_text[500];
	char c; /*char to hold text characters */
	int counter1, counter2; 
	FILE *input_file, *input1_file, *output_file; /*declaration of file pointers */
	printf("\nenter the name & extension of text file to create:");
	scanf("%s", &file_name);
	printf("\nenter the text to be reversed:");
	scanf("%s", &file_text);
	input_file = fopen(file_name, "w");
	if(input_file == NULL){ /*if it is a null pointer return error to operating system*/
		printf("Error: could not create file");
			for(counter1 = 0;file_text[counter1];counter1++){ /*eneter each character from the array into the file*/
				fputc(file_text[counter1], input_file);
	printf("\nFile sucessfully read.\n");
	input1_file = fopen(file_name, "r");
	if(input1_file == NULL){
		printf("Error file could not be read");
				c = fgetc(input1_file);
				if( c == EOF){ /*if it reaches the end of the file, break the loop */
				else if (counter2 < 200){
						c = file_text[counter2]; /*assign the character to elements of the array */
	output_file = fopen("output.txt", "w");
	if(output_file == NULL){
		printf("Could not write file");
			counter1--; /*it incremented one too many */
				if(counter1 != 0){
				fputc(file_text[counter1], output_file); /*output each character to the file individually starting with the last element in the array*/
	printf("\n\nprocess completed");
	return 0;

Any help in resolving my little problem would be much appreciated, thanks in advance.

> scanf("%s", &file_name);
You don't need an & here

> input_file = fopen(file_name, "w");
An input file with a mode of "w" - surely not...

Why do i not need an '&' ive always thought you needed that? and regardless of the file pointer name it stil gives me a partly garabge output :S

Why do i not need an '&' ive always thought you needed that? and regardless of the file pointer name it stil gives me a partly garabge output :S

file_name is a pointer already to the first character of the array.
You must omit the & or do it as &file_name[0]

The garbage output probably happens because the string is not terminated with a '\0' at some point.

Thanks, managed to fix it

And to clarify the '&' issue, if it wasn't already, the scanf function needs to know what the address of something is, and the '&' is the address of operator. Since the pointer already contains the address, it doesn't make sense to use '&' in that case.