something wrong with my frpintf

This is a discussion on something wrong with my frpintf within the C Programming forums, part of the General Programming Boards category; 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"); ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if you fail to open file - you still using the nul-pointer in fprintf and fclose
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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