简体   繁体   中英

C - History of commands (core dumped)

I am trying to remove the line 20 of file1 . The rest of the program is a simple shell that is working only I can not make the history of commands used by the user. The error message that I have when I try to insert a command is: Segmentation fault (core dumped)

The code that I am executing is the following:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

#define GetCurrentDir getcwd  //get the path of file
#define BUFFER_LEN 1024

int ch;
FILE *file1, file2;
char line[BUFFER_LEN]    

while(1){
    if(!fgets(line, BUFFER_LEN, stdin))
    {                       
        break;              
    }
    else if(line, BUFFER_LEN, SIGQUIT){
                fopen("fileOne.c","r");
            while((ch = getc(file1)) != EOF){   
                printf("%s",ch);
            } 
        } 

        if(count<20)
        {
            fputs(argv[100] ,file1);  
        }
        else{
            fclose(file1);
            file1 = fopen("fileOne.c","r"); 
            rewind(file1);        
            file2 = fopen("repicla.c","w"); 
            while((ch = getc(file1)) != EOF){              
                if((ch = getc(file1)) != "\n"){         
                    count++;            
                    if(count != 20){                
                    putc(ch, file2);            
                    }
                }   
            }   
            fclose(file1);  
            fclose(file2);  
            remove("fileOne.c");    
            rename("replica.c","fileOne.c");    
            fputs(argv[100] ,file1);   
        }
}

Most significantly, I can't compile your code! I'll delve into that shortly, but I suspect with major errors like the ones in your code, you need a different book because whatever book you're reading (you are reading one rather than guessing , right?), that isn't working out too well for you. Have you considered K&R2E?

My compiler emits error messages , and so will yours. It's possible that you're trying to execute a stale binary which we can't help you with. You need to make your code compilable .

Assuming that issue is fixed, you have an error in the following code, which your compiler is likely to warn you about :

while((ch = getc(file1)) != EOF){   
    printf("%s",ch);
} 

You're passing an int corresponding to the %s format delegate to printf . You need to pass a char * . If you pass the wrong type, you get undefined behaviour, and this is likely to cause a crash.

In this case, I think you might have intended putchar(ch); or printf("%c",ch); .

Nonetheless, when your compiler yells at you about something, DO NOT IGNORE IT! If you don't understand the error or warning, ask a question about that, instead !

Given

int ch;

This line is undefined behavior:

printf("%s",ch);

%s is the format for a nul-terminated string. But ch is an int value, so the printf() code almost certainly treats the passed int value as an address and tries to use it as the starting address of a nul-terminated string.

That's a pretty sure-fire way to force a core dump from a SIGSEGV .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM