Hi,
Im trying 2 write the contents of a input file to an output file in reverse order using file I/O in C. Is there a more efficient way 2 do it?

int main()
{
	FILE *fp1,*fp2;
	char ch,arr[100];
	int i=0;
	
	if( (fp1 = fopen("input.txt","r")) == NULL )
	{
		printf("Not able to open file");
		exit(1);
	}

	while((ch = fgetc( fp1 )) != EOF)
	{
		arr[i]=ch;
		i++;
	}
	
	fclose(fp1);

	fp2 = fopen("output.txt","w");
	while( i>=0 )
	{
		fputc(arr[i], fp2);
		i--;
	}

	fclose(fp2);

	return 0;
}

Thanks.

Edited 6 Years Ago by adrawat: n/a

> Is there a more efficient way 2 do it?
Well there are no simple ways of doing it more efficiently.

Also, if your input file contains no characters, how many characters are you writing to the output file?

hey. thnx 4 d reply.
the output file will have no characters if the input file has no characters.
Im concerned because if the input file has more than 100 characters, then whole of the input wont b written to the output.

> the output file will have no characters if the input file has no characters.
i = 0;

After the first loop of NO characters, i is still 0.

Your 2nd loop begins with i >= 0, it executes ONCE
You've just written a garbage character to the output file.

> Im concerned because if the input file has more than 100 characters
Indeed, reversing a very long file would indeed be a problem.
Are you allowed to assume the file will fit in memory?

Instead of reading character by character use "fread" to read more than 1 character from file at a time. And then use "fwrite" to write them to the output file.

You can also use "fgets" to read a file line by line. Because "fgets" terminates reading on "\n" (new line escape sequence).

Thanks 4 that garbage char prob :)
The question doesnt specify if the input fits in the memory.

Instead of reading character by character use "fread" to read more than 1 character from file at a time. And then use "fwrite" to write them to the output file.

You can also use "fgets" to read a file line by line. Because "fgets" terminates reading on "\n" (new line escape sequence).

I guess fread returns a pointer to the whole string rather than the first char. Is there some function that i can use in conjunction wit fread so that i can reverse the whole input?

Instead of reading character by character use "fread" to read more than 1 character from file at a time. And then use "fwrite" to write them to the output file.

You can also use "fgets" to read a file line by line. Because "fgets" terminates reading on "\n" (new line escape sequence).

And how does this help the OP write the "output file in reverse order" better than what he's done?

Please think before you answer questions. This is the second bad info I've seen you give in 2 posts.

Waltp i think every person has a common sense kind of thing. As you can see that he has appriciated my answer by saying "Thanks 4 that garbage char prob". I just try to solve his problem of reading char by char. It is easy to reverse a buffer as compared to a file.
It is a common sense that if you have a string in a buffer then there are hundereds of methods for reversing it.

I dont know why moderators always insult our answers and respond too harshly. Why not only modarators answer to every thread . I know that You have more knowledge than i. I respect your answers and your knowledge.

I replied to his question according to my knowledge. I know that i can b wrong. But if i m wrong then please point out my mistakes. But please dont insult me.

I always respect the person who point out my mistakes.

As you can see that he has appriciated my answer by saying "Thanks 4 that garbage char prob".

I thought his reply was to Salem.

> the output file will have no characters if the input file has no characters.
i = 0;

After the first loop of NO characters, i is still 0.

Your 2nd loop begins with i >= 0, it executes ONCE
You've just written a garbage character to the output file.

Thanks 4 that garbage char prob :)

I am not attempting to insult you.

Waltp i think every person has a common sense kind of thing. As you can see that he has appriciated my answer by saying "Thanks 4 that garbage char prob". I just try to solve his problem of reading char by char. It is easy to reverse a buffer as compared to a file.

As Dave points out, and using your terms, common sense dictates that if someone says "Thanks 4 that garbage char prob" and you never mentioned garbage char prob, he wasn't thanking you. Maybe?

I also stated "This is the second bad info I've seen you give in 2 posts." Anyone that seems to habitually give bad information should "think before you answer questions." This is also "common sense".

Please notice that your reputation indicator is not green. This is indicative of something. Maybe "common sense" is not that common.

ok then. if you have common sense then you should also see that i have solved 5 threads in just 30 posts. i never care about my reputation. i always tried to solve others problem.
As i already told you sir i respect your knowledge. But do not insult me. Because you do not have rights to insult me. May be you block my account. But i dont care for that because i m here to solve problems and gain knowledge. And i have done nothing wrong. So think before "YOU" speak. dont try to insult.

Comments
In one, you repeated what I'd already said. Same for with someone else in another thread. "Me toos" aren't really "solves". Gee, if I post to solved threads my number will go up too. :rolleyes:

hey guys...i didnt think my first problem would become such a big PROBLEM...let d tempers cool :)

thnx 4 everyones' help...!

This question has already been answered. Start a new discussion instead.