plz help...(printing text in reverse order)- c program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srp8982
    New Member
    • Sep 2006
    • 3

    plz help...(printing text in reverse order)- c program

    hello,
    I need help (code) on following program ,its in c...
    1)allow user to write file name in command line, read the file...
    2) count characters, spaces and no. of lines in file given...
    3)read upto first 10 lines, and print them in reverse order

    thanking you in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think this is a homework question. Please have an attempt at it yourself and we will be happy to help you deal with any problems that arise. We will not be doing your homework for you.

    Start with the simple things, opening the file, counting the number of characters, spaces and lines and then go onto the more complex, printing first 10 lines in reverse order.

    You will need to use the functions

    fopen
    fgets
    fclose

    and possibly

    fseek

    as well as array handling of a character array.

    Post your attempt and we'll help you.

    Comment

    • srp8982
      New Member
      • Sep 2006
      • 3

      #3
      [#include<fstrea m>
      #include <iostream>
      #include <stdlib>
      using namespace std;
      void reverse_file(if stream&file);
      static int nofl=0;
      int main (int argc,char *argv[])
      {
      if (argc==1)
      {
      cerr<<"usage:"< <argv[0]<<"filename[s]""\n";
      exit(1);
      }
      ifstream fin;
      ifstream file;
      int count;
      int lines;
      char ch;
      for(int fil=1; fil<argc; fil++)
      {
      fin.open(argv[fil]);
      if(!fin.is_open ())
      {
      cerr<<"can not open the given file"<<argv[fil]<<"\n" ;
      continue;
      }
      count=0;
      lines=0;
      while((fin.get( ch))&&(lines<10 ))
      {
      if(ch=='\n')
      {
      lines++;
      }
      count++;
      }
      cout<<count<<"c hracter\n"<<arg v[fil]<<"\n";
      fin.clear();
      fin.close();
      file.open(argv[fil]);
      if(!fin.is_open ())
      {
      cerr<<"this file can not be opened"<<argv[fil]<<"\n";
      continue;
      }
      reverse_file(fi le);
      file.close();
      }
      return 0;

      }
      void reverse_file(if stream&file)

      {
      int line;
      if((getline(fil e,line))&&(nofl <10))
      {
      nofl++;
      reverse_file(fi le);
      cout<<line<<"\n ";
      }
      return;

      }
      please find problem with this code......n plz give me a correct code... its urgent

      Comment

      Working...