Thread: fprintf segfault

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    fprintf segfault

    I am trying to pratice c programming, i am trying to write a very simple program that would record a order and write it to a text file, problem is the program crashes when trying to use fprintf.

    I have changed the fprintf line to print on stdout and it works, but not for a file.

    Here is the code


    [C] order c program - Pastebin.com


    This is happening on both my desktop(linux), and my android phone(using c4droid)

    Please help me, much appreciated
    Last edited by supermariolinux; 05-26-2012 at 06:06 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Compile with some more warnings.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘ask_order’:
    bar.c:50: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    bar.c:55: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    bar.c:60: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    bar.c:64: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    bar.c:65: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    bar.c:66: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    bar.c:77: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char **’
    bar.c:77: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char **’
    bar.c:77: warning: format ‘%s’ expects type ‘char *’, but argument 5 has type ‘char **’
    Your struct is definitely not what you think it is.

    Nor do you check that the file was opened
    orderfile = fopen("order.txt", "w");
    Where is
    if ( orderfile == NULL )
    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
    May 2010
    Posts
    4,633
    Please post your code in your post between code tags.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int i = 0;
    
    typedef struct
    {
    
    	char *entree[80];
    	char *drink[80];
    	char *dessert[80];
    
    } order;
    
    void print_commands()
    {
    	printf("e: entree\n");
    	printf("d: drink\n");
    	printf("t: dessert\n");
    	printf("o: print complete order\n");
    	printf("c: print list of commands\n");
    	printf("w: write to text file\n");
    	printf("x: exit\n");
    }
    
    
    void ask_order()
    {
    
    
    	char choice;
    	order o;
    	FILE *orderfile = NULL;
    	orderfile = fopen("order.txt", "w");
    
    	printf("Welcome\nWhat Would you like to order?:\n");
    	printf("Commands:\n\n");
    	print_commands();
    	while (i == 0)
    	{
    		printf("\nENTER_OPTION>");
    		scanf("%c", &choice);
    		switch (choice)
    		{
    
    		case 'e':
    
    			printf("Enter your entree: ");
    			scanf("%s", o.entree);
    			break;
    
    		case 'd':
    			printf("Enter your drink: ");
    			scanf("%s", o.drink);
    			break;
    
    		case 't':
    			printf("Enter a dessert: ");
    			scanf("%s", o.dessert);
    			break;
    
    		case 'o':
    			printf("Entree: %s\n", o.entree);
    			printf("Drink: %s\n", o.drink);
    			printf("Dessert: %s\n", o.dessert);
    			break;
    
    		case 'c':
    			print_commands();
    			break;
    
    		case 'w':
    			printf("Saving to File...\n");
    			/* This isnt Working, it segfaults... */
    			fprintf(orderfile, "Entree: %s\nDrink: %s\nDessert: %s\n",
    					o.entree, o.drink, o.dessert);
    			fclose(orderfile);
    			break;
    
    		case 'x':
    
    			i += 1;
    			break;
    
    
    		}
    	}
    }
    
    
    int main()
    {
    	ask_order();
    	return 0;
    }
    First I recommend you increase your compiler warning level, here are the warnings I received when compiling your code:
    main.c||In function ‘ask_order’:|
    main.c|50|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|55|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|60|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|64|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|65|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|66|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|77|warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char **’|
    main.c|77|warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char **’|
    main.c|77|warning: format ‘%s’ expects type ‘char *’, but argument 5 has type ‘char **’|
    main.c|44|warning: switch missing default case|
    Where do you ever allocate memory for your pointers contained in your structure? Why do you even have those pointers?

    Jim

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    thank you guys, i got it working now,
    i got rid of the pointers and made them simple strings,
    interstingly at first i was able to fix it by changing the write mode to "a+"
    but i had a lot of warnings so i fixed the code, and now it works fine

    i feel more confident on how to write to files, thank you guys

    here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int i = 0;
    
    typedef struct {
        
        char entree[80];
        char drink[80];
        char dessert[80];
    
    } order;
    
    void print_commands()
    {
        printf("e: entree\n");
        printf("d: drink\n");
        printf("t: dessert\n");
        printf("o: print complete order\n");
        printf("c: print list of commands\n");
        printf("w: write to text file\n");
        printf("x: exit\n");
    }
    
    
    void ask_order()
    {
        
        order o;
        char choice;
        FILE *file;
         
        printf("Welcome\nWhat Would you like to order?:\n");
        printf("Commands:\n\n");
        print_commands();
        while (i == 0) {
        printf("\nENTER_OPTION>");
        scanf("%c", &choice);
        switch(choice) {
            
            case 'e':
    
                printf("Enter your entree: ");
                scanf("%s", o.entree);
                break;
        
            case 'd':
                printf("Enter your drink: ");
                scanf("%s", o.drink);
                break;
    
            case 't':
                printf("Enter a dessert: ");
                scanf("%s", o.dessert);
                break;
            
            case 'o':
                printf("Entree: %s\n", o.entree);
                printf("Drink: %s\n", o.drink);
                printf("Dessert: %s\n", o.dessert);
                break;
    
            case 'c':
                print_commands();
                break;
                
            case 'w':
                printf("Saving to File...\n");
                file = fopen("order.txt", "w");
                if (file == NULL)
                {
                    fprintf(stderr, "Error Creating File\n");
                }
            fprintf(file, "Entree: %s\nDrink: %s\nDessert: %s\n", o.entree, o.drink, o.dessert);
                fclose(file);
                break;
    
            case 'x':
                
                i += 1;
                break;
        
                
        }
    }
    }
        
    
    int main()
    {
        ask_order();
        return 0;
    }
    sorry for not using code tags before, , i was working with this on my smart phone, i was going to post it somewhere else, but it wasnt working so i iused pastebin and didnt feel like copying and pasting it all
    again, its kinda annoying on my smartphone, plus i was really tired and probally wasnt thinking to
    bright...
    Last edited by supermariolinux; 05-26-2012 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault... Why?
    By jonheese in forum C Programming
    Replies: 14
    Last Post: 11-21-2011, 06:02 PM
  2. another SEGFAULT
    By cerr in forum C Programming
    Replies: 8
    Last Post: 01-14-2010, 11:04 AM
  3. Segfault
    By Triumph in forum C Programming
    Replies: 5
    Last Post: 04-10-2009, 02:24 AM
  4. Sometimes segfault, sometimes not
    By jcafaro10 in forum C Programming
    Replies: 18
    Last Post: 04-07-2009, 06:53 PM
  5. Replies: 7
    Last Post: 03-26-2008, 03:21 AM

Tags for this Thread