-1

Could you help me with the creation of a text file as right now the *fp pointer to the file is returning NULL to the function fopen ?

Using the library errno.h and extern int errno I get "Value of errno: 22".

if (!fp)perror("fopen") gives me "Error opening file: Invalid argument".

In my main function I enter the name of the file:

void main()
{
    float **x;
    int i,j;
    int l,c;
    char name_file[30];
    FILE *res;

    /* some lines omitted */

    printf("\nEnter the name of the file =>"); 
    fflush (stdin);
    fgets(name_file,30,stdin);
    printf("Name of file : %s", name_file);

    res=creation(name_file,l,c,x);
    printf("\nThe created file\n");
    readfile(res,name_file);
}

The function to create the text file:

FILE *creation(char *f_name,int l, int c, float **a) // l rows - c colums - a array 
{   FILE *fp;
    int i,j;

    fp = fopen(f_name,"wt+"); // create for writing and reading

    fflush(stdin);

/* The pointer to the file is NULL: */

    if (!fp)perror("fopen"); // it's returning Invalid argument
    printf("%d\n",fp); //0 ??

    if(fp==NULL) { printf("File could not be created!\n"); exit(1); }

    fflush(stdin);
    for(i=0;i<l;i++)
  {
     for(j=0;j<c;j++)
     {
        fprintf(fp,"%3.2f",a[i][j]); // enter every score of the array in the text file
     }
     fprintf(fp,"\n");
  }

    return fp;
}

Function to read the file and check if it is correct:

**void readfile(FILE *fp,char *f_name)**
{
  float a;
  rewind(fp);
  if(fp==NULL) { printf("File %s could not open\n",f_name); exit(1); }
  while(fscanf(fp,"%3.2f",&a)!= EOF)
    printf("\n%3.2f",a);
  fclose(fp);
}
Jongware
  • 21,058
  • 8
  • 43
  • 86
Alex
  • 13
  • 3
  • 4
    Welcome to Stack Overflow. Sadly there are several sins in your code. At this point I'd like to encourage you to read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). If `fopen` returns `NULL`, it's most likely because the file doesn't exist or you don't have reading permissions on the file. `errno` is set in this case, use it. – Pablo Jan 09 '18 at 23:46
  • 1
    Unless the file name is too long, you've got a newline at the end of it. That is not going to lead to happiness. Remove the newline returned by `fgets()`. On Unix-like systems, newlines are allowed in file names, but Windows may prevent such names being created. Also, `t` is not allowed (officially) in `fopen()` open modes on Unix-like systems. That's a Windows-ism. You can specify `b` in the open mode; that means 'binary', but it is a no-op on Unix (it matters on Windows). – Jonathan Leffler Jan 10 '18 at 01:14

1 Answers1

1

There are quite a few wrong things your code.

1. The correct signatures of main are

  • int main(void);
  • int main(int argc, char **argv)
  • int main(int argc, char *argv[])

See What are the valid signatures for C's main() function?

2. The behaviour of fflush(stdin) is undefined. See Using fflush(stdin). fflush works with output buffers, it tells the OS that is should write the buffered content. stdin is an input buffer, flushing makes no sense.

3. Use fgets like this:

char name_file[30];
fgets(name_file, sizeof name_file, stdin);

It's more robust using sizeof name_file because this will give you always the correct size. If you later change the declaration of name_file to an char array with less than 30 spaces, but forget to change the size parameter in fgets, you might end up with a buffer overflow.

4. You are passing to creation the uninitialized pointer p that is pointing to nowhere. In said function you cannot read nor write with the pointer a. You need to allocate memory prior to the call of creation. At least judging from the code you posted.

5. fgets preserves the newline ('\n') character, so name_file is containing the newline character. I really don't know if newline is allowed in file names. I did a google search but found conflicting answers. I don't think that you want to have newlines in your file names, anyway. It's best to remove it before passing it to fopen (which might be the reason for the error 22):

char name_file[30];
fgets(name_file, sizeof name_file, stdin);
int len = strlen(name_file);
if(name_file[len - 1] == '\n')
    name_file[len - 1] = 0;
Pablo
  • 12,254
  • 3
  • 31
  • 52