带文件的分段错误(核心转储)(C-linux)

时间:2021-11-03 22:27:47

I'm getting a segmentation error (core dump) when I try to run this. It compiles perfectly but I get the error, and I don't know why. There must be a problem with a file writing because without this works good. Any help would be great. Thanks!

当我尝试运行它时,我收到了分段错误(核心转储)。它完美编译,但我得到错误,我不知道为什么。文件写入一定存在问题,因为没有这个工作就好了。任何帮助都会很棒。谢谢!

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>
#include <string.h>

int
main(void)
{

FILE *f=fopen("shadow1.txt","w");


  if (f=NULL)
  {
    printf("ERROR");

  }


  unsigned long seed[2];
  char salt[] = "$1$........";
  const char *const seedchars =
    "./0123456789ABCDEFGHIJKLMNOPQRST"
    "UVWXYZabcdefghijklmnopqrstuvwxyz";
  char *password;
  int i;

  /* Generate a (not very) random seed.
     You should do it better than this... */
  seed[0] = time(NULL);
  seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);

  /* Turn it into printable characters from ‘seedchars’. */
  for (i = 0; i < 8; i++)
    salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];

  /* Read in the user’s password and encrypt it. */
  password = crypt(getpass("Password:"), salt);

  /* Print the results. */
  //fprintf(f,"%s $ %s",password);
  printf("Success Registration to file !");
  fclose(f);
  return 0;

}

3 个解决方案

#1


2  

 if (f=NULL)
  {
    printf("ERROR");

  }

was the problem...

是问题......

#2


1  

void Register(char u,char p) {

void Register(char u,char p){

you probably want these to be char * because of the fprintf that treats them as strings:

你可能希望这些是char *,因为fprintf将它们视为字符串:

fprintf(f,"%s $ %s",u,p);

fprintf(f,“%s $%s”,u,p);

and since you pass char *s in:

因为你传递char * s:

char *password,*username;
//...
Register(username,password);

This would most likely have been caught by compiler warnings. It is a lot faster to get your answer from the compiler than from here.

这很可能是由编译器警告捕获的。从编译器获得答案要快得多。

If you can't figure out why your program isn't working, you can enable all the warnings you should need with -Wall -Wextra and turn warnings into errors with -Werror.

如果您无法弄清楚程序无法正常工作的原因,您可以使用-Wall -Wextra启用所需的所有警告,并使用-Werror将警告转换为错误。

#3


1  

You are not allocating space to hold username so it will segfault on the scanf.

您没有分配空间来保存用户名,因此它会在scanf上发生段错误。

#1


2  

 if (f=NULL)
  {
    printf("ERROR");

  }

was the problem...

是问题......

#2


1  

void Register(char u,char p) {

void Register(char u,char p){

you probably want these to be char * because of the fprintf that treats them as strings:

你可能希望这些是char *,因为fprintf将它们视为字符串:

fprintf(f,"%s $ %s",u,p);

fprintf(f,“%s $%s”,u,p);

and since you pass char *s in:

因为你传递char * s:

char *password,*username;
//...
Register(username,password);

This would most likely have been caught by compiler warnings. It is a lot faster to get your answer from the compiler than from here.

这很可能是由编译器警告捕获的。从编译器获得答案要快得多。

If you can't figure out why your program isn't working, you can enable all the warnings you should need with -Wall -Wextra and turn warnings into errors with -Werror.

如果您无法弄清楚程序无法正常工作的原因,您可以使用-Wall -Wextra启用所需的所有警告,并使用-Werror将警告转换为错误。

#3


1  

You are not allocating space to hold username so it will segfault on the scanf.

您没有分配空间来保存用户名,因此它会在scanf上发生段错误。