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");
		exit(1);
		}
		else{
			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");
	fclose(input_file);
		
	input1_file = fopen(file_name, "r");
	if(input1_file == NULL){
		printf("Error file could not be read");
		exit(1);
		}
		else{
			while(1){
				c = fgetc(input1_file);
				if( c == EOF){ /*if it reaches the end of the file, break the loop */
					break;
					}
				else if (counter2 < 200){
						c = file_text[counter2]; /*assign the character to elements of the array */
						counter2++;
					}
				}
			}
	
	fclose(input1_file);
	output_file = fopen("output.txt", "w");
	
	if(output_file == NULL){
		printf("Could not write file");
		exit(1);
		}
		else{	
			counter1--; /*it incremented one too many */
			for(;file_text[counter1];counter1--){
				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");
	fclose(output_file);
	
			
	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.

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.