Thread: Segmentation fault (core dumped)? Where did I go wrong?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    25

    Segmentation fault (core dumped)? Where did I go wrong?

    When I run my program I get this...

    where am I going wrong?

    "lab7.c", line 4: warning: invalid white space character in directive
    "lab7.c", line 38: warning: implicit function declaration: strcpy
    "lab7.c", line 82: warning: newline not last character in file
    luna:~>a.out
    Segmentation fault (core dumped)

    Code:
    // the purpose of this lab is to read data from a file, capitalize the first letter of all the names 
    // in the file and rewrite the file
    // includes
    #include <stdio.h>
    struct employee
    {
    	char firstname[40];
    	char lastname[40];
    	int id;
    };
    typedef struct employee Employee;
    
    Employee e[3];
    
    void PrintEmployeeRecord();
    void SaveEmployeeRecord();
    //void Capitalize(const Employee e[]);
    
    int main ()
    {
    	Employee e[3];
    	
    	FILE *file;	
    	
    	file = fopen("employee.dat", "r");// open file
    	
    	int i = 0;
    	int c = 0;
    	int tID = 0;
    	char tFName[20];
    	char tLName[20];
    	
    	do
    	{  c = fscanf(file,"%d %s %s" , &tID,tFName,tLName);
    		if (c == 3)  // all three matched the format
    		{ 
    			e[i].id = tID;
    			strcpy(e[i].firstname, tFName);
    			strcpy(e[i].lastname,tLName);
    			i++ ;
    		}
    	} while ( c > EOF );  // end of file
    		fclose(file); //close file	
    	PrintEmployeeRecord();
    	
    	//Capitalize(e);
    	
    	PrintEmployeeRecord();
    	
    	SaveEmployeeRecord(e);
    }
    // end of main
    
    // the function saves the employee data to a file
    // the input is an array of 3 and the output is a file with the employee data
    void SaveEmployeeRecord(const Employee e[])
    {
    	int i;
    	FILE *file;	
    	
    	file = fopen("employee.dat", "w"); // open the file
    	
    	fprintf(file, "ID FIRSTNAME LASTNAME\n");  // header
    	
    	for (i=0; i<3; i++)
    	{
    		fprintf(file, "%d %s %s\n", e[i].id, e[i].firstname, e[i].lastname); // print the data
    	}	
    	fclose(file); // close the file
    }
    
    //purpose: Print Employee data
    //input: array of 3 employee data
    //output: 3 employee data printed to screen
    void PrintEmployeeRecord()
    {
    	int i;
    	for (i=0; i<3; i++);
    	{
    		printf("%d %s %s \n", e[i].id, e[i].firstname, e[i].lastname);
    	}
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Does employee.dat exist? You need to check the return value of fopen before trying to do anything with file. The program crashed for me, then when I created an empty employee.dat, it worked without crashing.

    I used a debugger to solve this, and it took me about 1 minute. You should also learn to use a debugger, as it is an invaluable tool and will help you track down these issues yourself. It allows you to step through the code line by line and see exactly what the value of each variable is and often takes you to the exact line that causes your crash.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    Quote Originally Posted by anduril462 View Post
    Does employee.dat exist? You need to check the return value of fopen before trying to do anything with file. The program crashed for me, then when I created an empty employee.dat, it worked without crashing.

    I used a debugger to solve this, and it took me about 1 minute. You should also learn to use a debugger, as it is an invaluable tool and will help you track down these issues yourself. It allows you to step through the code line by line and see exactly what the value of each variable is and often takes you to the exact line that causes your crash.
    I created a blank employee.dat file and this is what the compiler gave me

    "lab7.c", line 4: warning: invalid white space character in directive
    "lab7.c", line 38: warning: implicit function declaration: strcpy
    "lab7.c", line 82: warning: newline not last character in file
    luna:~>a.out
    0 ÿ¿ót
    0 ÿ¿ót

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dsured View Post
    I created a blank employee.dat file and this is what the compiler gave me

    "lab7.c", line 4: warning: invalid white space character in directive
    Retype line 4 of the file... #include <stdio.h>

    "lab7.c", line 38: warning: implicit function declaration: strcpy
    add... #include <string.h>


    "lab7.c", line 82: warning: newline not last character in file
    Go to the bottom of your source file and hit enter a couple of times.


    Also... see your previous thread on this same topic....
    Read A File From A Specific Line

    Note that I updated the suggested read process there.

    Quote Originally Posted by anduril462 View Post
    Does employee.dat exist? You need to check the return value of fopen before trying to do anything with file. The program crashed for me, then when I created an empty employee.dat, it worked without crashing.

    I used a debugger to solve this, and it took me about 1 minute. You should also learn to use a debugger, as it is an invaluable tool and will help you track down these issues yourself. It allows you to step through the code line by line and see exactly what the value of each variable is and often takes you to the exact line that causes your crash.
    FWIW... here's the file he's parsing (from his previous thread)

    Code:
    ID FIRSTNAME LASTNAME
    10 john doe
    20 mary jane
    
    30 jim smith

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    Quote Originally Posted by CommonTater View Post

    add... #include <string.h>

    Go to the bottom of your source file and hit enter a couple of times.


    [/code]
    Thank you for the tips so far.

    I added the string.h at the start,
    I pressed enter on the source file, employee.dat, and I still got the same errors


    "lab7.c", line 84: warning: newline not last character in file
    luna:~>a.out
    0 ÿ¿ót











    0 ÿ¿ót

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dsured View Post
    Thank you for the tips so far.
    I added the string.h at the start,
    I pressed enter on the source file, employee.dat, and I still got the same errors
    "lab7.c", line 84: warning: newline not last character in file
    Nope... not the data file... the file with your C source code in it.

    A C "translation unit" has to end with a newline (That is, there has to be at least one blank line at the end of the file).

    The problem may be that you have some junk at the end of the file... you need to check and clean it up as necessary.

    Also, it might have been better if you had continued your other thread... notice how we're transplanting information between them?
    Last edited by CommonTater; 03-17-2011 at 10:36 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    Quote Originally Posted by CommonTater View Post
    Nope... not the data file... the file with your C source code in it.
    ?
    Thanks!

    When I run the code, nothing comes up, I mean--when I input data and press enter, it moves to a new line... I have a feeling the code isn't processing anything.

    Code:
    luna:~>a.out
    1234
    bob
    smith
    1345
    adam
    jones
    1234
    will
    smith
    3432
    joe
    west
    1435
    jon
    xu

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look at your scanf in your do-while loop, then read the docs on scanf. It returns the number of elements successfully converted, or EOF if it's at the end of the file. You ask it to convert an integer, then two strings. Since there's no integer at the beginning of the first line of your data file, fscanf returns 0 and doesn't advance the file pointer. Since you have an infinite loop, I assume EOF is less than zero on your system, so c (which is 0) is greater than EOF and you repeat the loop, trying again to read an int from the first line of employee.dat, but the failed reads leave you right where you started if there's no conversion, so you keep doing the same thing over and over again.

    We went over this issue in your previous post (have you been paying attention -- Tater and I have, but it's not our thread). Before the loop, read the header row out of the data file with something like fgets, and discard it (do nothing with it).

    It's also a bad idea to use while (c > EOF) to control your loop. The standard doesn't guarantee that EOF is -1, or even less than zero. For all you know, it could be 12345, and your loop would fail instantly, only reading one line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 03:28 AM
  3. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  4. Segmentation fault
    By Buckshot in forum C++ Programming
    Replies: 14
    Last Post: 06-23-2005, 08:20 AM
  5. Core Dump / Segmentation Fault
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-08-2003, 08:24 PM