-1

to write arrray and structures in a given text file and retrieving it to us in calculation can somebody help me with a sample code this is my code

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

int main()
{   FILE *file1;
    FILE *file2;//declaring two file pointers file1 to read,and file2 to read

    file1=fopen("data.txt","w"); //creating a new text file in writing mode
    int array1[12],i;//declared array1 which we will input the data in 


    for(i=0;i<=12;i++){
     scanf("%d",&array1[i]);//i am trying to use a for loop to input elements into arrar
      fprintf(file1,array1);//am trying to write the gotten values into file1
    }

    fclose(file1);

    file2=fopen("data.txt","r");//in read mode a file2 poninter
    int array2[12]; //another array of same number of elements to store the data read from file
    while(!feof(file2)){ //to get all the values inthe text file
    for(i=1;1<=12;i++){
    fscanf(file2,"%d",&array2[i]);//from here i am just confused
    printf("%d",array2[12]);
    }

    }
    fclose(file2);

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Once you've done that, then please edit your question to include information about the program you show, how it works, how it doesn't work, what input you have, what output you get and what you expected. – Some programmer dude Nov 06 '19 at 07:15
  • 1
    Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Nov 06 '19 at 07:16
  • Read the documentation of `printf` and `fprintf`. You also need to check if `fopen` returns `NULL` which means that the file could not be opened. And you need to indent your code properly so it is readable. – Jabberwocky Nov 06 '19 at 07:17

1 Answers1

0
  • array1 and array2are declared to hold 12 elements each i.e.elements numbered from 0 to 11(total 12).The for loops need to accommodate this fact.`
  • fprintf requires three arguments int fprintf(FILE *fptr, const char *str, ...);

Please take notice of the comments posted by @Some programmer dude and @Jabberwocky

Implementing the corrections your code should look like this......have placed comments where I made changes.....hope they are helpful....

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

int main()
{   
  FILE *file1;
  FILE *file2;

  file1=fopen("data.txt","w"); 
  int array1[12],i;

  if(file1 != NULL){
    for(i=0;i<12;i++){//i needs to be less than 12
      scanf("%d",&array1[i]);
      fprintf(file1,"%d\n",array1[i]);//fprintf needs three arguments
    }
  }

  fclose(file1);

  file2=fopen("data.txt","r");
  int array2[12]; 
  if(file2 != NULL){ //to get all the values inthe text file
    for(i=0;i<12;i++){//i needs to be less than 12
      fscanf(file2,"%d",&array2[i]);
      printf("%d\n",array2[i]);
    }
  }
  fclose(file2);
}

I will leave error handling (if the file fails to open) as an exercise for you.

Lily AB
  • 392
  • 2
  • 6