Problem with file handling (pointers)

This is a discussion on Problem with file handling (pointers) within the C Programming forums, part of the General Programming Boards category; Hi! Its my first post on forum. I have problem - to illustrate it ill add some lines in c: ...

  1. #1
    hmk
    hmk is offline
    Registered User hmk's Avatar
    Join Date
    Sep 2008
    Posts
    2

    Problem with file handling (pointers)

    Hi! Its my first post on forum. I have problem - to illustrate it ill add some lines in c:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main (int argc, char *argv[])
    {
     FILE *out0,*out1,*out2,*out3,*out4;
     double alfa,omega,k0,c;
     char filtemp [128];
    
    sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lfsignature.dat",alfa,omega,k0,c);
     if(!(out0=fopen(filtemp,"w")))
     {
      printf("Can't open %s file!",filtemp);
      exit(0);
     }
    
     sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lfpotential.dat",alfa,omega,k0,c);
     if(!(out1=fopen(filtemp,"w")))
     {
      printf("Can't open %s file!",filtemp);
      exit(0);
     }
    
     sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lfderivative.dat",alfa,omega,k0,c);
     if(!(out2=fopen(filtemp,"w")))
     {
      printf("Can't open %s file!",filtemp);
      exit(0);
     }
     
     sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lfresults.dat",alfa,omega,k0,c);
     if(!(out3=fopen(filtemp,"w")))
     {
      printf("Can't open %s file!",filtemp);
      exit(0);
     }
     
     fprintf(out0,"test");
     fprintf(out1,"test");
     fprintf(out2,"test");
     fprintf(out3,"test");
     
     fclose(out0);
     fclose(out1);
     fclose(out2);
     fclose(out3);
    
     return 0;
    }
    Everything works without problem, but when I tried to short upper code, I wrote following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void filemaker(FILE *plik, char plik_nw[128], double alfa, double omega, double k0, double c)
    {
     char filtemp [128]; 
     sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lf%s.dat",alfa,omega,k0,c,plik_nw);
     if(!(plik=fopen(filtemp,"w")))
     {
      printf("Can't open %s file!",filtemp);
      exit(0);
     }
    }
    
    main (int argc, char *argv[])
    {
     FILE *out0,*out1,*out2,*out3,*out4;
     double alfa,omega,k0,c;
     char filtemp [128];
     alfa=omega=k0=c=1;
     filemaker(out0,"signature",alfa,omega,k0,c);
     filemaker(out1,"potential",alfa,omega,k0,c);
     filemaker(out2,"derivative",alfa,omega,k0,c);
     filemaker(out3,"results",alfa,omega,k0,c);   
     
     fprintf(out0,"test");
     fprintf(out1,"test");
     fprintf(out2,"test");
     fprintf(out3,"test");
     
     fclose(out0);
     fclose(out1);
     fclose(out2);
     fclose(out3);
    
     return 0;
    }


    Unfortunately after running program error occurs. I only know that it occurs on lines 26-29 - when program tries to write lines to files... debugger showed following information: "an access violation (segmentation fault) raised in your program".

    After googling I found that its all about pointers, but really I never before worked on them. Can anybody help me?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    I would suggest changing your function to:
    Code:
    FILE *filemaker(char plik_nw[128], double alfa, double omega, double k0, double c)
    {
        char filtemp [256]; 
        sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lf%s.dat",alfa,omega,k0,c,plik_nw);
        return fopen(filtemp,"w");
    }
    And calling it thusly:
    Code:
    FILE *out0 = filemaker("signature",alfa,omega,k0,c);
    if (!out0)
    {
        printf("Can't open %s file!",filtemp);
        return NULL;
    }

  3. #3
    hmk
    hmk is offline
    Registered User hmk's Avatar
    Join Date
    Sep 2008
    Posts
    2
    Someone on other forum gave me solution

    Code:
    void filemaker(FILE **plik, char *plik_nw, double alfa, double omega, double k0, double c)
    {
      char filtemp [128];
      sprintf(filtemp,"a%3.1lfW%3.1lfk%3.1lfBA%3.1lf%s.dat",alfa,omega,k0,c,plik_nw);
     if(!(*plik=fopen(filtemp,"w")))
     {
      printf("Can't open %s file!",filtemp);
      exit(0);
     }
    }
    Code:
      filemaker(&out0,"signature",alfa,omega,k0,c);

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Well, thanks for wasting my time.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Thumbs down

    Quote Originally Posted by rags_to_riches View Post
    Well, thanks for wasting my time.
    You don't have to be such a dick.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > You don't have to be such a dick.
    Your sentiment will change if you ever get good enough to help people, and reach the point of being totally ........ed off at seeing exactly the same question from the same poster in many different places.

    http://www.catb.org/~esr/faqs/smart-...ons.html#forum
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling Problem
    By Erkan in forum C Programming
    Replies: 4
    Last Post: 10-25-2005, 06:29 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Problem in file handling.
    By Abhid in forum C Programming
    Replies: 6
    Last Post: 02-18-2005, 11:11 PM
  4. File Handling problem
    By skyttkar in forum C++ Programming
    Replies: 1
    Last Post: 07-10-2003, 05:35 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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