Hi all,

I have a problem with my code. Please help me.

I am trying to create multiple files based on the input file and read its respective data only.

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 8184256
PC_value: 4831838656
Tick_of_the_data: 14000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 010101010001011110000000001111111111

The above is a block of information in my input file. I am trying to create a file named on Data_block_size, Packet_transfers_through_Bus_name, and Is_Instruction. And write the Bits_on_packet in to the file.

The problem is I am unable to write the correct data into the correct file.

Please find the code below.

Cheers,

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

int main(int argc,char *argv[])
{
        FILE *stream,*stream1,*fopen();

        char file[150],block_size[3],bus_name[34],block_size_value[150],bus_name_value[150],packet_value[150],buffer[150],Is_Instruction[10],Is_Instruction_Value[150];
        char *arr[4]={"Data_block_size:","Packet_transfers_through_Bus_name:","Bits_on_packet:","Is_Instruction:"};
        char *arr1[6]={"64","32","16","8","4","2"};
        char *arr2[3]={"toL2Bus","toL3Bus","tomembus"};
        char *arr3[2]={"0","1"};

        int i=0,j=0,k=0, l=0;
        //stream=fopen(argv[1],"r");

        while(1)
        {

                strcat(file,arr1[j]);
                strcat(file,"_");


                strcat(file,arr2[k]);
                strcat(file,"_");

                strcat(file,arr3[l]);
                strcat(file,".txt");
                strcpy(block_size,arr1[j]);
                strcpy(bus_name,arr2[k]);
                strcpy(Is_Instruction, arr3[l]);
                stream1=fopen(file,"w");
                stream=fopen(argv[1],"r");

                while(!feof(stream))
                {
                        fscanf(stream,"%s",buffer);
//                      printf("%s \n", buffer,"\n");
                        if(strcmp(buffer,arr[i]))
                        {
                        }

                        else
                        {
                                if(i==0)
                                {
                                        fscanf(stream,"%s",block_size_value);
//                                      printf("%s %s\n",arr[i],block_size_value);

                                }

                                if(i==1)
                                {
                                        fscanf(stream,"%s",bus_name_value);     
//                                      printf("%s \n",bus_name_value);

                                }
                                if(i==2)
                                {
                                        fscanf(stream,"%s",packet_value);
//                                      printf("%s \n",packet_value);
                                }
                                if(i==3)
                                {
                                        fscanf(stream,"%s",Is_Instruction_Value);
//                                      printf("%s %s \n", arr[i],Is_Instruction_Value);

                                        if(Is_Instruction_Value == "0")
                                        {

                                             l++;
                                        }


                                if(strcmp(block_size,arr1[j]) && strcmp(bus_name,arr2[k])&&strcmp(Is_Instruction,arr3[l]))
                                {
                                        //printf("hello 3\n");
                                }
                                else
                                {
                                        if(strcmp(packet_value,"Thread_id:"))
                                        {
                                                fprintf(stream1,"%s", packet_value);
                                                memset(packet_value,'\0',150);
//                                              printf("%s\n", packet_value);
                                        }
                                    }
                                }
                        }
                i++;

                if(i==4)
                {
                        i=0;
                        //break;
                }
                }
        }

        k++;
        l++;

        if(k==3)
        {
                j++;
                k=0;
        }
        if(l==2)
        {
               l=0;
        }

//      printf(" %s\n",arr1[j]);
//      printf(" %s %s\n",block_size,arr1[j]);
//      printf(" %s %s\n",bus_name,arr2[k]);
//      printf("%d %d\n",j,k);

        memset(file,'\0',150);
        memset(block_size,'\0',3);
        memset(bus_name,'\0',34);

        fclose(stream);
        fclose(stream1);

        if(j==6)
        break;
}
}

It seems the problem is fscanf() -- it will only read up until the first white space( spaces, tabs, ets ). If you need the entire line then use fgets() to read the line and then parse it -- call strchr() to find the colon then extract the rest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.