Thread: segmentation violation

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    segmentation violation

    I just can't figure out where this error is coming from. I get a segment violation each time. Does anyone have any ideas. This is my first attempt at working with files, so any help would be nice. Thank you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void filename_accept(char fileName[]);
    int main()
    {
        FILE *myfile;
    	char fileName[40];
    	char fileData[1000];
    	int c;
    
    	filename_accept(fileName);
    
        myfile = fopen(fileName,"r");
    
    		while((c = fgetc(myfile)) != EOF)
    			{
    				int i=0;
    				fileData[i++] = c;
    			}
    
        fclose(myfile);
    	free(fileData);
    
        return 0;
    }
    
    
    void filename_accept(char fileName[])    /*Reads in file name*/
    {
    
    	printf("\nEnter file you would like to open:\n");
    	gets(fileName);
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First never use gets, use fgets(). See this link.

    Second you are using free on a pointer that you did not malloc. The fileData variable was statically allocated fileData[1000].

    Jim

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char filedata[1000];
    
    
    		while((c = fgetc(myfile)) != EOF)
    			{
    				int i=0;    
    				fileData[i++] = c;
    			}
    Filedata should be initialized to 0 ...
    Code:
    char filedata[1000] = {0};
    ... should work as long as you don't read more than 1000 characters.
    Also the int i = 0; should be executed only once above your while loop... unless you want to repeatedly write to filedata[0];

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    I'm still getting the segmentation error even with the suggested changes from CommonTater.


    Quote Originally Posted by CommonTater View Post
    Code:
    char filedata[1000];
    
    
    		while((c = fgetc(myfile)) != EOF)
    			{
    				int i=0;    
    				fileData[i++] = c;
    			}
    Filedata should be initialized to 0 ...
    Code:
    char filedata[1000] = {0};
    ... should work as long as you don't read more than 1000 characters.
    Also the int i = 0; should be executed only once above your while loop... unless you want to repeatedly write to filedata[0];

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > free(fileData);
    If you didn't call malloc, then don't call free.

    freeing something you didn't malloc is a bad thing.
    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. Access Violation(Segmentation Fault) error
    By nirvana619 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 08:43 AM
  2. An Access Violation Segmentation Fault. Need Help ASAP
    By darknessfalls in forum C Programming
    Replies: 2
    Last Post: 08-22-2010, 05:56 AM
  3. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  4. Replies: 3
    Last Post: 07-17-2008, 08:45 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM