Thread: Stupid segfault

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Stupid segfault

    Hello, I'm currently doing a project that requires the user to run a program with a source file name and destination file name as parameters. The program then encrypts the text from the source file and puts it into the destination file using a simple substitution cipher. The program reads in a character from the source, puts it into a method that returns the encrypted character, and then is supposed to write the encrypted character into the file. When I try to write the character into the file, I get a segfault. I've tried just about everything to try and fix this. I have a feeling it's right under my nose. Anyways, here's my routine that encrypts everything:

    Code:
    void encrypt_file(CIPHER * cipher_key, FILE * src_file, FILE * dest_file){
    	char encrypted_char;
    
    	while((encrypted_char = get_encrypted_char(cipher_key, fgetc(src_file))) != EOF){
    		//printf("%c", encrypted_char);
    		fputc(encrypted_char, dest_file);
    	}
    	
    	fclose(src_file);
    	fclose(dest_file);
    }
    and here's the get_encrypted_char method:

    Code:
    char get_encrypted_char(CIPHER * cipher_key, char char_to_encrypt){
    	int i;
    	char encrypted_char;
    	encrypted_char = char_to_encrypt;
    	for (i = 0; i < CIPHER_LENGTH; i++){
    		if(char_to_encrypt == cipher_key->plaintext[i]){
    			encrypted_char = cipher_key->ciphertext[i];
    		}
    	}
    	return encrypted_char;
    
    }
    the CIPHER is a struct that encapsulates a plaintext character array along with a ciphertext character array such that their indices match. Any insight on this would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When the function returns with the encrypted char, can you print it and see that it's ok?

    It looks like you're trying to return a variable that has gone out of scope. Dodgy imo.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Quote Originally Posted by Adak View Post
    When the function returns with the encrypted char, can you print it and see that it's ok?

    It looks like you're trying to return a variable that has gone out of scope. Dodgy imo.
    Yes. The encrypted chars print fine.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose a silliness check -- if printing to the screen works, and the file doesn't, then is the file (a) opened (b) successfully (c) for output?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Quote Originally Posted by tabstop View Post
    I suppose a silliness check -- if printing to the screen works, and the file doesn't, then is the file (a) opened (b) successfully (c) for output?
    Yes, I have the following code to open the destination file:

    Code:
    dest_file = fopen(argv[2], "w+");
    		
    if(dest_file = NULL){
    	printf("Error opening destination file.\n");
    }
    
    else{
    	printf("Loaded destination file successfully.\n");
    }
    When I run the program, it prints that it opened the file successfully.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgetc returns int so you could distinguish EOF from other chars
    you are passing EOF as char into get_encrypted_char loosing this info, so how do you plan to exit the loop?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > if(dest_file = NULL)
    Yes, and == would be so much better....
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  2. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  3. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  4. Incredibly Stupid People
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-09-2003, 04:12 PM
  5. Stupid errors!
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-08-2002, 11:48 PM