C-Segmentation fault (core dumped) error in linux - c

#include <stdio.h>
#include <stdlib.h>
struct fileIndex{
char name;
int key;
} index1;
int main(int argc, char *argv[]){
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
for(i=0; i<10; i++){
printf("%c",fgetc(pFile));
}
fclose(pFile);
}
When I want to run my program, it gives that error. I looked so long for a wrong line in code, but I didn't find any. Can you help me ?

If the file failed to open, that will make pFile equal NULL, which can easily cause fgetc() to segfault.
You must check for this before trying to read from the file:
if (pfile == NULL)
{
perror("Failed to open file");
exit(1);
}

change your code as
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
if(!pFile)
return;
Also.. looks like you file path is misplaced... are your meant t ouse /cat/home/sysadmin/deneme.txt
Is cat your current directory or part of absolute path

Related

How can I open a file in c using fgets() and an input file?

I am trying to open the file indicated by the input file[100]. I keep getting a segmentation fault. I know that there is some problem with the fgets() function, but I am not exactly sure what it is. I can't get any code inside the loop to run. The final code is supposed to take the file from the first line of a text file, but it does not work. Any help would be appreciated, and I can post the main function code if needed!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numInt.h>
#include <string.h>
char *getCoeff(char file[100], char chemName[50])
{
FILE* fptr = fopen(file, "r");
char data[80];
char *dataptr = data;
char line[80];
int count = 0;
int i = 0;
printf("%s",file);
while (fgets(line, sizeof line, fptr) != NULL)
{
printf("Inside loop\n");
printf("%s",line);
strcpy(data,line);
}
fclose(fptr);
return dataptr;
}

C fscanf segmentation fault error "No source available for "flockfile() at 0x7fff855e6d39"

I'm currently working on an old coding problem from USACO in C. Here are the first couple lines of my code, in which I am trying to use the fscanf() function to grab the first value, an int, from the blocks.in file:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fin = fopen ("blocks.in", "r");
FILE *fout = fopen ("blocks.out", "w");
int i,j;
int linecount = 0;
int alphabetCount[26];
fscanf(fin," %d",&linecount);
Running gdb (as a part of the Eclipse C/C++ IDE), I consistently get a segmentation fault error on the line:
fscanf(fin," %d",&linecount);
The error consistently reads:
No source available for "flockfile() at 0x7fff855e6d39"
I haven't been able to source the issue. I've not had any problems with this in the past. Do you see what is wrong, or have a better solution/function with which to extract the data?
I suspect that there is no blocks.in file in the directory from which you run the program. Even if the file is present, it may not open successfully. Some simple error-checking could help you avoid problems here:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fin;
FILE *fout;
int i,j;
int linecount = 0;
int alphabetCount[26];
if ((fin = fopen("blocks.in", "r")) == NULL) {
fprintf(stderr, "Unable to open input file\n");
exit(EXIT_FAILURE);
}
if ((fout = fopen("blocks.out", "w")) == NULL) {
fprintf(stderr, "Unable to open output file\n");
exit(EXIT_FAILURE);
}
fscanf(fin," %d",&linecount);
return 0;
}

Segmentation fault when calling void function

