我有這個錯誤的大問題。我盡我所能從文件中讀取(fscanf,fgets,gets,fgetln,fread,read),但我無法管理它。每次出現分段錯誤(核心轉儲)錯誤)。我需要做些什麼才能打印我的文件內容。從文件讀取 - 分段錯誤(核心轉儲)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <signal.h>
#include <termios.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#define MAX_COMAND_LENGTH 100
#define MAX_NUMBER_OF_PARAMS 10
int i=0;
char cmd[MAX_COMAND_LENGTH+1];
char *params[MAX_NUMBER_OF_PARAMS+1];
char cmdline[1000];
int hfd=-1,ifd=-1,ofd=-1;
int lines_in_hist = 0;
int curent_line = -1;
struct termios save_term;
char *HISTORY;
char *TEMP,*TEMP2,*ax;
void parseCmd(char *cmd, char **params)
{
for(i=0; i< MAX_NUMBER_OF_PARAMS;i++){
params[i]=strsep(&cmd," ");
if(params[i] == NULL)
break;
}
}
int main(int argc, char *argv[])
{
char *username = getenv("USER");
int status=-1;//pentru deschiderea fisierelor
int status1;
char *directory="/tmp";
char *file_name;
char buff[256];
while(1)
{
printf("%[email protected] >>", username);
//citeste de pe linia de comanda
if(fgets(cmd,sizeof(cmd),stdin)==NULL)
break;
//elimina terminatorul de sir de pe o linie noua
if(cmd[strlen(cmd)-1]=='\n')
cmd[strlen(cmd)-1]='\0';
parseCmd(cmd,params);
if(!strcmp(params[0],"exit"))
exit(0);
if(!strcmp(params[0],"help"))
help();
if(!strcmp(params[0],"version"))
version();
if(!strcmp(params[0],"info"))
if(!strcmp(params[1],"tail"))
infoTail();
else if(!strcmp(params[1],"uniq"))
infoUniq();
else if(!strcmp(params[1],"cd"))
infoCd();
if(!strcmp(params[0],"uniq"))
{
if(!strcmp(params[1],"-d"))
{
printf("Enter the name of file\n");
gets(file_name);
if((status=open(file_name,O_RDONLY))==-1)
{
printf("Nu am putut deschide fisierul!");
exit(1);
}
else
{
printf("\t\t ==>%s<==\n",file_name);
sscanf(file_name,"%s",buff);
printf("Continutul fisierului:\n%s\n", buff);
}
close(status);
}
}
if(!strcmp(params[0],"cd"))
{
status1=chdir(directory);
if(status1 !=0)
perror("Eroare!");
}
}
return 0;
}
如果你在崩潰後在gdb中做了一個bt,你會看到它在哪裏爆炸它的負載。可能會讓你更接近根本原因。一般也可以打開.core文件。 –