mafendee
asked on
strcpy() or malloc() problem !!
hi,
i have to tried to create a program which should display n last line from a file specified at command line. here is the code (i called it mylast.c)
######################
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define DEFAULT 5
/* This program has a Segmentation Fault error when running
with -33 or greater lastfile and not when n < 33.
*/
int main(int argc, char **argv)
{
if (argc<2 || argc>3) {
printf("Usage: last [-n] <filename>\n");
exit(1);
}
int line;
char *filename;
if (argc == 2) {
line = DEFAULT;
filename = argv[1];
} else {
if (argv[1][0] == '-') {
line = atoi(argv[1]) * -1;
} else {
printf("Usage: last [-n] <filename>\n");
exit(1);
}
filename = argv[2];
//printf("line = %d, filename = %s\n", line, filename);
}
FILE *fp;
char **abuffer;
char buffer[100];
if ((fp = fopen(filename, "rt")) == NULL) {
fprintf(stderr, "Can't open file %s\n", filename);
exit(1);
}
abuffer = (char **)malloc(line * sizeof(char));
for (int i=0; i<line; i++)
{
abuffer[i] = (char *)malloc(100*sizeof(char)) ;
}
/* why we got a Segmentation Fault if line > 32 */
int x=0;
while (fgets(buffer, 100, fp) != NULL) {
for (int j=line-1; j>0; j--) {
printf("i is %d\n", j);
printf("abuffer[j] = %s, abuffer[j-1] = %s\n", abuffer[j], abuffer[j-1]);
strcpy(abuffer[j], abuffer[j-1]);
}
x++;
printf("x is %d\n", x);
strcpy(abuffer[0], buffer);
printf("buffer = %s and abuffer[0] = %s\n", buffer, abuffer[0]);
}
for (int k=line-1; k>=0; k--) {
fprintf(stdout, "%s", abuffer[k]);
}
}
################
i have compiled it without any error
> CC mylast.c -o mylast
i have executed it without any error when it trying to display n last line where n <= 32.
> mylast -32 lastfile #lastfile just a file containing text to be displayed
BUT when i do,
> mylast -33 lastfile
i have got Segmentation Fault(coreDumped)
Any idea why it happened when line number is > than 32???
lastfile contains:
################
hai my name is robert
how are you
i am fine thanx
are u happy
i am sure u are
dont mad at me
stop making a noise
i am sick of it
i am sure u know that
sleep and dream
orange
apple
######################
i have to tried to create a program which should display n last line from a file specified at command line. here is the code (i called it mylast.c)
######################
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define DEFAULT 5
/* This program has a Segmentation Fault error when running
with -33 or greater lastfile and not when n < 33.
*/
int main(int argc, char **argv)
{
if (argc<2 || argc>3) {
printf("Usage: last [-n] <filename>\n");
exit(1);
}
int line;
char *filename;
if (argc == 2) {
line = DEFAULT;
filename = argv[1];
} else {
if (argv[1][0] == '-') {
line = atoi(argv[1]) * -1;
} else {
printf("Usage: last [-n] <filename>\n");
exit(1);
}
filename = argv[2];
//printf("line = %d, filename = %s\n", line, filename);
}
FILE *fp;
char **abuffer;
char buffer[100];
if ((fp = fopen(filename, "rt")) == NULL) {
fprintf(stderr, "Can't open file %s\n", filename);
exit(1);
}
abuffer = (char **)malloc(line * sizeof(char));
for (int i=0; i<line; i++)
{
abuffer[i] = (char *)malloc(100*sizeof(char))
}
/* why we got a Segmentation Fault if line > 32 */
int x=0;
while (fgets(buffer, 100, fp) != NULL) {
for (int j=line-1; j>0; j--) {
printf("i is %d\n", j);
printf("abuffer[j] = %s, abuffer[j-1] = %s\n", abuffer[j], abuffer[j-1]);
strcpy(abuffer[j], abuffer[j-1]);
}
x++;
printf("x is %d\n", x);
strcpy(abuffer[0], buffer);
printf("buffer = %s and abuffer[0] = %s\n", buffer, abuffer[0]);
}
for (int k=line-1; k>=0; k--) {
fprintf(stdout, "%s", abuffer[k]);
}
}
################
i have compiled it without any error
> CC mylast.c -o mylast
i have executed it without any error when it trying to display n last line where n <= 32.
> mylast -32 lastfile #lastfile just a file containing text to be displayed
BUT when i do,
> mylast -33 lastfile
i have got Segmentation Fault(coreDumped)
Any idea why it happened when line number is > than 32???
lastfile contains:
################
hai my name is robert
how are you
i am fine thanx
are u happy
i am sure u are
dont mad at me
stop making a noise
i am sick of it
i am sure u know that
sleep and dream
orange
apple
######################
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanx for the quick answer. I am actually now moving into C/Unix programming.
most probably loads of questions to come and of course i am also quiet generous with points.
most probably loads of questions to come and of course i am also quiet generous with points.
running amok in unallocated memory as soon as
you hit abuffer[4].
Ever heard of the command tail(1) ?
$ tail -33 <filename>