Thread: Text File Handling - seg fault

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Text File Handling - seg fault

    The code below to append a file is giving segmentation fault...
    It takes the input but doesnt print DONE..
    Plz help me out...

    Code:
    #include <stdio.h>
    
    main() {
    	char * ptr;	
    	printf("\nName the file: ");	
    	scanf("&#37;s",ptr);
    	FILE *f=fopen(ptr,"a");
    	if(f) {
    		fprintf(f,"The file is appended here\n");
    		fclose(f);
    	}
    	printf("\nDONE!\n");
    }
    Last edited by Salem; 08-24-2008 at 10:12 AM. Reason: Code is not written in italics

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    ptr isn't allocated any space, so when you read in your string it goes to a random place in memory.
    In your case, in this instance, it blows up.

    Allocate some space, or just use a char array.

    > FILE *f=fopen(ptr,"a");
    Mixing declarations and statements is not allowed in regular C

    Also, be specific about main returning int, and actually return something.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Thx for your reply...
    I've made the changes u suggested... but the prob still persists...

    the modified code is;

    Code:
    #include <stdio.h>
    
    int main() {
    	char name[20];
    	printf("\nName the file: ");	
    	scanf("&#37;s",name);
    	FILE *f;
    	f=fopen(name,"a");
    	if(f) {
    		fprintf(f,"The file is appended here\n");
    		fclose(f);
    	}
    	printf("\nDONE!\n");
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by Salem View Post
    > FILE *f=fopen(ptr,"a");
    Mixing declarations and statements is not allowed in regular C
    I guess that depends on what you mean by "regular C". C90 might not allow this, but I'm pretty sure C99 will.

    Also, I don't have any problems compiling and running your code. Are you sure you aren't trying to enter a filename that's too large for the buffer?

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    its working now
    thanks ppl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

Tags for this Thread