I create a C program:
#include <stdio.h>
void user_connect( char user[], char date[]){
FILE* fichier=NULL;
fichier=fopen("log.txt","+a");
fprintf(fichier,"user:%s connected at :%s",user,date);
fclose(fichier);
}
void etat_periph( char periph[]){
FILE* fichier=NULL;
fichier=fopen("log.txt","+a");
fprintf(fichier,periph);
fclose(fichier);
}
int main()
{
char user[]="user";
char periph[]="led is on";
char date[]="02/08/2015";
user_connect(user,date);
etat_periph(periph);
return 0;
}
when I run it I got this error message "segmentation fault(core dumped)"
I tried to debugger it but I didn't get any valuable thing to resolve it.
You should change your code to
fichier=fopen("log.txt","a+");
"+a" -> "a+"
Related
I am using turbo c++. I am getting the error "Declaration Syntax error" in FILE *fp;
the following is my code.
#include <bios.h>
#include <dos.h>
FILE *fp;
unsigned char buf[512];
unsigned char st[60];
unsigned char headno[10];
unsigned char secno[10];
unsigned char trackno[10];
void main (void)
{
int i ;
for (i=0;i<512;i++)
buf[i]=0;
gets(st);
fp=fopen(st,"wb");
printf("Head ");
gets(headno);
puts (headno);
printf("\nsector ");
gets(secno);
puts(secno);
printf("\ntrack ");
gets(trackno);
puts(trackno);
i = biosdisk(2,0x80,atoi(headno),
atoi(trackno),atoi(secno),1,buf) ;
if (*(((char *)(&i))+1)==0)
{
fwrite(buf,1,512,fp);
fclose(fp);
}
else
printf("Cannot Read Error# = %x",i);
}
The error is shown in the image1
FILE* and fopen() normally require you to #include <stdio.h>. I assume that is true for Turbo C++ too.
I am constantly getting "Segmentation fault (Core dumped)" error in the following c program. Please help me debug the program.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int numcmp(const char *a,const char *b){
if(atoi(a)==atoi(b)) return 0;
return -1;
}
int isequal(char *a,char *b,int (*cmp)(const char *,const char *)){
if(!cmp(a,b)) return 1;
return 0;
}
int main(int argc,char *argv[]){
if(argc!=3){
puts("Usage: Compare two string alphabeticaly or numericaly");
printf("Syntax: %s string1 string2\n",argv[0]);
exit(-1);
}
if(isequal(argv[1],argv[2],isalpha(argv[1])?strcmp:numcmp))
printf("%s and %s are equal\n",argv[1],argv[2]);
else
printf("%s and %s are not equal\n",argv[1],argv[2]);
}
isalpha only works for chars, not whole strings. You could write a function for that:
/* 1 - only letters, 0 - other characters */
int str_isalpha(const char* str) {
while (*str)
if (!isalpha(*(str++)))
return 0;
return 1;
}
and then use it appropriately. But actually, I don't see a reason for using numcmp here at all. Comparing ints and const char*s should yield the similar results.
If I had this code all inside just one function, I could get everything working fine, but now my problem is that I can't read the results of my regex. When I try printing the first result, I get a segmentation fault. Instead, I was expecting the first regex result to appear of "T/2/b".
Also, the word "Matches" appears on the screen right before the segmentation fault occurred.
Is there something I'm missing?
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
typedef struct{
char str[1000];
} regex;
long regexmatch(const char* str,const char* regexs,size_t nummatch,regex** reg){
regex_t r;regmatch_t match[nummatch];
if (regcomp(&r,regexs,REG_EXTENDED) != 0){return -1;}
if (regexec(&r,str,nummatch,match,0)!=0){regfree(&r);return -1;}
size_t i=0;
for (i=0;i<nummatch;i++){
if (match[i].rm_so > -1){
memset((**reg).str,0,1000);
memcpy((**reg).str,(char*)(str+match[i].rm_so),match[i].rm_eo-match[i].rm_so);
(*reg)++; //this is where I load the result into the struct and advance the pointer.
}
}
char *p=(**reg).str;
*p='\0';
regfree(&r);
return 0;
}
int main(){
char buf[1000];
memset(buf,0,1000);
regex* r=(regex*)buf;
if (regexmatch("T/2/b","([A-Z]+)/([0-9]+)/([a-z]+)",10,&r)==0){
printf("Matches\n");
printf("%s\n",r->str); //causes segmentation fault. Expecting a "T/2/b" to be displayed instead.
}
}
I am getting a segmentation fault on the execution of the code below.The program compiles successfully ,but gives an error on execution.
What is a segmentation fault and how to correct it?
#include<stdio.h>
#include<malloc.h>
#include "sel.h"
#include<sys/time.h>
main(int argc,char *argv[])
{
struct timeval t1,t2;
struct timezone tz;
int i,n;
int *a;
char *num;
FILE *fp;
unsigned long long time1,time2;
fp=fopen("file1.txt","w");
sscanf(argv[1],"%d",&n);
a=(int*)malloc (sizeof(int)*n);
for(i=n;i>0;i--)
{
sprintf(num,"%d\n",i);
fputs(num,fp);
}
fclose(fp);
fp=fopen("file1.txt","r");
i=0;
while(fgets(num,255,fp)!=NULL)
{
sscanf(num,"%d",&a[i++]);
}
gettimeofday(&t1,&tz);
time1=t1.tv_sec*1000000+t1.tv_usec;
sel(a,n);
gettimeofday(&t2,&tz);
time2=t2.tv_sec*1000000+t2.tv_usec;
fclose(fp);
fp=fopen("file2","w");
for(i=0;i<n;i++)
{
sprintf(num,"%d\n",a[i]);
fputs(num,fp);
}
free(a);
printf("\n %llu",time2-time1);
}
Segmentation fault means you had a bad memory access. In this case, you never allocated any memory for num, so you are writing to an uninitialized pointer.
I have problems using fopen() in c.
When I compile and run this:
const char* mode="r";
FILE* imgstream = fopen(pathinput, mode);
I am getting a Segmentation fault:
main called
argv[1]: /home/student/workspace/Flip/res/test.pbm
open mode: r
./testscript: line 6: 12454 Segmentation fault ./flippr /home/student/workspace/Flip/res/test.pbm /home/student/workspace/Flip/test_out.pbm
It appears to me that it should be working ...
What am I doing wrong?
the whole code of main.c :
#include "flip.h"
#include "img.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int pbm_image_flip(PbmImage* image){
return 0;
}
int main(int argc, char **argv) {
#ifdef DEBUG
printf("main called\n");
#endif
int* error=RET_PBM_OK;
#ifdef DEBUG
printf("argv[1]: %s\n", argv[1]);
#endif
const char* mode="r";
#ifdef DEBUG
printf("open mode: %s\n", mode);
#endif
FILE* imgstream = fopen(argv[1], mode);
#ifdef DEBUG
printf("imgstream fopen");
#endif
PbmImage* pbmimage=pbm_image_load_from_stream(imgstream, error);
pbm_image_flip(pbmimage);
return 0;
}
The code runs until before I try to fopen the stream.
The error comes from this line:
const char* mode='r';
There is a difference between singe and double quotes in C. Single are for characters, double for string literals (which can be safely assigned to char*)
The solution for you would be
FILE* imgstream = fopen(pathinput,"r");
There is still one problem:
int* error = RET_PBM_OK;
will crash or will lead to a crash later. You are using an uninitialized pointer.
Write this:
int error = RET_PBM_OK ;
...
PbmImage* pbmimage=pbm_image_load_from_stream(imgstream, &error);
But this is bad practice. Normally pbm_image_load_from_stream shoud take care of setting error to RET_PBM_OK if it succeeds. In that case you don't even need to initialize error before calling pbm_image_load_from_stream.