[Segmentation Fault] fopen("filename","w")

This is a discussion on [Segmentation Fault] fopen("filename","w") within the C Programming forums, part of the General Programming Boards category; Hello everybody, I've a strange problem in a small application I'm working at. This program reads a file, elaborates some ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    [Segmentation Fault] fopen("filename","w")

    Hello everybody,

    I've a strange problem in a small application I'm working at.
    This program reads a file, elaborates some data then writes new data onto another file. The destination file is a fixed-name file, named "results": not a variable name.
    Yesterday I tested the program with a file - input files have a very simple structure - and everything went fine. Today I written a slightly bigger input file and...
    ...and it causes my program crash (Seg.fault) while "fopening" the results file.

    Here's the code in the writing function.
    Code:
    void processaDati(){
    		
    		
    		printf("Processing dati...\n\n");
    		FILE * file;
    		file=fopen ("risultati.psm","w");
    		int i;
    		printf("Let's begin...\n");
    		//Setup
    		fprintf(file, "def rAtomo = 1;\n");
    //Other fprintf instructions omitted...
    Maybe this piece of code is not very interesting, but I do not know what to post.

    The curious problem is that the parsing & calculating phase of the program works well (I mean, it produces no errors!) so I believe the problem is not the input-file structure. Any hints?
    Thanks a lot!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    If you're using malloc, and you've trashed the memory pool by overrunning something, then anything can happen.

    fopen usually calls malloc to allocate a FILE (which it returns to you as a pointer).
    If malloc fails because you trashed memory, then fopen will fail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Thank you,
    I dont know what you mean by "trashing".
    In the program I never malloc'd. I just declared arrays and a char*.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well you're doing something wrong.

    Just declaring something as char* ?

    This is wrong for example, it trashes memory
    char *p;
    scanf("%s",p);

    If you've got stuff like that, reading data into bare pointers, then the code is broken
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    You mean that if I made some mistakes elsewhere, these mistakes could cause the Seg-fault at fopening time?

    However, I'm posting more code:


    GLOBAL DECLARATIONS
    Code:
    typedef struct {
    int indice;
    float xpos;
    float ypos;
    
    } Atomo;
    
    typedef struct{
    int a;
    int b;
    int tipo;
    
    } Legame;
    
    
    Atomo atomi[100];
    Legame legami[100];
    int totAtomi,totLegami;
    The following code open the input-file (arg.#1) and parse it line by line.
    Then it checks the first char of the line, parse the line and fill a struct in the "atomi" and "legami" arrays. (notice: I removed some printf for clarity)

    FILE READING FUNCTION
    Code:
    void carica(char * nomefile){
    	FILE * file;
    	file=fopen (nomefile,"r");
    	totAtomi = 0;
    	totLegami =0;
    
    	char * linea;
    	
    	float x,y;
    
    	int ia,ib,tipo,ind;
    	
    	printf("APRO: %s \n",nomefile);
    
    	if (file==NULL) printf ("Error reading file \n");
    	else
    	{
    		do {
    			fgets(linea,100,file);
    
    			if (linea[0] == 'A'){
    				sscanf(linea,"A %d | %f %f",&ind,&x,&y);
    
    				atomi[totAtomi].indice = ind;
    				atomi[totAtomi].xpos = x;
    				atomi[totAtomi].ypos = y;
    				totAtomi++;
    				
    			}
    			if (linea[0] == 'L'){
    				sscanf(linea,"L %d %d | %d",&ia,&ib,&tipo);
    				
    				legami[totLegami].tipo = tipo;
    				legami[totLegami].a = ia;
    				legami[totLegami].b = ib;
    				totLegami++;
    				
    			}
    		 } while ((feof(file) != 1));
    		
    		
    	}
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > fgets(linea,100,file);
    Yeah, EXACTLY my point.

    char *linea;
    Where is this pointing - answer, the middle of nowhere.
    You write up to 100 bytes over someone else's memory.

    Try
    char linea[100];
    this has 100 bytes for your own personal use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    One more thing, replace your do while loop (which is wrong) with
    Code:
    while ( fgets(linea,100,file) != NULL ) {
      if (linea[0] == 'A' && totAtomi < 100 )
        // your sscanf stuff here
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Thanks a lot for your advice! It seems to work now, declaring the 100-char array.

    And yes, that was a ridiculous mistake... the totAtomi <100 :P

    Thank you very much, you saved me hours of wasted time.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21