0

I have a code which should read an array, write it to a binary and to a text file, then print the files. However, the fprintf function returns an error and i have no idea why. This is my code :

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

int main()
{ 
    FILE *f,*b;

    if (f=fopen("fis.txt","w+")==NULL) { 
        printf ("Error\n");
        exit(1);
    }
    if(b=fopen("binar.txt","w+b")==NULL) { 
        printf ("Error\n");
        exit(1);
    }

    float *v; int n;
    char s[1000];
    scanf("%d",&n);
    v=malloc(n*sizeof(float)); 

    int x,y;
    for (int i=0;i<=n;i++) { 
        scanf("%f",&v[i]);
        printf("%f",v[i]);

        x=fprintf(f,"%f",v[i]); 
        if (x<0) printf("err\n");

        y=fprintf(b,"%f",v[i]); 
        if (y<0) printf ("err2\n");
    }

    fgets(s,sizeof(s),f); 
    puts(s); 
    printf("\n");

    fgets(s,sizeof(s),b);
    puts(s);
    printf("\n");

    free(v);
    fclose(f);
    fclose(b);
}
12
  • Off by 1 error in your loop. Trying to scanf into v[0], v[1], ... v[n-1], v[n]; but v[n] does not exist. Commented Dec 27, 2018 at 21:22
  • int i=0;i<=n;i++ should beint i=0;i<n;i++ writing 1 value out of array Commented Dec 27, 2018 at 21:22
  • Try perror to print the error reason (based on the value of errno); most standard library functions set errno on failure. Commented Dec 27, 2018 at 21:25
  • Thank you. I modified but it still doesnt work Commented Dec 27, 2018 at 21:26
  • 1
    I strongly recommend you compile with all warnings enabled, and treat any warnings as errors. I would expect most compilers to warn about the assignment bug. Admittedly the warning message may be a bit obscure, probably something like "conversion from integer to pointer", but it would at least point you to a line where something is wrong. And in that case the first thing to do would be to get rid of anything you are not absolutely sure about, which would hopefully include chaining multiple operators without parentheses. Commented Dec 27, 2018 at 21:36

1 Answer 1

3

The main issue is how you're opening the files:

if (f=fopen("fis.txt","w+")==NULL) { 

The equality operator == has higher precedence than the assignment operator =. So first the result of fopen is compared to NULL, then the result of that comparison, i.e. either 0 or 1, is assigned to f. So f doesn't point to a valid location, and that is why your fprintf calls fail. If you have warnings turned up on your compiler, it should have warned about assigning an integer to a pointer.

Add parenthesis to get the proper ordering:

if ((f=fopen("fis.txt","w+"))==NULL) {

And:

if ((b=fopen("binar.txt","w+b"))==NULL) { 

Also, your loop condition is incorrect:

for (int i=0;i<=n;i++) { 

The array v has n elements, meaning its indexes go from 0 to n-1, but you loop from 0 to n. Change the loop condition to account for this:

for (int i=0;i<n;i++) { 

You also need to call rewind on each file descriptor before reading back from them so that you can read what you just wrote:

rewind(f);
fgets(s,sizeof(s),f); 
puts(s); 
printf("\n");

rewind(b);
fgets(s,sizeof(s),b);
puts(s);
printf("\n");
Sign up to request clarification or add additional context in comments.

3 Comments

agree, this is what I said about the fopen, it is very strange she doesn't see she even cannot open the input file
I did not get any error and i overlooked the parenthesis . Thank you.
@MaryPoppins Glad I could help. Feel free to accept this answer if you found it useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.