468,512 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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
Sep 6 '06 #1
2 8256
Banfa
9,056 Expert Mod 8TB
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.
Sep 6 '06 #2
[#include<fstream>
#include <iostream>
#include <stdlib>
using namespace std;
void reverse_file(ifstream&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<<"chracter\n"<<argv[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(file);
file.close();
}
return 0;

}
void reverse_file(ifstream&file)

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

}
please find problem with this code......n plz give me a correct code... its urgent
Sep 7 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Nathan Rose | last post: by
4 posts views Thread by Tracey | last post: by
3 posts views Thread by Adriano | last post: by
3 posts views Thread by Evil Bert | last post: by
10 posts views Thread by Fabian Steiner | last post: by
48 posts views Thread by Yahooooooooo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.