Z
Zach
I told gcc to compile with "-g" yet when I run the debugger it says no
symbols are found and the very brief backtrace does not help me at
all.
foo@nickel:~/$ gcc -o main5 main5.c -g
main5.c: In function ‘main’:
main5.c:27: warning: passing argument 1 of ‘fstat’ makes integer from
pointer without a cast
What precisely does this warning mean? I don't understand why casting
would be needed in the fstat call. The file test.log is plain ascii
text.
foo@nickel:~/$ ./main5 test.log
Segmentation fault (core dumped)
foo@nickel:~/$ gdb --core=./core
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-linux-gnu".
(no debugging symbols found)
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.
1".
Core was generated by `./main5 test.log'.
Program terminated with signal 11, Segmentation fault.
#0 0xb7ea7158 in ?? ()
(gdb) bt
#0 0xb7ea7158 in ?? ()
(gdb) quit
Here is the program source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAXLINE 90
int main(int argc, char *argv[])
{
FILE *fp;
char buf[MAXLINE];
int *psz;
struct stat fileStat;
if((fp = fopen(argv[1], "r")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
while((fp = fopen(argv[1], "r")))
{
if(fstat(fp,&fileStat) < 0)
{
psz = malloc(fileStat.st_size);
}
}
while(fgets(buf, MAXLINE - 2, fp) != NULL)
{
char sub_string1[MAXLINE];
char sub_string2[MAXLINE];
char sub_string3[MAXLINE];
sscanf (buf,"%s %s %s",sub_string1,sub_string2,sub_string3);
printf ("%s\n %s\n %s\n",sub_string1,sub_string2,sub_string3);
}
fclose(fp);
free(psz);
return 0;
}
Is there a better way to check if whether the file is readable and non-
zero? I have:
while((fp = fopen(argv[1], "r")))
I just needed way to check if the file is legit and if so to allocate
memory. Is there a better way to do this or a way using a standard
library function?
Zach
symbols are found and the very brief backtrace does not help me at
all.
foo@nickel:~/$ gcc -o main5 main5.c -g
main5.c: In function ‘main’:
main5.c:27: warning: passing argument 1 of ‘fstat’ makes integer from
pointer without a cast
What precisely does this warning mean? I don't understand why casting
would be needed in the fstat call. The file test.log is plain ascii
text.
foo@nickel:~/$ ./main5 test.log
Segmentation fault (core dumped)
foo@nickel:~/$ gdb --core=./core
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-linux-gnu".
(no debugging symbols found)
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.
1".
Core was generated by `./main5 test.log'.
Program terminated with signal 11, Segmentation fault.
#0 0xb7ea7158 in ?? ()
(gdb) bt
#0 0xb7ea7158 in ?? ()
(gdb) quit
Here is the program source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAXLINE 90
int main(int argc, char *argv[])
{
FILE *fp;
char buf[MAXLINE];
int *psz;
struct stat fileStat;
if((fp = fopen(argv[1], "r")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
while((fp = fopen(argv[1], "r")))
{
if(fstat(fp,&fileStat) < 0)
{
psz = malloc(fileStat.st_size);
}
}
while(fgets(buf, MAXLINE - 2, fp) != NULL)
{
char sub_string1[MAXLINE];
char sub_string2[MAXLINE];
char sub_string3[MAXLINE];
sscanf (buf,"%s %s %s",sub_string1,sub_string2,sub_string3);
printf ("%s\n %s\n %s\n",sub_string1,sub_string2,sub_string3);
}
fclose(fp);
free(psz);
return 0;
}
Is there a better way to check if whether the file is readable and non-
zero? I have:
while((fp = fopen(argv[1], "r")))
I just needed way to check if the file is legit and if so to allocate
memory. Is there a better way to do this or a way using a standard
library function?
Zach