Urgent!! Need help

This is a discussion on Urgent!! Need help within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm writing a program that works like a phone book Im having a lot of problems dealing with ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Urgent!! Need help

    Hi guys, I'm writing a program that works like a phone book
    Im having a lot of problems dealing with the fgets() function
    Everything works fine except the readSaved() function, the program always crashs on me when it reaches the fgets() function and I have no idea how to fix that, any suggestions?

    Code:
    int readSaved(struct Contact contact[])
    {
    	FILE *fp;
    	int max = 2*NAMELENGTH + 2*PHONELENGTH;
    	char *line,  *token;
    	int i = 0;
    
    	if((fp = fopen(FILENAME, "r")) != NULL)
    	{
    		fgets(line, max, fp);
    		//printf("Readed first line\n");
    		while((int)line != EOF)
    		{
    			//printf("Into loop\n");
    			//printf("%s\n", line);
    			token = strtok(line, " ");
    			contact[i].first= token;
    
    			token = strtok(NULL, " ");
    			contact[i].last = token;
    			
    			token = strtok(NULL, " ");
    			contact[i].home = token;
    			
    			token = strtok(NULL, "\n");
    			contact[i].cell = token;
    
    			i++;
    			fgets(line, max, fp);
    		}
    		fclose(fp);
    	}
    
    	return i;
    }
    Last edited by nareik9394; 03-18-2009 at 01:14 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    your line is not initialized pointer

    Code:
    char line[BUF_SIZE];
    while(fgets(line , sizeof line, fp))
    {
       /* process line */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    FWIW,

    From The Forum Guidelines

    Tips for Efficient and Successful Posting

    1. Don't use all caps.

    2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Again, the same thing is covered off in the excellent read How to ask questions the smart way.

    To quote:

    Don't flag your question as “Urgent”, even if it is for you

    That's your problem, not ours. Claiming urgency is very likely to be counter-productive: most hackers will simply delete such messages as rude and selfish attempts to elicit immediate and special attention.
    Anyhow.

    As vart points out, line is simply a pointer, nothing else. It points to a random place in memory. Either use malloc() to allocate a segment of memory, or simply replace it with char line[BUFSIZ];. Use of BUFSIZ is optional, just use some value that you think is large enough to store the content of one line; perhaps that nice little handy max variable you have there. (Once you make max const, of course.)

    Perhaps passing a string to readSaved() that is the filename would also help with flexibility later on, if you need to expand on this program, so that it can read from different "books".
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 09:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM

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