Thread: File between functions

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    File between functions

    Passing file between functions.

    In the main, there is a switch case. Option 1 executes Function named Main_Linear_Reg()
    Code:
    #include "Pearson.h"
    void Main_Linear_Reg();
    void Main_Linear_Reg()
    {
    int  n, i;
         printf("Enter Number Of Pairs:\t");
                scanf("%d", &n);
                struct Pearson Per[n];
                for(i=0; i<n; i++)
                {
                    printf("Pair: %d\n",i+1);
                    InDetails(&Per[i]);
                    printf("\n");
                }
                system("cls");
                for(i=0; i<n; i++)
                {
                    printf("Pair: %d\n",i+1);
                    DiDetails(Per[i]);
                    printf("\n");
                }
    FILE *fptr;
                fptr= fopen ("Data/t-test_Linear Regression.txt","w");
                for(i=0; i<n; i++)
                {
                    FileDetails(Per[i], &fptr);
                }
    The Functions used are from Pearson.h :

    Code:
    struct Pearson
    {
        float X;
        float Y;
    };
    
    
    void InDetails(struct Pearson *P);
    void DiDetails(struct Pearson P);
    void FileDetails(struct Pearson P, FILE *OutFile);
    void InDetails(struct Pearson *P)
    {
        printf("X:\t");
        scanf("%f", &P->X);
        printf("Y:\t");
        scanf("%f", &P->Y);
    }
    
    
    void DiDetails(struct Pearson P)
    {
        printf("X:\t%3.3f, Y:\t%3.3f",P.X,P.Y);
    }
    void FileDetails(struct Pearson P, FILE *OutFile)
    {
        fprintf(OutFile,"X:\t%3.3f, Y:\t%3.3f",P.X,P.Y);
    }
    Code:
    I get the following error:
    5 main.c In file included from main.c 
     MainLin.h In function `Main_Linear_Reg': 
    62  [Warning] passing arg 2 of `FileDetails' from incompatible pointer type
    What i want to do in this case is print all X and Y values of the struct to a file. If there is an alternative way, do let me know. Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by skg29 View Post
    Code:
    FILE *fptr;
                fptr= fopen ("Data/t-test_Linear Regression.txt","w");
                for(i=0; i<n; i++)
                {
                    FileDetails(Per[i], &fptr);
                }
    FILE *fptr, declares fptr as a pointer. FileDetails expects a pointer, you give the address of fptr itself by adding the ampersand, which is why you get a complaint about an incompatible pointer type. Using a plain fptr as an argument is how you declared your interface, so just pass fptr.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    I removed the & before fptr. But the file is still empty. The program does not write anything.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by skg29 View Post
    I removed the & before fptr. But the file is still empty. The program does not write anything.
    "still"? you never mentioned this as part of your problem, of course that would have been a consequence of the above but..

    Start by adding basic error checking after you open the file. Check that you were able to successfully open it, that is mandatory code that you should never leave out. Test if the file pointer is NULL after you attempt to open it, and use perror() to get a sensible error message out of it.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    I close everything, and opened again. rebuilt all and its working thnks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. define functions in another file
    By suryak in forum C Programming
    Replies: 14
    Last Post: 08-09-2011, 12:59 AM
  2. functions in .C file not recognized in .CPP file
    By tooKool4School6 in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2006, 10:30 AM
  3. file functions
    By Ash1981 in forum C Programming
    Replies: 6
    Last Post: 01-28-2006, 01:41 PM
  4. Unicode file I/O functions
    By MiamiCuse in forum C Programming
    Replies: 8
    Last Post: 11-04-2005, 01:09 PM
  5. File functions
    By clancyPC in forum C Programming
    Replies: 4
    Last Post: 05-08-2003, 02:49 PM