-1

test_system.txt

UPLOADER_ID X1  Y1  X2  Y2  
1   1.00    1.00            
END                 
CUSTOMER_ID TIP X1  Y1  X2  Y2
1   3   2.00    1.00    2.00    1.00
2   2   3.00    1.00    3.00    1.00
3   1   4.00    1.00    4.00    1.00
4   1   5.00    1.00    5.00    1.00
5   2   6.00    1.00    6.00    1.00
6   2   7.00    1.00    7.00    1.00
7   1   8.00    1.00    8.00    1.00
8   1   9.00    1.00    9.00    1.00
9   2   10.00   1.00    10.00   1.00
10  1   11.00   1.00    11.00   1.00
11  1   12.00   1.00    12.00   1.00
END                 
LINE_ID X1  Y1  X2  Y2  
1   1.00    1.00    2.00    1.00    
2   2.00    1.00    3.00    1.00    
3   3.00    1.00    4.00    1.00    
4   4.00    1.00    5.00    1.00    
5   5.00    1.00    6.00    1.00    
6   6.00    1.00    7.00    1.00    
7   7.00    1.00    8.00    1.00    
8   8.00    1.00    9.00    1.00    
9   9.00    1.00    10.00   1.00    
10  10.00   1.00    11.00   1.00    
11  11.00   1.00    12.00   1.00    
END 

This was my text file which I have to read .

#include "stdlib.h"
#include "string.h"
#include "math.h"

typedef struct {
    int size,id,tip;
    float x1,x2,y1,y2;
}blocks;

int main() {
    blocks *block1 , *block2 , *block3;
    char line[60];
    int blcNum=1, sizer=0, temp;
    block1 = malloc( sizeof(blocks) );
    block2 = malloc( sizeof(blocks) );
    block3 = malloc( sizeof(blocks) );
    FILE *fp;
    fp = fopen("test_system.txt","r");
    while(!feof(fp)){
        fgets(line,60,fp);
            if ( strstr(line,"\0") != NULL && strstr(line,"ID") == NULL && strstr(line,"END") == NULL ) {
            sizer++;
            printf(line); 
            }
            else if ( strstr(line,"END") != NULL ){
                if ( blcNum == 1){
                    block1->size = sizer,blcNum++,sizer = 0;
                }
                else if ( blcNum == 2){
                    block2->size = sizer,blcNum++,sizer = 0;
                }
                else if ( blcNum == 3){
                    block3->size = sizer,blcNum++,sizer = 0;
                }
            }
    };
    block1 = realloc(block1 , block1->size * sizeof(blocks));
    block2 = realloc(block2 , block2->size * sizeof(blocks));
    block3 = realloc(block3 , block3->size * sizeof(blocks));
    fclose(fp);
    line == NULL,blcNum = 1;
    fp = fopen("test_system.txt","r");
    for ( temp =0 ; temp < block1->size ; ++temp)
    {
        while(!feof(fp)){
            fgets(line,60,fp);
            if (  strstr(line,"ID") == NULL && strstr(line,"END") == NULL && strstr(line,"\0") != NULL)
            {
                if ( blcNum == 1){
                    fscanf(fp,"%d %.2f %.2f",&(block1+temp)->id,&(block1+temp)->x1,&(block1+temp)->y1);
                }
                else if ( blcNum == 2){
                    fscanf(fp,"%d %d %.2f %.2f %.2f %.2f",&(block2+temp)->id,&(block2+temp)->tip,
                                                          &(block2+temp)->x1,&(block2+temp)->y1,
                                                          &(block2+temp)->x2,&(block2+temp)->y2);
                }
                else if ( blcNum == 3){
                    fscanf(fp,"%d %.2f %.2f %.2f %.2f",&(block3+temp)->id,&(block3+temp)->x1,
                                                       &(block3+temp)->y1,&(block3+temp)->x2,
                                                                          &(block3+temp)->y2);
                }
            }
            else if ( strstr(line,"END") != NULL ){
                    blcNum++;
            }
        }
    }
   fclose(fp);
   printf("\nUploader's information : \n");
   for (temp =0; temp < block1->size ; ++temp)
   {
    printf(" id : %d , X1 : %.2f , Y1 : %.2f  \n",(block1+temp)->id,(block1+temp)->x1,(block1+temp)->y1);
   }
   printf("\ncustomer's information : \n");
   for (temp =0; temp < block2->size ; ++temp)
   {
    printf(" id : %d , tip : %d , X1 : %.2f , Y1 : %.2f , X2 : %.2f , Y2 : %.2f \n",(block2+temp)->id,(block2+temp)->tip,(block2+temp)->x1,
                                                                                    (block2+temp)->y1,(block2+temp)->x2,(block2+temp)->y2);
   }
   printf("\nLine's information : \n");
   for (temp =0; temp < block3->size ; ++temp)
   {
    printf(" id : %d , X1 : %.2f , Y1 : %.2f , X2 : %.2f , Y2 : %.2f \n",(block2+temp)->id,(block2+temp)->x1,
                                                                         (block2+temp)->y1,(block2+temp)->x2,(block2+temp)->y2);
   }
    printf("\n1 . block's size : %d ",block1->size);
    printf("\n2 . block's size : %d ",block2->size);
    printf("\n3 . block's size : %d ",block3->size);
    free(block1);
    free(block2);
    free(block3);
    return 0;
} 

also this is my source code to get values from text file and put them in struct . Have done everyting that I know but can't access back what I put in struct truely . I'm newbie at here and C so someone would give a hand ?

There are 3 blocks in text source named uploader , customer and line which has id ,x1,x2,y1,y2 coordinate variables also customer has one more variable that named type .

I calculated their sizes with endlines doing ignore lines includes strings like "END" . Then allocated struct objects with their sizes before getting values from text source line by line .

So so how can I access them truely ?

Pip Doun
  • 1
  • 1
  • 1
    Please read [why !feof() is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – unwind Mar 23 '20 at 11:42
  • Welcome to SO. What is this supposed to do? `strstr(line,"\0") != NULL` You might want to consult the [man page](https://www.manpagez.com/man/3/strstr/): "If s2 is an empty string, s1 is returned". – Gerhardh Mar 23 '20 at 11:59
  • 1
    Please don't get used to writing stuff like that. `block3->size = sizer,blcNum++,sizer = 0;` There is no need to squeeze that into 1 single statement. – Gerhardh Mar 23 '20 at 12:00
  • strstr(line,"\0") != NULL , Im counting size of blocks with this to see line[60] "END" string , size will increase by itself. – Pip Doun Mar 23 '20 at 12:51

4 Answers4

0

@KamilCuk @Hubert Nguyen

I remaked and shortened my source code as you guys adviced me as

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct {
    int size;
    int id;
    int tip;

    float x1;
    float x2;
    float y1;
    float y2;
}blocks;

int main() {

    blocks *block1 , *block2 , *block3;
    char line[60];
    int blcNum=1;
    int sizer=0;
    int temp;
    block1 = malloc( sizeof(blocks) );
    block2 = malloc( sizeof(blocks) );
    block3 = malloc( sizeof(blocks) );

    FILE *fp;
    fp = fopen("test_system.txt","r");

    // block's size calculator and memory allocater ;
    while(fgets(line,60,fp))
    {
            if ( strstr(line,"\0") != NULL && strstr(line,"ID") == NULL && strstr(line,"END") == NULL ) {
            sizer++;
            }
            else if ( strstr(line,"END") != NULL ){

                if ( blcNum == 1){
                    block1->size = sizer;
                    blcNum++;
                    sizer = 0;
                }
                else if ( blcNum == 2){
                    block2->size = sizer;
                    blcNum++;
                    sizer = 0;
                }
                else if ( blcNum == 3){
                    block3->size = sizer;
                    blcNum++;
                    sizer = 0;
                }

            }
    };
    block1 = realloc(block1 , block1->size * sizeof(blocks));
    block2 = realloc(block2 , block2->size * sizeof(blocks));
    block3 = realloc(block3 , block3->size * sizeof(blocks));
    fclose(fp);
    line == NULL;
    blcNum=1;

    puts("\n\n");

    fp = fopen("test_system.txt","r");
    char *szTempString;
    int index=1;

    while(fgets(line,60,fp))
    {
        szTempString = line;
        if( strstr(line,"ID") == NULL && strstr(line,"END") == NULL )
        {
            printf("%s",line);
            if(index <= block1->size)
            {
                printf("%s",line);
                sscanf(szTempString,"%d %f %f",&(block1 + (index*sizeof(blocks)))->id,
                                               &(block1 + (index*sizeof(blocks)))->x1,
                                               &(block1 + (index*sizeof(blocks)))->y1);
                index++;
            }
        }

    }

   fclose(fp);

  printf("\nUploader's information : \n");
   for (temp =1; temp <= block1->size ; ++temp)
   {
    printf(" id : %d , X1 : %f , Y1 : %f  \n",(block1+(temp*sizeof(block1)))->id,(block1+(temp*sizeof(block1)))->x1,(block1+(temp*sizeof(block1)))->y1);
    puts("\nIt must be \nid : 1 , X1 : 1.00 , Y1 : 1.00");
   } 

    printf("\n1 . block's size : %d ",block1->size);
    printf("\n2 . block's size : %d ",block2->size);
    printf("\n3 . block's size : %d ",block3->size);    
    free(block1);
    free(block2);
    free(block3);
    return 0;
}

even also doesnt access values like I wish , where is the problem , It prints the line correctly but never get values right :(

Pip Doun
  • 1
  • 1
0

Because of sizeof(block1) is different with sizeof(blocks). Let try to replace in your print function.

0

I realized today these blocks pointers jump their first indexes thats why I couldnt fix.

#include "stdlib.h"
#include "string.h"

typedef struct {
    int size;
    int id;
    int tip;

    float x1;
    float x2;
    float y1;
    float y2;
}blocks;
int credit();

int main() {  

    blocks *block1 , *block2 , *block3;
    char line[60];

    int index=1;
    int blcNum=1;
    int endCount=1;
    int temp;

    int sizer=0;
    int* sizep;
    sizep = malloc( (int*)(sizep + 3*sizeof(int)));


    block1 = malloc( sizeof(blocks) );
    block2 = malloc( sizeof(blocks) );
    block3 = malloc( sizeof(blocks) );

    FILE *fp;
    fp = fopen("IEEE_test_system_12.txt","r");

    // block's size calculator and memory allocater ;

    while(fgets(line,60,fp))
    {

            if ( strstr(line,"ID") == NULL && strstr(line,"END") == NULL ) 
            {
                sizer++;
            }
            else if( strstr(line,"END") != NULL )
            {

                if(endCount == 1)
                {
                    *(sizep + endCount*sizeof(int)) = sizer;
                    sizer=0;
                    endCount++;
                }
                else if(endCount == 2)
                {
                    *(sizep + endCount*sizeof(int)) = sizer;
                    sizer=0;
                    endCount++;
                }
                else if (endCount == 3)
                {
                    *(sizep + endCount*sizeof(int)) = sizer;
                    sizer=0;
                }

            }
    };

    block1->size = *(sizep + sizeof(int));
    block2->size = *(sizep+2*sizeof(int));
    block3->size = *(sizep+3*sizeof(int));

    block1 = realloc(block1 , block1->size * sizeof(blocks));
    block2 = realloc(block2 , block2->size * sizeof(blocks));
    block3 = realloc(block3 , block3->size * sizeof(blocks));
    fclose(fp);
    line == NULL;
    fp = fopen("IEEE_test_system_12.txt","r");
    char *szTempString;
    while(fgets(line,60,fp))
    {

        if (  strstr(line,"ID") == NULL && strstr(line,"END") == NULL && strstr(line,"\0") != NULL)
        {


            szTempString = line;
            if ( blcNum == 1){
                printf("%s",line);
                sscanf(szTempString,"%d %f %f",&(block1 + (index*sizeof(blocks)))->id,&(block1 + (index*sizeof(blocks)))->x1,&(block1 + (index*sizeof(blocks)))->y1);
                index++;
            }
            else if ( blcNum == 2){
                printf("%s",line);
                sscanf(szTempString,"%d %d %f %f %f %f",&(block2 + (index*sizeof(blocks)))->id,&(block2 + (index*sizeof(blocks)))->tip, &(block2 + (index*sizeof(blocks)))->x1,&(block2 + (index*sizeof(blocks)))->y1,&(block2 + (index*sizeof(blocks)))->x2,&(block2 + (index*sizeof(blocks)))->y2);
                index++;
            }
            else if ( blcNum == 3){
                printf("%s",line);
                sscanf(szTempString,"%d %f %f %f %f",&(block3 + (index*sizeof(blocks)))->id,&(block3 + (index*sizeof(blocks)))->x1, &(block3 + (index*sizeof(blocks)))->y1,&(block3 + (index*sizeof(blocks)))->x2, &(block3 + (index*sizeof(blocks)))->y2);
                index++;
            }
        }
        else if ( strstr(line,"END") != NULL ){
                blcNum++;
                index = 0;
        }

    }   
    fclose(fp);
    printf("\nUploader's information : \n");
   for (temp =1; temp <= block1->size ; temp++)
   {
    printf(" id : %d , X1 : %.2f , Y1 : %.2f  \n",(block1+(temp*sizeof(blocks)))->id,(block1+(temp*sizeof(blocks)))->x1,(block1+(temp*sizeof(blocks)))->y1);
   } 

   printf("\nCustomer's information : \n");
   for (temp =1; temp <= block2->size ; temp++)
   {
    printf(" id : %d , tip : %d , X1 : %.2f , Y1 : %.2f , X2 : %.2f , Y2 : %.2f \n",(block2+(temp*sizeof(blocks)))->id,(block2+(temp*sizeof(blocks)))->tip,(block2+(temp*sizeof(blocks)))->x1,
                                                                            (block2+(temp*sizeof(blocks)))->y1,(block2+(temp*sizeof(blocks)))->x2,(block2+(temp*sizeof(blocks)))->y2);
   } 

   printf("\nLine's information : \n");
   for (temp =1; temp <= block3->size ; temp++)
   {
    printf(" id : %d , X1 : %.2f , Y1 : %.2f , X2 : %.2f , Y2 : %.2f \n",(block2+(temp*sizeof(blocks)))->id,(block2+(temp*sizeof(blocks)))->x1,
                                                                         (block2+(temp*sizeof(blocks)))->y1,(block2+(temp*sizeof(blocks)))->x2,
                                                                         (block2+(temp*sizeof(blocks)))->y2);
   }

    printf("\nblock1's size : %d ",block1->size);
    printf("\nblock2's size : %d ",block2->size);
    printf("\nblock3's size : %d ",block3->size);   
    printf("\na random number : %d",(block3->size*block2->size + block1->size));
    free(block1);
    free(block2);
    free(block3);
    //credit();
    return 0;
}```
Someone would find why first indexes are jumped ?
Pip Doun
  • 1
  • 1
-1
  • When you using function fgets(line,60,fp) in line 46, the file pointer will point to next line when you call fucntion fscanf. I suggest that you should use one template pointer and reference to each line out put, then parse it by using sscanf
  • You should not use for ( temp =0 ; temp < block1->size ; ++temp) because it is not necessary because you have already get each line in while loop. You can use a variable, increase it in each blocks and reset it at the end of each blocks.
  • The final problem I founded, you should use index * (sizeof(pointer type)) to get next elements. Here is source code after I fixed above issues based on your source code, hope that it can help you:

The code:

#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "stdio.h"

typedef struct {
    int size,id,tip;
    float x1,x2,y1,y2; }blocks;

int main() {
    blocks *block1 , *block2 , *block3;
    char line[60];
    int blcNum=1, sizer=0, temp;
    block1 = malloc( sizeof(blocks) );
    block2 = malloc( sizeof(blocks) );
    block3 = malloc( sizeof(blocks) );
    FILE *fp;
    fp = fopen("test_system.txt","r");
    while(!feof(fp)){
        fgets(line,60,fp);
            if ( strstr(line,"\0") != NULL && strstr(line,"ID") == NULL && strstr(line,"END") == NULL ) {
            sizer++;
            printf(line); 
            }
            else if ( strstr(line,"END") != NULL ){
                if ( blcNum == 1){
                    block1->size = sizer,blcNum++,sizer = 0;
                }
                else if ( blcNum == 2){
                    block2->size = sizer,blcNum++,sizer = 0;
                }
                else if ( blcNum == 3){
                    block3->size = sizer,blcNum++,sizer = 0;
                }
            }
    };
    block1 = realloc(block1 , block1->size * sizeof(blocks));
    block2 = realloc(block2 , block2->size * sizeof(blocks));
    block3 = realloc(block3 , block3->size * sizeof(blocks));
    fclose(fp);
    char *szTempString;
    int index = 0;
    line == NULL,blcNum = 1;
    fp = fopen("test_system.txt","r");
    while(!feof(fp)){
        fgets(line,60,fp);
        if (  strstr(line,"ID") == NULL && strstr(line,"END") == NULL && strstr(line,"\0") != NULL)
        {
            szTempString = line;
            if ( blcNum == 1){
                sscanf(szTempString,"%d\t%f\t%f",&(block1 + (index*sizeof(blocks)))->id,&(block1 + (index*sizeof(blocks)))->x1,&(block1 + (index*sizeof(blocks)))->y1);
                index++;
            }
            else if ( blcNum == 2){
                sscanf(szTempString,"%d\t%d\t%f\t%f\t%f\t%f",&(block2 + (index*sizeof(blocks)))->id,&(block2 + (index*sizeof(blocks)))->tip, &(block2 + (index*sizeof(blocks)))->x1,&(block2 + (index*sizeof(blocks)))->y1,&(block2 + (index*sizeof(blocks)))->x2,&(block2 + (index*sizeof(blocks)))->y2);
                index++;
            }
            else if ( blcNum == 3){
                sscanf(szTempString,"%d\t%f\t%f\t%f\t%f",&(block3 + (index*sizeof(blocks)))->id,&(block3 + (index*sizeof(blocks)))->x1, &(block3 + (index*sizeof(blocks)))->y1,&(block3 + (index*sizeof(blocks)))->x2, &(block3 + (index*sizeof(blocks)))->y2);
                index++;
            }
        }
        else if ( strstr(line,"END") != NULL ){
                blcNum++;
                index = 0;
        }
    }    fclose(fp);    printf("\nUploader's information : \n");    for (temp =0; temp < block1->size ; temp++)    {
    printf(" id : %d , X1 : %.2f , Y1 : %.2f  \n",(block1 + (temp*sizeof(blocks)))->id,(block1 + (temp*sizeof(blocks)))->x1,(block1 + (temp*sizeof(blocks)))->y1);    } printf("\ncustomer's information : \n");    for (temp =0; temp < block2->size ; temp++)    {
    printf(" id : %d , tip : %d , X1 : %.2f , Y1 : %.2f , X2 : %.2f , Y2 : %.2f \n",(block2 + (temp*sizeof(blocks)))->id,(block2 + (temp*sizeof(blocks)))->tip,(block2 + (temp*sizeof(blocks)))->x1,
                                                                                    (block2 + (temp*sizeof(blocks)))->y1,(block2 + (temp*sizeof(blocks)))->x2,(block2 + (temp*sizeof(blocks)))->y2);    } printf("\nLine's information : \n");    for (temp =0; temp < block3->size ; temp++)    {
    printf(" id : %d , X1 : %.2f , Y1 : %.2f , X2 : %.2f , Y2 : %.2f \n",(block3 + (temp*sizeof(blocks)))->id,(block3 + (temp*sizeof(blocks)))->x1,
                                                                         (block3 + (temp*sizeof(blocks)))->y1,(block3 + (temp*sizeof(blocks)))->x2,(block3 + (temp*sizeof(blocks)))->y2);    }
    printf("\n1 . block's size : %d ",block1->size);
    printf("\n2 . block's size : %d ",block2->size);
    printf("\n3 . block's size : %d ",block3->size);
    free(block1);
    free(block2);
    free(block3);
    return 0; }
  • 1
    I'll tried to reformat your post. Put code inside code blocks, not inside `>` citations. [How should we format our code blocks?](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks). Your code has many problems, has undefined behavior, is very unsafe and has inconsistent indentation. [`while(!ffeof())` is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and it is wrong here - you should check the return of `fgets`. `printf(line)` is very unsafe and shouldn't be used - in case `line` contains `%` character it will break. – KamilCuk Mar 23 '20 at 13:35
  • In you third bullet point - `you should use index(sizeof(pointer type))*`, no, that's not true. Pointer arithmetic already do the multiplication. Just use `[` like `&block3[index].id`. – KamilCuk Mar 23 '20 at 13:51
  • Thank you, I edited it again but I think both of two way &(block3 + (index * (sizeof(pointer type)))->id or &block3[index].id is correct. Because two way also move pointer to next n*byte on register. – Hubert Nguyen Mar 23 '20 at 14:00