Thread: something wrong with my frpintf

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    something wrong with my frpintf

    Code:
    void printtofile(char **wrdhold, char *out)
    
    {
    
        printf("%s\n", wrdhold[5]);
    
    
        FILE *f_out=fopen(out, "w");
    
        if(f_out==NULL) {
        printf("Error: can't create file for writing.\n");
        }
    
        fprintf(f_out, "%c", wrdhold[0]);
    
        fclose(f_out);
    
    
    }
    wrdhold is a 2d char array, I get a seg fault trying to write it to a file.

    Am I not allowed to use 2d arrays for fprintf?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    opps,

    should be %s...but still get same error.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How does the caller declare the parameter you receive as wrdhold ?
    How does the caller initialise it?

    The problem is entirely with the caller. If they pass a garbage pointer, then this is where it fails.
    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.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you fail to open file - you still using the nul-pointer in fprintf and fclose
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    I've done this with your function and is working just fine:

    Code:
    #include <stdio.h>
    
    void printtofile(char **wrdhold, char *out)
    
    {
    
        printf("%s\n", wrdhold[5]);
    
    
        FILE *f_out=fopen(out, "w");
    
        if(f_out==NULL) {
        printf("Error: can't create file for writing.\n");
        }
    
        fprintf(f_out, "%s", wrdhold[0]);
    
        fclose(f_out);
    
    }
    
    int main()
    {
      char *wrdhold[10], *out = "text.txt"; 
      wrdhold[0] = "ana";
      wrdhold[5] = "ddsd";
    
      printtofile(wrdhold, out);
    
      return 0;
    }
    First of all wrdhold is a pointer to a pointer to a char... maybe this is your pb.. maybe your not using it like that or maybe u don't initialized your wrdhold so that your passing garbage data.
    I have stopped reading Stephen King novels. Now I just read C code instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM