Thread: Reading files by dividing into parts

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    4

    Reading files by dividing into parts

    HOW CAN READ THİS FİLES BY DİVİDİNG PARTS? İ WANT TO READ NUMBERS AND LOAD TO STRUCT BEFORE 'END' AND THEN AFTER HAT_ID LINES LOAD TO ANOTHER STRUCT.

    Reading files by dividing into parts-6ecae09b-6ee9-49ca-a593-af099736cdca-jpg
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    struct musteri{
        float id[10];
        float x1[10];
        float x2[10];
        float x3[10];
        float x4[10];
        float x5[10];
        
    }musteri;
    
    
    struct hat{
        float id[30];
        float x1[30];
        float x2[30];
        float x3[30];
        float x4[30];
    }hat;
    
    
    int main() {
    
    
    FILE *dosya;
    dosya=fopen("test.txt","r");
    int SIZE =256;
    int i=0;
    int j=0;
    char buff[SIZE];
    while ( fgets(buff,SIZE,dosya) ) {
      if ( buff[0] != 'E' ) {
        sscanf(buff," %f %f %f %f %f %f  ",&musteri.id[i],&musteri.x1[i],&musteri.x2[i],
                 &musteri.x3[i],&musteri.x4[i],&musteri.x5[i]);
                 i++;
      } 
      if (buff[0] != 'A') {
          sscanf(buff," %f %f %f %f %f   ",&hat.id[j],&hat.x1[j],&hat.x2[j],
                 &hat.x3[j],&hat.x4[j]);
                 j++;
      }
      
    }
    Last edited by nothingelse; 03-25-2020 at 07:27 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use two loops.
    Code:
    while ( fgets(buff,SIZE,dosya) ) {
      if ( buff[0] == 'E' ) break;
      sscanf(buff," %f %f %f %f %f %f  ",&musteri.id[i],&musteri.x1[i],&musteri.x2[i],
                 &musteri.x3[i],&musteri.x4[i],&musteri.x5[i]);
      i++;
    }
    
    fgets(buff,SIZE,dosya); // burn the header line
    
    while ( fgets(buff,SIZE,dosya) ) {
      if (buff[0] == 'A') break;
      sscanf(buff," %f %f %f %f %f   ",&hat.id[j],&hat.x1[j],&hat.x2[j],
                 &hat.x3[j],&hat.x4[j]);
      j++;
    }
    It's odd that you have the arrays inside your struct.
    Code:
    struct musteri{
        float id[10];
        float x1[10];
        float x2[10];
        float x3[10];
        float x4[10];
        float x5[10];
         
    }musteri;
    Most people would do this.
    Code:
    struct musteri{
        float id;
        float x1;
        float x2;
        float x3;
        float x4;
        float x5;
    } musteri[10];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    4
    thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-29-2012, 02:55 PM
  2. reading parts of a tab delimited file
    By AngKar in forum C Programming
    Replies: 8
    Last Post: 01-24-2006, 10:38 PM
  3. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  4. Reading from files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2002, 12:53 PM

Tags for this Thread