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;
}
}
Attachments
Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 704960
PC_value: 4831838656
Tick_of_the_data: 13000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

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: 

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 384
PC_value: 4831838640
Tick_of_the_data: 15000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 704960
PC_value: 4831838656
Tick_of_the_data: 15000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 0000000000000000000000000000000011111111111111111111111111111111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 8184256
PC_value: 4831838656
Tick_of_the_data: 16000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 0000000000000000000000000000000011111111111111111111111111111111

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 1450432
PC_value: 4831838656
Tick_of_the_data: 16000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 0000000000000000000000000000000011111111111111111111111111111111

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 384
PC_value: 4831838640
Tick_of_the_data: 17000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 0000000000000000000000000000000011111111111111111111111111111111

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 1450432
PC_value: 4831838656
Tick_of_the_data: 18000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 0000000000000000000000000000000011111111111111111111111111111111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 704960
PC_value: 4831838656
Tick_of_the_data: 20000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000000000000000000010111000011110100000000000010111101111001

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 8184256
PC_value: 4831838656
Tick_of_the_data: 22000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000000000000000000010111000011001110000000000010111101111001

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 384
PC_value: 4831838640
Tick_of_the_data: 24000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000000000000000000010111000011110100000000000010111101111001

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 1450432
PC_value: 4831838656
Tick_of_the_data: 26000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000000000000000000010111000011001000100000000010111101111001

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 704960
PC_value: 4831838656
Tick_of_the_data: 50000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 704960
PC_value: 4831838656
Tick_of_the_data: 57000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001100110110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 8184256
PC_value: 4831838656
Tick_of_the_data: 60000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 704960
PC_value: 4831838656
Tick_of_the_data: 61000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000101111011110111111000011

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 8184256
PC_value: 4831838656
Tick_of_the_data: 67000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 384
PC_value: 4831838640
Tick_of_the_data: 70000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 705024
PC_value: 4831838720
Tick_of_the_data: 70000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 8184256
PC_value: 4831838656
Tick_of_the_data: 71000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 705024
PC_value: 4831838720
Tick_of_the_data: 76000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 384
PC_value: 4831838640
Tick_of_the_data: 77000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 1450432
PC_value: 4831838656
Tick_of_the_data: 80000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000000110101000000000100011

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 8184320
PC_value: 4831838720
Tick_of_the_data: 80000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 384
PC_value: 4831838640
Tick_of_the_data: 81000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 8184320
PC_value: 4831838720
Tick_of_the_data: 86000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 1450432
PC_value: 4831838656
Tick_of_the_data: 87000
Packet_transfers_through_Bus_name: system.toL3Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 705024
PC_value: 4831838720
Tick_of_the_data: 90000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 11010000000000001101110111100100000000010100000010111101110001

Thread_id: 1
Data_block_size: 8
Request_commmand_type: ReadReq
Packet_block_Address: 1398976
PC_value: 4831838676
Tick_of_the_data: 90000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadReq
Packet_block_Address: 448
PC_value: 4831838656
Tick_of_the_data: 91000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00000100010110001010100010110011010111

Thread_id: 1
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 8184320
PC_value: 4831838720
Tick_of_the_data: 92000
Packet_transfers_through_Bus_name: system.membus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00111000000000001101110111100100000000001010011010111101110001

Thread_id: 1
Data_block_size: 8
Request_commmand_type: ReadReq
Packet_block_Address: 1396800
PC_value: 4831838688
Tick_of_the_data: 92000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 

Thread_id: 0
Data_block_size: 64
Request_commmand_type: ReadResp
Packet_block_Address: 1450432
PC_value: 4831838656
Tick_of_the_data: 93000
Packet_transfers_through_Bus_name: system.toL2Bus
Is_Instruction: 0
Is_Data: 0
Data_Read: 0
Data_Write: 1
Bits_on_packet: 00001101010110001010100010110011010

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.

This article has been dead for over six months. Start a new discussion instead.