-
curious code
Ok, this might take some explaining. here goes.
In my program the user enters a full filepath and the program takes the filename and outputs access time, modify time, and size to screen and file until the user enters quit.
I ran into a segmentation fault just after the beginning of my main while loop (where user enters the filenames). the weird thing is that after i entered the third filename the seg fault happened.
(why 3?) also i put an ordinary cout<<"1\n"; at the very beginning of the while loop and my problems were solved.
why did this cout fix my segmentation fault?
If i my explanation is confusing or if any ?'s email me at [email protected] or post a message here |
V
-mungyun
-
I'm not sure what you will need to know, so here is the source.
FYI: This is my first attempt at a useful program.
I'm not sure if i am even going in the right direction. i'm doing this without much help.
its messy.
-
also, i removed the cout and the segmentation fault didnt come back i dont know why.
the loop starts at line 107
-
>line 149 (maybe others): while(!feof(fdtmp))
don't control the loop in this manner, use the return code from the function doing the reading.
>line 36: char IOFilename[33];
>line 165: filename[50]
The lengths are different.
>line 182: strcpy(n_ptr->IOFilename,filename);
If I'm following your code correctly, I think both these pointers point to the same memory? Is the result undefined behaviour?
Hope this helps a little!
-
> struct Frec *s_ptr=(struct Frec *)malloc(sizeof(Frec));// initializes the linked list
This isn't the best way to initialise a linked list - you're forcing the user to have at least one node in the list
An empty list is
struct Frec *s_ptr = NULL;
In addition, this being C++, you don't need struct, so
Frec *s_ptr = NULL;
Finally, you should be using new/delete for memory allocations in C++
Frec *s_ptr = new Frec;
> FILE *fdtmp=fopen("crdet.init","r");
> FILE *fd=fopen("crdet.init","a");
My initial impression is that fd will always be NULL - you're opening (always) the same file using two incompatible modes.
I suggest you open the file in the appropriate mode, when you've decided which command line option you're dealing with
It would help to know which command line option you tried.
-
the command line i used was: ./crdet -init
then i used /bin/ls
/bin/cp
/bin/rm
those are what i used to test input and such.
thanks for the help on linked lists i don't know them well so this was just an experiment.