For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2004 > Revese the output of a file.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Revese the output of a file.
Rodrick Brown

2004-10-15, 3:56 am

Hello everyone just looking for feedback on the following block of code
on issues or concerns to watch out for, I'm new to C but not
programming so please be as detailed as possible on do's and do not's
thanks :)


#include <stdlib.h>
#include <string.h>

int
main(int argc,char **argv)
{
FILE *fp;
char linebuff[512];
char *buff[100];
int line_cnt,n = 0;

if( argc != 2)
{
fprintf(stdout,"usage: %s [filename]\n",argv[0]);
exit(EXIT_FAILURE);
}

if ( (fp = fopen(argv[1],"r")) == NULL)
{
perror("Fatal: fopen() unable to open file");
exit(EXIT_FAILURE);
}


while(fgets(linebuff,sizeof(linebuff),fp
))
{
buff[line_cnt] = malloc(strlen(linebuff)+1);
strncpy(buff[line_cnt],linebuff,strlen(l
inebuff));
line_cnt++;
}

for(n = 0; n < line_cnt; line_cnt--)
{
fprintf(stdout,"%s",buff[line_cnt]);
}

return EXIT_SUCCESS;
}

--
Unix Systems Engineer
The City of New York
Dept. of Information Technology
http://www.nyc.gov/doitt
rbrown[(@)]doitt.nyc.gov

Paul Pluzhnikov

2004-10-15, 3:56 am

Rodrick Brown <rbrown[@]doitt.nyc.gov> writes:

> Hello everyone just looking for feedback on the following block of
> code on issues or concerns to watch out for


You are using standard IO; where is '#include <stdio.h>' ?
Who initializes line_cnt before first use?
Is line_cnt value correct when the second for() loop starts?
What happens if an input line is longer then 512 characters?
What happens if there are more then 100 lines of input?
Are buff[i] properly terminated with '\0' ?
How many times do you call strlen(linebuff) ?
Who free()s the memory you malloc() ?

1 bug for every 5 lines of code :-(

> Unix Systems Engineer The City of New York Dept. of Information
> Technology


Yikes. I would hold off on using that title for a while, if I
were you.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Barry Margolin

2004-10-15, 3:56 am

In article <2004101421590816807%rbrown@doittnycgov>,
Rodrick Brown <rbrown[@]doitt.nyc.gov> wrote:

> Hello everyone just looking for feedback on the following block of code
> on issues or concerns to watch out for, I'm new to C but not
> programming so please be as detailed as possible on do's and do not's
> thanks :)
>
>
> #include <stdlib.h>
> #include <string.h>
>
> int
> main(int argc,char **argv)
> {
> FILE *fp;
> char linebuff[512];
> char *buff[100];
> int line_cnt,n = 0;
>
> if( argc != 2)
> {
> fprintf(stdout,"usage: %s [filename]\n",argv[0]);


The usage line indicates that the filename is optional, but you actually
require it. The Unix convention is that if the filename is omitted, the
program reads from stdin.

> exit(EXIT_FAILURE);
> }
>
> if ( (fp = fopen(argv[1],"r")) == NULL)
> {
> perror("Fatal: fopen() unable to open file");
> exit(EXIT_FAILURE);
> }
>
>
> while(fgets(linebuff,sizeof(linebuff),fp
))
> {
> buff[line_cnt] = malloc(strlen(linebuff)+1);
> strncpy(buff[line_cnt],linebuff,strlen(l
inebuff));
> line_cnt++;
> }
>
> for(n = 0; n < line_cnt; line_cnt--)
> {
> fprintf(stdout,"%s",buff[line_cnt]);
> }
>
> return EXIT_SUCCESS;
> }


--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Rodrick Brown

2004-10-15, 3:56 am

On 2004-10-14 22:47:49 -0400, Paul Pluzhnikov
<ppluzhnikov-nsp@charter.net> said:

> Rodrick Brown <rbrown[@]doitt.nyc.gov> writes:
>
>
> You are using standard IO; where is '#include <stdio.h>' ?
> Who initializes line_cnt before first use?
> Is line_cnt value correct when the second for() loop starts?
> What happens if an input line is longer then 512 characters?
> What happens if there are more then 100 lines of input?
> Are buff[i] properly terminated with '\0' ?
> How many times do you call strlen(linebuff) ?
> Who free()s the memory you malloc() ?
>
> 1 bug for every 5 lines of code :-(
>
>
> Yikes. I would hold off on using that title for a while, if I
> were you.
>
> Cheers,


stdio.h was left out during the past I guess.
dont int foo1,foo2,foo3 = 0; initilize all varibles to the same value ?

when sinse when do you need to know C to be a Unix admin :-P
Ok i've cleaned up the code somewhat based on the feedback you gave me
please let me know what you think now.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc,char **argv)
{
FILE *fp;
char linebuff[1024];
char **buff;
int line_cnt = 0,n = 0;

if( argc != 2)
{
fprintf(stdout,"usage: %s [filename]\n",argv[0]);
exit(EXIT_FAILURE);
}

if ( (fp = fopen(argv[1],"r")) == NULL)
{
perror("Fatal: fopen() unable to open file");
exit(EXIT_FAILURE);
}

if ( (buff = malloc(sizeof(buff)/sizeof(char *))) == NULL)
{
perror("fatal: malloc() failed");
exit(EXIT_FAILURE);
}

while(fgets(linebuff,sizeof(linebuff),fp
))
{
buff[line_cnt] = malloc(sizeof(linebuff)+1);
strncpy(buff[line_cnt],linebuff,strlen(l
inebuff));
line_cnt++;
}

for(n = 0; n < line_cnt; line_cnt--)
{
fprintf(stdout,"%s",buff[line_cnt]);
free(buff[line_cnt]);
}

free(buff);
return EXIT_SUCCESS;
}



--
Unix Systems Engineer
The City of New York
Dept. of Information Technology
http://www.nyc.gov/doitt
rbrown[(@)]doitt.nyc.gov
http://www.rodrickbrown.com

Paul Pluzhnikov

2004-10-15, 3:56 am

Rodrick Brown <rbrown[@]doitt.nyc.gov> writes:

> dont int foo1,foo2,foo3 = 0; initilize all varibles to the same value ?


Nope. Only 'foo3' is initialized above.

> sinse when do you need to know C to be a Unix admin :-P


Since when "Systems Engineer" == "Unix admin" ?
And all the *really* good symins (I've met just one, so my sample
size is somewhat limited :) do know at least some 'C'.

> Ok i've cleaned up the code somewhat based on the feedback you gave me
> please let me know what you think now.


Get yourself a Linux box, compile your program on it, and run it
under valgrind. You've fixed 2 bugs and introduced at least 1 new one.

What do you think 'sizeof(buff)/sizeof(char *)' evaluates to?
[Hint: it's a rather small constant :]

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Stephane CHAZELAS

2004-10-15, 3:56 am

Why not installing GNU tac (in GNU coreutils).

Note that GNU tac (or tail -r on some systems) don't allocated
the whole file in memory when it's a regular file. It makes use
of ls to read the file backward. You could also use mmap.

--
Stephane


Barry Margolin

2004-10-16, 3:56 am

In article <slrncmusoq.2ec.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS <this.address@is.invalid> wrote:

> Why not installing GNU tac (in GNU coreutils).


He'd better remove the copyleft notices, or the teacher will probably
figure out that he plagiarized the assignment.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com