0
#include <stdlib.h>
#include<string.h>
typedef struct monster {
    int id;
    char name[64];
    char element[64];
    int population;
    double weight;
} monster;

typedef struct {
    long long int compares;
    long long int copies;
} sort_results;

int main(void) {
  monster m[1000],n;
  sort_results s;
  FILE *file = NULL;
  file = fopen("10.txt", "r");
  if(file==NULL)
    printf("not read");
  int i=0;  
  while(!feof(file)) {
  fscanf(file,"%s %s %d %lf", n.name,n.element,&n.population,&n.weight);
  n.id = i+1;
   
  /*  this commented code gives error segmenatation fault*/
  // strcpy(m[i].element,n.element);
  // strcpy(m[i].name,n.name);
  // m[i].population = n.population;
  // m[i].weight = n.weight;
  // m[i].id = n.id;

  i++;
  
   // this is working fine , printing expected result
  printf("%s %s %d %lf\n",n.name,n.element,n.population, n.weight);
  }
  printf(" \n record count :%d",i);
}
File content

0ybOAtlTP KaRIvi05m 77 1.658986

Zr700nwzB BaFrl3BMN 85 3.019501

SJXVx8seR cMQZZBogi 179 0.237434

n5F30UhgA GPJucKtsW 243 1.082797

BnJBzm9dq NwZKlaEu3 179 5.048067

there is one new line gap in records 

1 . this is sample of 5 records , but there can be any number of records present in the file . The statement Fscanf is working fine..it is reading records as expected . But there is more than one record , hence i tried to store them into array , but it throw error as Segmant fault for the commented part of code.
2. Since there is not any fixed amount of records to be read , is their any way to achieve this as every time when it encounters new record in while loop , a new memory block is to be created , some concept of realloc and calloc in C . i tried to achieve this using static allocation of array , but i need this as dynamic

  • How many more such records do you have in file? is it more than `1000` ? – kiran Biradar Nov 06 '20 at 06:37
  • yes , i have 5 different files to be handled having varying length of record (1000,5000,10000) . But i tried this code only for 5 records. Still giving error – Deepak Sharma Nov 06 '20 at 06:38
  • 1
    `while (!(feof(...))` is **always** wrong, and you need to check the return value of `fscanf`! And if file was not opened successfully you still proceed to reading from it... – Antti Haapala -- Слава Україні Nov 06 '20 at 06:39
  • Here is the link to the relevant Stack Overflow question: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/12149471) – Andreas Wenzel Nov 06 '20 at 06:41
  • Hi the segment fault may be simply stack overflow...... ur situation is kind of like in database. The solution in simple is to create a bulk of memory (8kb usally), and use a list to connect each page, so you could add or delete page each time you would like. Usually there is a bitmap to indicate occupation of each block in page, if all cleared, the page is deleted. – Shore Nov 06 '20 at 06:48
  • OT: Why do you scan into `n` and the copy `n` to the array? Why don't you scan directly into an array element? – Support Ukraine Nov 06 '20 at 07:04
  • Check that `fscanf` returns 4 – Support Ukraine Nov 06 '20 at 07:05
  • OT: Don't just continue when `file` is NULL. Instead use `exit(1);` or similar – Support Ukraine Nov 06 '20 at 07:06
  • 1
    Using `%s` is very bad as it may overflow the buffer. Use `%63s` – Support Ukraine Nov 06 '20 at 07:08
  • For `printf` : `%lf` --> `%f` – Support Ukraine Nov 06 '20 at 07:09
  • OT: Large array with automatic storage duration (which typically means on a stack) shall in general be avoided. Here you have ~150.000 bytes so it's unlikely to be the real problem but still... use dynamic memory allocation (aka `malloc`, `calloc`, `realloc`) – Support Ukraine Nov 06 '20 at 07:12
  • 1
    @DeepakSharma I checked in my system. I did not get any Segmentation Fault. It is working. Your code did not include "stdio.h" header file. – Krishna Kanth Yenumula Nov 06 '20 at 07:49
  • Cannot reproduce. Could you [Edit] your question and show the verbatim output you get? OTOH if `fopen` fails for some reason, you just just print "not read", but you continue anyway and this may very well result in a segfault – Jabberwocky Nov 06 '20 at 07:52
  • yeah , i have fixed that using dynamic memory allocartion ..using realloc – Deepak Sharma Nov 06 '20 at 07:54

0 Answers0