Segmentation fault错误
#include <stdio.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
struct DIR *dp;
struct dirent *temp_dp;
char temp_name[NAME_MAX+1];
struct stat temp_stat;
int temp_fd;
dp=opendir("/dev/");
if(dp==NULL){
fprintf(stderr,"open /dev/ dir error");
exit(0);
}
strcpy(temp_name,"/dev/");
while((temp_dp=readdir(dp))!=NULL){
strncpy(temp_name+sizeof("/dev/")-1,
temp_dp->d_name,NAME_MAX);
if(strcmp(temp_name,"/dev/.")==0 || strcmp(temp_name,
"/dev/..")==0)
continue;
if((temp_fd=open(temp_name,O_RDONLY))<0){
fprintf(stderr,"open file %s error\n",temp_name);
exit(0);
}
if(stat(temp_name,&temp_stat)<0){
fprintf(stderr,"get file %s stat error\n",temp_name);
exit(0);
}
if(isastream(temp_fd)){
printf("%s is a stream device ",temp_name);
if(S_ISREG(temp_stat.st_mode)){
printf("and regular file");
}else if(S_ISDIR(temp_stat.st_mode)){
printf("and dir file");
}else if(S_ISCHR(temp_stat.st_mode)){
printf("and charact device file");
}else if(S_ISBLK(temp_stat.st_mode)){
printf("and block device file");
}else if(S_ISFIFO(temp_stat.st_mode)){
printf("and fifo file");
}
printf("\n");
}
close(temp_fd);
}
}
怎么输出Segmentation fault错误?
相关推荐
Segmentation Fault错误原因总结 _ Sissy 婷婷 Blog1
Segmentation Fault,通常简称为SIGSEGV,是Linux和其他类Unix操作系统中的一种错误,它发生在程序尝试访问其无权访问的内存区域时。
Python解决Segmentation Fault错误
在Python编程中,有时候你可能会遇到"Segmentation Fault"(分段错误)的错误。本文将向你介绍一些常见的原因和解决方法,帮助你解决Python中的Segmentation Fault错误。当你使用数组或者其他缓冲区来存储数据时,如果写入的数据超过了缓冲区的边界,就可能导致缓冲区溢出。当你的程序陷入无限递归时,栈空间可能会耗尽,从而导致Segmentation Fault错误。由于缓冲区的长度只有5,而我们却访问了超出范围的索引,这就导致了Segmentation Fault错误。
C_C++中的段错误(Segmentation fault)[转] - 温柔的暴力 - 博客园1
【C/C++中的段错误(Segmentation fault)】在计算机编程中,特别是在使用C或C++这类低级语言时,段错误(Segmentation fault)是一个常见的运行时错误,它发生在程序试图访问其不应该访问的内存区域时
Linux环境下段错误(Segmentation fault)的产生原因及调试方法
在Linux环境下,编程时经常会遇到一个让开发者头疼的问题——段错误(Segmentation fault)。这个问题通常是由于程序尝试访问其不应该访问或者无法访问的内存区域而触发的。
Linux下的段错误(Segmentation fault)产生的原因及调试方法(经典)
一旦进程接收到该信号且未定义对应的信号处理函数,系统默认动作就是终止该进程并生成核心转储文件(core dump),同时在终端输出“Segmentation fault”字样。


