Thread: segmentation fault

  1. #16
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    955
    Quote Originally Posted by sharonch View Post
    I think I have correct the warning. No segmentation faults, but now the "line" and "content" is not writing to where it suppose to.
    Code:
    #include <stdio.h>
    Code:
    int main()
    {
      getKey();
      return0;
    }
    void getKey()
    {
      FILE *fp, *fp2;
      char line[100] = "#\n#the following key is for lastfirst\n#\n";
      char content[1206];
      system("cd /home/user/.ssh/;pwd | ssh-keygen -t rsa -N \'\' -q -f lastfirst");
      if(fp2 = fopen("lastfirst.pub", "r"))
        {
          for(content; !feof(fp2); fscanf(fp2, "%s", content));
          fclose(fp2);
        }else{  return;  }
    
      fp = fopen("lastfirst.pub", "w+");
          fprintf(fp, "%s", line);
          fprintf(fp, "%s", content);
      fclose(fp); 
    return;
    }
    Assuming "return0" has a space between "return" and "0" (given the different colors of the two), this does compile with several warnings. It also does exactly what I expected it to do after I mentally stepped through your code. It generates a key pair named lastfirst and lastfirst.pub (assuming those files don't already exist, of course), reads in one word at a time from lastfirst.pub into the "content" array, and then overwrites the lastfirst.pub file with three lines of comments and the last word from the original lastfirst.pub file. In my case, at least, this last word is my username@hostname, which is the comment field from the public key. A public key has three fields: the key type (ssh-rsa), the base64-encoded key, and an optional comment field.

    Are you trying to copy the entire line to the new lastfirst.pub file? Then you'll have to read and save all three fields before overwriting the original file. Your code currently reads all three fields but overwrites the previous field each time, so only the last field is really saved.
    Last edited by christop; 07-16-2012 at 05:12 PM. Reason: clarification

  2. #17
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Please allow me to answer my own question. After testing things for times. I figure that the fopen didn't go to the directory that I am working with to open that particular file. That is why it is not generating the things I want.

  3. #18
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    955
    Quote Originally Posted by sharonch View Post
    Please allow me to answer my own question. After testing things for times. I figure that the fopen didn't go to the directory that I am working with to open that particular file. That is why it is not generating the things I want.
    That is a possible problem too. I don't have a directory called /home/user/.ssh, so ssh-keygen just plopped the files in my current directory, and then your program had no problem finding lastfirst.pub there.

  4. #19
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by christop View Post
    Are you trying to copy the entire line to the new lastfirst.pub file? Then you'll have to read and save all three fields before overwriting the original file. Your code currently reads all three fields but overwrites the previous field each time, so only the last field is really saved.
    Yes, I am trying to copy the entire public key into the variable "content". and paste the "line" in the beginning of the new public key then append the content after "line". now, it is working with the correct directory, but "line" is still not in. I am trying fopen(~, "a"), still no luck. Any idea?

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by sharonch View Post
    It compiled. Your statement is not true.
    Jimmy probably meant 'it doesn't compile without errors and warnings'.

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by dmh2000 View Post
    Jimmy probably meant 'it doesn't compile without errors and warnings'.
    I don't think that's what he meant.

    Quote Originally Posted by Jimmy View Post
    That code does not even compile.
    Not least for the line:

    Code:
    return0;

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by sharonch View Post
    After testing things for times. I figure that the fopen didn't go to the directory that I am working with to open that particular file. That is why it is not generating the things I want.
    If you change the directory within a system()-call, the working directory of your program isn't changed. The OS spawns a new subprocess for every system()-call which is independent of your program's process and closed immediately after the call has finished.
    fopen() always looks for a file in the working directory of your program if you don't specify a full path.

    Is there a reason why you don't use the absolute path for the fopen()-call?

    Bye, Andreas

  8. #23
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Thanks for all the meaningful replies. I solved all of the problems that I have asked earlier. I would like to recap what happen:
    1. correct warning. Since a lot of warning I have are not as harmful. The C compiler is smart enough to figure out and make the whole thing run which is very helpful. They are easy to fix, too.
    2. use fopen() or system(). I need to use system() instead of fopen() because I need the result to be generated right after the command finish because the code after system() are going to deal with the result. Plus. I am too carelessly writing the code and forgot that I am not in some particular directory.
    3. some syntax issue. Well. I assume that I remember certain syntax, such as fprintf(), but I don't. It is bad. I should have double check that all the time.
    Again. Thanks for everyone's help! See you later!

  9. #24
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by dmh2000 View Post
    Jimmy probably meant 'it doesn't compile without errors and warnings'.
    Yes, that is kind of what "doesn't compile" means... :-)

    If you run it through a compiler and get no executable due to errors during the compilation then it "doesn't compile". But clearly the OP has a compiler that magically creates working executables from broken code, because I could not cut and paste that code and compile it. After that I wouldn't even bother to look at the code, I'd rather look at code from somebody that can be bothered to at least compile the code before posting or at least not be rude and implying that I'm lying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault?
    By onako in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2010, 10:51 PM
  2. Segmentation fault
    By phoneix_hallows in forum C Programming
    Replies: 8
    Last Post: 08-27-2009, 05:56 AM
  3. Segmentation fault
    By Buckshot in forum C++ Programming
    Replies: 14
    Last Post: 06-23-2005, 08:20 AM
  4. segmentation fault : ?????
    By gemini_shooter in forum C Programming
    Replies: 5
    Last Post: 06-06-2005, 02:18 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM