Thread: Reading files processing

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

    Reading files processing

    I tried to reading this file and write in struct musteri until 'E'.But this doesnt work correctly can you help me?



    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;
    
    
    int main() {
    	char harf;
    	int i=0;
    	int ch;
    	FILE *d;
    	d=fopen("d.txt","r");
    	
    while ((harf=fgetc(d)) != 'E'){
    
    
    	fscanf(d," %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++;
    	
    	
    }
    ch=i;
    i=0;
    
    
    for(i=0;i<ch;i++){
    	
    	printf("%f %f %f %f %f %f",musteri.id[i],musteri.x1[i],musteri.x2[i],musteri.x3[i],musteri.x4[i],musteri.x5[i]);
    	printf("\n\n\n");
    	
    }	
    return 0;
    }
    Reading files processing-1fed5320-3cd9-473a-8e76-cc059b078dbf-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while ((harf=fgetc(d)) != 'E')
    Well this eats the first character of the 1st line, just ruining your attempt to scanf the rest of the line.

    Normally I would suggest
    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,d) ) {
      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]);
      } else {
        break; // all done
      }
    }
    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

    thanks a lot.

    thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  2. Reading and processing input from keyboard
    By papagaio in forum C Programming
    Replies: 1
    Last Post: 11-12-2008, 03:59 PM
  3. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 11:33 AM
  4. Processing binary or plaintext files
    By Jags in forum C Programming
    Replies: 12
    Last Post: 08-04-2006, 02:35 PM
  5. opening/processing zip files
    By mako in forum C Programming
    Replies: 3
    Last Post: 02-20-2006, 01:59 PM

Tags for this Thread