Home > front end >  I am having trouble making a function that will open a file
I am having trouble making a function that will open a file

Time:12-25

void writeResultsToFile(FILE *f, char arrTypeLabels[3][7], char sortNames[4][11], float times[4][3], int n);
void writeResultsToFile(FILE *f, char arrTypeLabels[3][7], char sortNames[4][11], float times[4][3], int n)
{
f= fopen( "TIMES.txt", "wb");
if((f=fopen("TIMES.txt", "wb"))==NULL) {
  printf("Cannot open file.n");
}   
if (f == NULL)
{
  printf("Error! Could not open file\n");
  exit(-1);
}
fprintf(f,"Array size: %d\n================\n                     ",n);
int i,j;
for(i=0;i<3;i  )
{
  for(j=0;j<7;j  )
  {
    fwrite(arrTypeLabels[i]   j, sizeof (char), sizeof (char), f);
  }
}
for(i=0;i<4;i  )
{
  for(j=0;j<11;j  )
  {
    fwrite(sortNames[i]   j, sizeof (char), sizeof (char), f);
    if (j==9 && i==0)
    {
      int df;
      for(df=0;df<3;df  ) 
        fprintf(f,"    %f",times[0][df]);
    }
    if (j==9 && i==1)
    {
      int df;
      for(df=0;df<3;df  ) 
        fprintf(f,"    %f",times[1][df]);
    }
    if (j==5 && i==2)
    {
      int df;
      fprintf(f,"       ");
      for(df=0;df<3;df  ) 
        fprintf(f,"    %f",times[2][df]);
    }
    if (j==5 && i==3)
    {
      int df;
      fprintf(f,"       ");
      for(df=0;df<3;df  ) 
        fprintf(f,"   %f",times[3][df]);
    }
  }
}
fclose(f);
}

//This is how i call it inside the main
writeResultsToFile(FILE *f,arrTypeLabels[3][7],sortNames[4][11],times[4][3],n);

the code inside it is right i tested it without the funtion, it works , i just dont know how to call it as a funtion if anybody can help.

Expected expression before FILE, and conflicted type errors..............................................

CodePudding user response:

FILE *f;
writeResultsToFile(f,arrTypeLabels,sortNames,times,n);

i am just writting this

CodePudding user response:

#include <stdlib.h>
#include <wchar.h>
#include <stdio.h>

int writeResultsToFile(FILE *f, char arrTypeLabels[3][7], char sortNames[4][11], float times[4][3], int n);

int writeResultsToFile(FILE *f, char arrTypeLabels[3][7], char sortNames[4][11], float times[4][3], int n)
{
    f = fopen("TIMES.txt", "wb");
    if ((f = fopen("TIMES.txt", "wb")) == NULL)
    {
        printf("Cannot open file.n");
    }
    if (f == NULL)
    {
        printf("Error! Could not open file\n");
        exit(-1);
    }
    fprintf(f, "Array size: %d\n================\n                     ", n);
    int i, j;
    for (i = 0; i < 3; i  )
    {
        for (j = 0; j < 7; j  )
        {
            fwrite(arrTypeLabels[i]   j, sizeof (char), sizeof (char), f);
        }
    }
    for (i = 0; i < 4; i  )
    {
        for (j = 0; j < 11; j  )
        {
            fwrite(sortNames[i]   j, sizeof (char), sizeof (char), f);
            if (j == 9 && i == 0)
            {
                int df;
                for (df = 0; df < 3; df  )
                    fprintf(f, "    %f", times[0][df]);
            }
            if (j == 9 && i == 1)
            {
                int df;
                for (df = 0; df < 3; df  )
                    fprintf(f, "    %f", times[1][df]);
            }
            if (j == 5 && i == 2)
            {
                int df;
                fprintf(f, "       ");
                for (df = 0; df < 3; df  )
                    fprintf(f, "    %f", times[2][df]);
            }
            if (j == 5 && i == 3)
            {
                int df;
                fprintf(f, "       ");
                for (df = 0; df < 3; df  )
                    fprintf(f, "   %f", times[3][df]);
            }
        }
    }
    fclose(f);
}

int main(void)
{
    //assign theses variables before calling your function
    char arrTypeLabels[3][7];
    char sortNames[4][11];
    float times[4][3];
    int n;
    //--------------------------

    FILE * f; //no need to assign this variable
    writeResultsToFile(f, arrTypeLabels, sortNames, times, n); //call your function like that
    return 0;
}
  •  Tags:  
  • c
  • Related