0

I have some problem with my recover code. I based it on copy.c so the beggining lines are :

    int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: call the program with the full name of storage source.\n");
        return 1;
    }

    // remember source filename
    char *infile = argv[1];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

When I run program like this I get seg fault - but when it is done step by step with gdb it shows me that it stops at the line:

if (argc != 2)
{
    fprintf(stderr, "Usage: call the program with the full name of storage source.\n");
    return 1;
}

and its quite amaizing to me get it so I changed

(argc != 2) for (argc != 1)

and I get

call the program with the full name of storage source.

like i should but running with gdb it shows that it is running till

if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

so it passes the 1st if test - or maybe I do not understand something? I am confused but: why I cannot read card.raw file ?

2
  • Is this card.raw in the same directory as the source code? – MARS Mar 13 '18 at 20:09
  • What is the command that you're using to execute the command? – Cliff B Mar 13 '18 at 23:19
1

Your code looks perfectly fine. It checks that argv[1] exists and then it checks that it can open that file.

So ./recover card.raw is being tested properly.

If you are segfaulting, it's not because of this bit of code.

edit to answer comment

So the issue is with gdb?

When I run gdb I get exactly what you'd expect:

~/workspace/fall2017/pset4/recover/ $ gdb ./recover
Reading symbols from ./recover...done.
(gdb) run
Starting program: /home/ubuntu/workspace/fall2017/pset4/recover/recover 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Usage: call the program with the full name of storage source.
[Inferior 1 (process 82342) exited with code 01]
(gdb) 

So it's definitely catching the lack of argv[1] and printing your error message.

If instead, I actually give it the argument, it runs to completion.

~/workspace/fall2017/pset4/recover/ $ gdb ./recover
Reading symbols from ./recover...done.
(gdb) run card.raw
Starting program: /home/ubuntu/workspace/fall2017/pset4/recover/recover card.raw
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Inferior 1 (process 82452) exited normally]
(gdb) 

Are you running gdb differently?

5
  • yes but why does it not go (running of the program) // open input file FILE *inptr = fopen(infile, "r"); if (inptr == NULL) { fprintf(stderr, "Could not open %s.\n", infile); return 2; } when I do you gdb ? I cannot wrap my head around that – koko loko Mar 14 '18 at 14:08
  • @kokoloko I edited my answer. – curiouskiwi Mar 15 '18 at 4:59
  • I ran gdb like this "gdb ./recover card.raw" when I run it like you say(gdb recover card.raw) I get: "/home/ubuntu/workspace/pset4/recover/card.raw" is not a core dump: File format not recognized - right now I will look into this problem but from what I understand I have some problem with card.raw not with my code. Am i correct? – koko loko Mar 15 '18 at 16:07
  • I will not delete previous comment but I see that I have used wrongly gdb I am starting to recgneze that I am doing a lot of stuff with blindfold (a'ka i do not know what excacly i am doing). Right now I think i am getting how gdb works.Now I run the program with gdb and I see I have problem with fread. thank you so much! – koko loko Mar 15 '18 at 16:22
  • I still have the problem with segfaoult but right now I got this: [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Program received signal SIGSEGV, Segmentation fault. _IO_new_fclose (fp=0x0) at iofclose.c:54 54 iofclose.c: No such file or directory. I will try for one/two days more then I will ask again ;) – koko loko Mar 15 '18 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .