read from a file and display on monitor in reverse order

This is a discussion on read from a file and display on monitor in reverse order within the C++ Programming forums, part of the General Programming Boards category; works fine without the for loop.... if i use for loop...it doesnt give the output... where am i going wrong? ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    6

    read from a file and display on monitor in reverse order

    works fine without the for loop.... if i use for loop...it doesnt give the output... where am i going wrong?

    Code:
    #include<iostream>
    #include<conio.h>
    #include<fstream>
    using namespace std;
    main()
    {
    	char c;
    	int i;
    	ifstream in;
    	in.open("efg.txt");
    	int p=in.tellg();
    	for(i=1;i<p;i++)
    	{
    	in.seekg(-i,ios::end);
    		in>>c;
    		cout<<c;
    	}
    		in.close();
    		getch();
    		return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    If by "works fine" you mean that it does nothing observable, then I suppose it works fine without the for loop. Your code has the same fundamental problem, with or without the loop, the only difference is whether there is a visible symptom.

    You need to open the input file in binary mode. The streaming operators (<< and >>) are also formatted I/O (i.e. not for binary mode).

    If the file isn't too large, it would be easier to read the entire file contents into memory, reverse them, and then output.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    solved!

    Code:
    in.seekg(0,ios::end);
    just had to add this line!!!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    You're just getting lucky. You're on a system where binary and text mode files happen to be equivalent.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Posting data from file1 into new file in reverse order
    By DOLZero in forum C Programming
    Replies: 5
    Last Post: 09-24-2013, 06:22 PM
  2. Replies: 1
    Last Post: 05-30-2010, 11:22 PM
  3. Read and display contents from text file
    By spadez in forum C Programming
    Replies: 2
    Last Post: 02-03-2009, 03:25 PM
  4. I don't get a display on my monitor...
    By deltabird in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2003, 05:28 PM
  5. read into array and spit out in reverse order
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-07-2001, 03:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21