Program crashes on reading a line from a file

This is a discussion on Program crashes on reading a line from a file within the C Programming forums, part of the General Programming Boards category; Hi all, Im kind of newbie in C programming. I am trying to write a little application which compares 2 ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Program crashes on reading a line from a file

    Hi all,

    Im kind of newbie in C programming. I am trying to write a little application which compares 2 text files from the user, compare between them (line by line) and writes in a third file the differences between the 2 first.

    I have chosen to use fgets command to read the lines, but my program crashes. There are no compilation errors. It fails on the While

    Can you please take a look?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main ()
    {
    	FILE* firstHandle;
    	FILE* secondHandle;
    
    	char* firstFileLine=NULL;
    	char* secondFileLine=NULL;
    
    	char firstFile[15];
    	char secondFile[15];
    
    	//interface
    
    	printf ("\nPlease enter filename of first file: ");
    	scanf ("%s",firstFile);
    	printf ("\nPlease enter filename of second file: ");
    	scanf ("%s",secondFile);
    
    	// open both files
    	firstHandle=fopen (firstFile,"rt");
    	if (firstHandle==NULL)
    	{
    		printf ("\nCannot open firstfile: %s\n",firstFile);
    		exit(0);
    	}
    
    	secondHandle=fopen (secondFile,"rt");
    	if (secondHandle==NULL)
    	{
    		printf ("\nCannot open second file: %s\n",secondFile);
    		exit(0);
    	}
    	
    	//let's read the lines until eof of the first given file. 
    	while (fgets(firstFileLine, 50, firstHandle)!=NULL)
    	{
    		//Do the compare stuff here
    	}
    
    }

  2. #2
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    You don't appear to allocate any memory for firstFileLine, which would be your problem.
    In fact, you're trying to write to a null pointer in your fgets.

    Just use char firstFileLine[50] to allocate the memory.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Thanks, but...

    Thanks, Dweia, looks better now, however, why do I get 2 warnings?

    warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[50]'
    warning C4024: 'fgets' : different types for formal and actual parameter 1

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Guessing you have

    Code:
    char * firstFileLine[50];
    instead of

    Code:
    char firstFileLine[50];
    But I might not be psychic enough.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Thanks

    Thanks everybody...

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > void main ()
    int main people!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

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