Edit: as requested I've included the code in 'ledic'. However, it never ran - any of it, not even a hello world printf as first line, so I am relatively sure the problem would never be withinit.
Edit2: ironically enough, it was within the 'ledic' function. Looks like I understand even less about this than I previously thought.
I am writing for my current project at Uni and no one around me can figure out this segmentation fault. It should be pretty straightforward so I appreciate your help.
Code as follows:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void ledic(FILE *fp){
printf("Hello world\n");
int len;
int j, i, k;
char palavra[30];
char dictionary[30][10000][30];
int VecOcorrencias[30];
for (j=0; j<30; j++)
VecOcorrencias[j]=0;
printf("Hello world\n");
while ( fscanf(fp, "%s", palavra) == 1 ) {
len = strlen(palavra);
k = VecOcorrencias[len];
strcpy (dictionary[len][k], palavra);
VecOcorrencias[len]++;
}
for (i=0; i<1000; i++)
printf("%s %d\n", dictionary[5][i], VecOcorrencias[5]);
}
}
FILE *OpenFile( char *nome, char *mode){
FILE *fp;
fp = fopen (nome, mode);
if( fp == NULL){
printf(" Cant open file\n");
exit(1);
}
return (fp);
}
int main( int argc, char *argv[]){
FILE * fp;
fp = OpenFile( argv[1], "r");
ledic(fp);
return(0);
}
The code breaks when it enters the void ledic(FILE *fp) function, and says it cannot access the referenced memory (I suppose *fp).
I cannot for the life of me figure out why. Any thoughts?
The declaration char dictionary[30][10000][30]; creates a 9Mb variable 30*30*1000 = 9'000'000. As it is a local variable it is created on the stack and the default stack size on a typical Linux machine is only 8Mb (on Windows it's even only 1Mb).
If you declare it as static, the variable is not on the stack and therefore it can take more memory than the stacksize.
See this SO article for more details on the static keyword.

Anyone able to tell me what causes the segmentation fault in this code?

Sorry to ask about this at such a late timing but I have an assignment due the next day and this is only the first out of the seven questions I have.My tutors and lecturers do not respond to my cries for help and I have been stuck on it for a few days already. I know that segmentation fault usually means that there is an illegal memory access somewhere but I cant figure out why it occurs. I have already allocated the memory and it seems like it occurs at the fseek area because theres no segmentation fault if i comment of that whole chunk after fseek().
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"fcntl.h"
int file_exists( char *filename)
//int main(int argc, char **argv[])
{
FILE *file;
if(file=fopen(filename,"r"))
//if(file=fopen(argv[1],"r"))
{
fclose(file);
return 1;
}
return 0;
}
int main(int argc, char *argv[] )
{
int fp,number,size,i;
char *buffer;
fp = open(argv[1], "w");
number=atoi(argv[3]);
printf("%d",number);
if(file_exists(argv[1])==1)
{
fseek(fp,0L,SEEK_END);
size=ftell(fp);
fseek(fp,0L,SEEK_SET);
if(size>number)
{
fseek(fp,number,SEEK_SET);
fprintf(fp,argv[2]);
}
if(size<number)
{
int spaces;
fseek(fp,0L,SEEK_END);
spaces=number-size;
buffer= malloc(spaces);
memset(buffer," ",spaces);
//for(i=0;i<spaces;i++)
//{
fprintf(fp,buffer);
//}
free(buffer);
buffer=malloc(number*sizeof(number));
fprintf(fp,argv[2]);
free(buffer);
}
}
if(file_exists(argv[1])==0)
{
creat(argv[1],"w");
buffer=malloc(number);
memset(buffer,' ',number);
//for( i=0;i<number;i++)
//{
fprintf(fp,buffer);
//}
free(buffer);
buffer=malloc(number);
fprintf(fp,argv[2]);
free(buffer);
}
return 0;
}
fprintf requires a file HANDLE, not a file DESCRIPTOR.
If you really want to use fprintf, use fopen to obtain a file handle from your file descriptor (fp).

Segmentation fault

Here is my code.
#include<stdio.h>
int main(int argc,char** argv)
{
FILE* fp;
fp=fopen(argv[1],"r");
struct element{
int value;
char activity;
};
typedef struct element element;
element a;
printf("%d",feof(fp));
}
Now if I don't give the last printf command it does not give me a segmentation fault, but if I give it printf it gives me a seg fault. Why?
I got the answer to my prev problem, now i have another problem
i had .txt appended to my input file in my makefile. Now i have another problem. on command make it gives error.
0make: *** [a.out] Error 1
why?
Check the return value of fopen (well, check the return value of any call), it probably failed to open the file.
Because you do not specify the file in a command line arguments, or because the file you have specified there could not be opened for some reason. In that case, fopen returns NULL, and when you pass that NULL to feof it crashes the program. You have to check return values and error codes, especially when functions may return NULL.
The correct code may look something like this:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
FILE *fp;
if (argc < 2)
{
fprintf (stderr, "Please specify the file name.\n");
return EXIT_FAILURE;
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
perror ("Cannot open input file");
return EXIT_FAILURE;
}
printf ("%d\n", feof (fp));
return EXIT_SUCCESS;
}

Resources