It seg faults right away

This is a discussion on It seg faults right away within the C Programming forums, part of the General Programming Boards category; I'm trying to read in text from a file exclusive or it against a random number, and output that to ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    22

    It seg faults right away

    I'm trying to read in text from a file exclusive or it against a random number, and output that to another file. I thought this should work, but it seg faults as soon as I run my program. Any help would be great thanks.

    Code:
    /*Implement a stream cipher*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <getopt.h>
    
    
    
    int main(int argc, char *argv[])
    {
    
    
    	/*Declare Variables*/
    	int randNum;
    	int seed;
    	int s;
    	int i = 0;
    	char stream[131];
    	char fileName1[11];
    	char fileName2[11];
    
    
    	/*opening files*/
    	FILE *infp;
    	FILE *outfp;
    	
    	infp = fopen(argv[1], "r");
    	outfp = fopen(argv[2], "rw");
    	
    	/*Test to ensure both files opened successfully*/
    	if(!infp)
    		printf("File could not be opened ");
    	else if(!outfp)
    		printf("File could not be opened ");	
    
    
    		/*set seed value*/	
    		seed = atoi(argv[3]);	
    		srand(seed);		
    		
    		stream[i] = fgetc(infp);
    		++i;
    		printf (" line 91 \n ");
    		/*atoi converts to integer*/
    		for (i; (stream[i] != EOF); ++i)
    		{
    			s = atoi(stream[i]); /*line 95*/
    			s = s ^ rand();
    			putc(s, outfp);
    			stream[i] = fgetc(infp); 	
    		}
    
    	/*close the files*/
    	 fclose(infp);
    	 fclose(outfp);
    	
    	return 0;
    	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Try solving it yourself first, e.g., use a debugger or the poor man's method of commenting out code to see what works and what does not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    line 95 is the culprit!
    you are not passing a pointer to atoi here

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you compile with warnings enabled, then you would be told that you are not passing a pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You might want to check whether argc is big enough before attempting to access the argv[i] - unless this program gets called from a script and you know you don't need to check, but even then, it's trivial to do.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    22
    Thanks for the responses, so I'm pretty sure its line 95, so I did try to change it to a pointer.
    Code:
                *ptr = stream[i];
    	     s = atoi(&ptr);
    that didn't fix it, I thought that was right, could it be somewhere else?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Here's a tidied up version that "works" with notes afterwards:

    Code:
    /* Implement a stream cipher */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
      This program expects three commandline parameters:
      <input file> <output file> <seed value>
    */
    int main(int argc, char *argv[]) {
      FILE *infp;
      FILE *outfp;
      char c;
    
      if (argc != 4) {
        fputs("Usage: prog <input file> <output file> <seed value>\n", stderr);
        return EXIT_FAILURE;
      }
    	
      infp = fopen(argv[1], "r");
      outfp = fopen(argv[2], "w");
    	
      /* Test to ensure both files opened successfully */
      if (!infp) {
        fputs("Input file could not be opened!\n", stderr);
        return EXIT_FAILURE;
      }
    
      if (!outfp) {
        fputs("Output file could not be opened!\n", stderr);
        return EXIT_FAILURE;
      }
    
      /* The third commandline parameter should be a seed value */	
      srand(atoi(argv[3]));
    		
      while(!feof(infp)) {
        c = fgetc(infp);
        putc((int)c ^ rand(), outfp);
      }
    
      /* close the files */
      fclose(infp);
      fclose(outfp);
    	
      return EXIT_SUCCESS;
    }
    1) As robatino pointed out, you should probably check to see whether the proper number of arguments have been passed to you. Notice I added this.

    2) If your FILE pointers weren't created successfully, you shouldn't let the program continue as it simply wont work. Notice I added "return" to your error handling to halt the program. Furthermore, you were opening the output file using file mode "rw". This is not a valid mode string. Look here for the documentation on fopen() http://www.cplusplus.com/reference/c...dio/fopen.html

    3) There's no need to read characters into a buffer like you were doing. If you are going to read each character in one at a time and XOR them individually, just store them in a char, XOR it, and then write it to the output file.

    4) You do NOT want to use atoi() to convert the chars from the input files into numbers. Instead, you want to cast them to integers (since they are really just 8-bit integers anyway). These are two completely different things. Here's an example of why:
    What you were doing: atoi("8") which equals int 8
    What you should do: (int)'8' which equals int 56 (the ASCII encoding of '8')
    (Plus, what would happen if you read in an alphabetic character? atoi("a") == ???)

    5) The version of the program I wrote "works", but it will fail to do what you expect. I say "works" because it won't segfault, however it will also fail for many input files on most machines. The reason why is because you are opening the files in text mode, but you are performing operations that should really be done in binary mode. For example, on a Windows machine, you will probably read the EOF character somewhere if your input file contains random binary data. In text mode this will cause the EOF flag to be set and your program will terminate without reading in the whole file. To fix this open the files in binary mode and use fread(). In text mode you will also encounter all sorts of other odd errors.

    6) I also removed certain unnecessary parts and rearranged things. I apologize but every coder has their own style
    Last edited by arpsmack; 01-31-2008 at 09:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 09:29 PM
  2. Seg faults. structs. pointers.
    By ominub in forum C Programming
    Replies: 12
    Last Post: 05-03-2009, 07:04 PM
  3. Help with arrays and seg faults
    By IdioticCreation in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2008, 03:16 PM
  4. sorted linked list...why seg faults!
    By S15_88 in forum C Programming
    Replies: 4
    Last Post: 03-24-2008, 11:30 AM
  5. prog runs on 64bit - seg faults on 32bit
    By hollie in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 12:59 AM

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