Get the numbers at even positions from a file c++

145 views Asked by At

I wrote this program that is suppose to write some float numbers into a binary file, then read it and display only the numbers located on even positions. Now, i'm having a hard time figuring how to verify the position and to display what's in there. Could anyone please take a look over the code and tell me if there are any mistakes and how could i display something that way? I am also a bit confused about what exactly changes in the way ftell,fgetpos etc. act when they are used in binary files. Any answer would be most appreciated. Here is the code:

#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <string>

void read(float *p,int n);

void main()
{
    int n;
    float *p,nr;
    char name[20];
    fpos_t poz;
    printf("How many numbers would you like to have in your array?\n");
    scanf("%d",&n);
    if(!(p=(float*)malloc(n*sizeof(float))))
    {
        printf("Allocation unsuccessful!\n");
        return;
    }

    read(p,n);
    printf("Give the name of the binary file\n");
    scanf("%s",name);

    FILE *f;

    if((f=fopen(name,"w+b"))==NULL)
    {
        printf("The file could not be reached for writing\n");
        return;
    }
    for(int i=0; i<n; i++)
    {
        fwrite((p+i),sizeof(float),1,f);
        fclose(f);
    }

    if((f=fopen(name,"rb"))==NULL)
    {
        printf("The file could not be opened:\n");
        return;
    }

    printf("The numbers at positions 0,3,6 etc. are:\n");
    while ((fread(&nr,sizeof(float),1,f)==1))
    {
        poz=ftell(f);
        if((poz%2)==0)
        {
            printf("%f",nr);
        }
    }
    fclose(f);
    if(p)
        free(p);
    system("pause");
}

void read(float *p,int n)
{
    printf("Introduce the series of numbers:\n");
    for(int i=0; i<n; i++)
    {
        printf("Give the number %d:",i+1);
        scanf("%f",(p+i));
    }
}
1

There are 1 answers

2
AudioBubble On

Errors that I spotted on first look, might be more:

void main()

main should return int. The parameter list should be void. Your compiler takes your version probably nonetheless:

int main(void)

Next problem:

for(int i=0; i<n; i++)
{
    fwrite((p+i),sizeof(float),1,f);
    fclose(f);
}

You close the file after first iteration, then you try to write again. You should close the file when you are finished not after the first write:

for(int i=0; i<n; i++)
{
    fwrite((p+i),sizeof(float),1,f);
}
fclose(f);

This part does not do what you expect it to do:

poz=ftell(f);
if((poz%2)==0)
{
    printf("%f",nr);
}

ftell returns the position in bytes. Each float takes usually 4 bytes. So at this point the position will always be even. You want to check whether it is divided by 8 or better twice the size of float:

poz=ftell(f);
if(( poz % (2*sizeof(float)) == 0)
{
    printf("%f",nr);
}

However it would be much more practicable to use a counter instead:

for(int poz=0; fread(&nr,sizeof(float),1,f)==1; poz++)
{
    if((poz%2)==0)
    {
        printf("%f",nr);
    }
}

Also is this supposed to be C or C++? The code looks like C but you tagged it C++, also this include would be wrong in C: #include <string> should be #include <string.h>.

To use system("pause"); you need to #include <stdlib.h>. The line itself is not portable, you should handle this in a different way, see comments above.