I have written a program which basically reads a file named "data.txt" line-by-line. As a line is read, it validates the line with a certain specification. If the specification is met, it will output the data within a file named data.txt. If the line does not meet the validation criteria, then it will output that specific line to a separate file named error.txt.
I have finished the code and was wondering if anyone could look at it and see if they could help me with a few points in making the code look more compact and tidy. I feel I have done the same code over and over again for no reason. I have added comments so the code should make sense.
#include <stdio.h> //library including standard input and output functions
#include <stdlib.h> //library including exit and system functions used below
#include <string.h> //library including string functions used
struct packet{
int source; // 1 - 1024 range (int)
int destination; // 1 - 1024 range (int)
int type; // 0 - 10 range (int) // Varibles for the structure
int port; // 1 = 1024 (int)
char data[50]; // 1 - 50 range (char)
};
int main()
{
char filename[32] = { '\0' } ; // variables which declare the I/O stream and the filename structure
char DataLine[71]; // Reads the file one line at a time
char ErrorLine[71]; // This is the varible that deals with the validation error
char TempStorage[5]; // Stores data to be validated
char TempData[50]; // Stores the data which will be validated
int TempS, TempD, TempT, TempP; // Stores the integer derived from the input file
int Flag = 0; // This is the Flag that indicates a Line has not passed validation
int Count = 0; // This is the Flag that indicated a line has passed validation
int Ecount = 0; // This counts the number of errors
struct packet *DataRecords;
DataRecords = malloc(sizeof(struct packet)); // This deals with storing the data needed for the next task.
printf("Enter the filename you wish to open\n");
scanf("%s", &filename);
// user inputs the filename
FILE *DataFile;
if (( DataFile = fopen(filename, "r")) == NULL)
{
printf ("\nfile could not be opened. : %s\n", filename); // If a value of NULL is returned then the program will close.
}
else
{
FILE *ErrorFile = fopen("error.txt","w"); // This will start searching through the lines and store the lines not passing the validation test to a txt file named "error.txt".
printf("File has been found, checking validation");
while( fgets (DataLine, 71, DataFile)!=NULL) {
strcpy(ErrorLine, DataLine);
strcpy(TempStorage, strtok(DataLine,":"));
TempS = atoi(TempStorage);
strcpy(TempStorage, strtok( NULL, ":")); // these lines of code looks through each line and stores the line within the "Temp Storage" varible, the : token is what the element within the line is seperated by.
TempD = atoi(TempStorage);
strcpy(TempStorage, strtok( NULL, ":"));
TempT = atoi(TempStorage);
strcpy(TempStorage, strtok( NULL, ":"));
TempP = atoi(TempStorage);
strcpy(TempData, strtok( NULL, ":"));
if (TempS < 1 || TempS > 1024) Flag = 1;
if (TempD < 1 || TempD > 1024) Flag = 1;
if (TempT < 0 || TempT > 10) Flag = 1; // // Validation aspect, if the validation is not met then a flag is added to which then the line is posted within the error file.
if (TempP < 1 || TempP > 1024) Flag = 1;
if (strlen(TempData) < 1 || strlen(TempData)> 50) Flag = 1;
if (Flag == 1)
{
Ecount++;
printf("Error %i %i:%i:%i:%i:%s",Ecount,TempS,TempD,TempT,TempP,TempData);
fprintf(ErrorFile,"%s", ErrorLine);
// fprintf writes formatted text to the output stream you specify
}
else
{
DataRecords[Count].source = TempS;
DataRecords[Count].destination = TempD;
DataRecords[Count].type = TempT;
DataRecords[Count].port = TempP;
strncpy(DataRecords[Count].data,TempData,51);
Count++; //increment sequence number
DataRecords = realloc(DataRecords,(Count+1)*sizeof(struct packet));//allocate more memory for packetdata
}
Flag = 0;
}
FILE *DFile = fopen("data2.txt","w");
int ii;
for (ii = 0; ii < Count; ii++)
{
fprintf(DFile, "%04i:%04i:%04i:%04i:%s",DataRecords[ii].source, // Where the data that has passed validation goes
DataRecords[ii].destination,
DataRecords[ii].type,
DataRecords[ii].port,
DataRecords[ii].data);
}
fclose(DFile);
fclose(ErrorFile);
fclose(DataFile);
printf("\nNumber of errors: %i \n", Ecount);
printf("Number of saved records: %i ", Count);
free(DataRecords);
}
return 0;
}
This is some of the data it needs to validate.
0005:0002:0002:0020:100000000000000000017
0001:0002:0002:0080:</BODY>
0006:0003:0002:0041:100000000000000000019
0006:0002:0002:0060:100000000000000000020
0002:0004:0002:0090:100000000000000000022
0001:0002:0003:0021:cp /etc/hosts /root
0002:0004:0002:0010:100000000000000000023
0003:0004:0002:0180:100000000000000000025
DataFile
– janos 7 hours ago