Skip to content

Commit

Permalink
REFACTOR: split the single c cource file into serveral .c and .h file…
Browse files Browse the repository at this point in the history
…s; but cannot call read_the_single_file function correctly
  • Loading branch information
tianhuan committed Sep 22, 2018
1 parent 1744a91 commit 1df365f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 58 deletions.
14 changes: 14 additions & 0 deletions MyStruct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by toto on 9/22/18.
//

#ifndef HOMEWORK1_TIANHUAN_TU_MYSTRUCT_H
#define HOMEWORK1_TIANHUAN_TU_MYSTRUCT_H


typedef struct MyStruct{
int address;
int id;
int times;
};
#endif //HOMEWORK1_TIANHUAN_TU_MYSTRUCT_H
75 changes: 17 additions & 58 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,33 @@
#include <cstdio>
#include <cstring>

struct MyTuple {
int address = 0;
int id = 0;
int times = 0;
};
#include "MyStruct.h"
#include "read_the_single_file.h"

int main() {
// input file name
const char read_file[] = "../read_first_100_lines.out";

// file pointer
FILE *read_single;

// char buffer to hold data within fgets()
char buffer[50];

// variables to pass data to tuple property
int operation_id, address, call_id, times;
float read_timestamp;

// char pointer to the address where the returned buffer of fgets() starts to be stored.
char *buffer_string;
// input files
const char *read_single_file = "../read_first_100_lines.out";
// const char *write_single_file = "../write_first_100_lines.out";

// pointer to the address where the dynamically allocated memory for our data starts
MyTuple *beginning = NULL ;
MyStruct *beginning_read_single = nullptr;
// MyStruct *beginning_write_single = nullptr;

// numbers of our data reading from files
unsigned long int number_of_read_single = 0;

unsigned int size_of_my_tuple = sizeof(MyTuple);

// read the file
read_single = fopen(read_file, "r");
if (read_single == nullptr) {
printf("Reading %s failed!", read_file);
exit(2);
}
unsigned long int ZERO = 0;
unsigned long int *number_of_read_single = &ZERO;
// unsigned long int *number_of_write_single = &ZERO;

while (nullptr != (buffer_string = fgets(buffer, 50, read_single))) {
// extract data from the buffer_string
sscanf(buffer_string, "%d %f %x %d", &operation_id, &read_timestamp, &address, &call_id);
times = 1;

// dynamically allocate memory to store our data
if (number_of_read_single == 0) {
// number_of_read_singe = 0 means current data is the first, and we need to newly allocate memory space.
beginning = (MyTuple *) calloc(number_of_read_single + 1, size_of_my_tuple);
} else {
// number_of_read_single > 0 means there exist data and we need to resize the memory space by 1 to store current data
beginning = (MyTuple *) realloc(beginning, size_of_my_tuple * (number_of_read_single + 1));
}
(beginning + number_of_read_single)->address = address;
(beginning + number_of_read_single)->id = call_id;
(beginning + number_of_read_single)->times = times;

// count + 1
number_of_read_single++;

// printf("%d\t%.1f\t0x%08x\t%d\n", operation_id, read_timestamp, address, call_id);
}
// read_the_single_file(read_single_file, beginning_read_single, number_of_read_single);

printf("Data below is reading from the memory\n");
for (int i = 0; i < number_of_read_single; i++) {
printf("0x%08x \t%d \t%d\n", (beginning + i)->address, (beginning + i)->id, (beginning + i)->times);
for (int i = 0; i < *number_of_read_single; i++) {
printf("0x%08x \t%d \t%d\n", (beginning_read_single + i)->address, (beginning_read_single + i)->id,
(beginning_read_single + i)->times);
}


fclose(read_single);
free(beginning);
free(beginning_read_single);
return 0;
}


64 changes: 64 additions & 0 deletions read_the_single_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by toto on 9/22/18.
//
#include <stdlib.h>
#include <stdio.h>

#include "MyStruct.h"

#ifndef HOMEWORK1_TIANHUAN_TU_READ_FILE_H
#define HOMEWORK1_TIANHUAN_TU_READ_FILE_H

#endif //HOMEWORK1_TIANHUAN_TU_READ_FILE_H
void read_the_single_file(char *file_name, MyStruct *beginning, unsigned long int *number){
// save the frequently-used value to make performance better
const unsigned int size_of_my_struct = sizeof(MyStruct);

// file pointer
FILE *input_scream;

// char buffer to hold data within fgets()
char buffer[50];

// variables to pass data to tuple property
int operation_id, address, call_id, times;
float read_timestamp;

// char pointer to the address where the returned buffer of fgets() starts to be stored.
char *buffer_string;


// read the input_scream.out
input_scream = fopen(file_name, "r");
if (input_scream == nullptr) {
printf("Reading %s failed!", file_name);
exit(2);
}

while (nullptr != (buffer_string = fgets(buffer, 50, input_scream))) {
// extract data from the buffer_string
sscanf(buffer_string, "%d %f %x %d", &operation_id, &read_timestamp, &address, &call_id);
times = 1;

// dynamically allocate memory to store our data
if (*number == 0) {
// number_of_read_singe = 0 means current data is the first, and we need to newly allocate memory space.
beginning = (MyStruct *) calloc(*number + 1, size_of_my_struct);
} else {
// *number > 0 means there exist data and we need to resize the memory space by 1 to store current data
beginning = (MyStruct *) realloc(beginning, size_of_my_struct * (*number + 1));
}
(beginning + *number)->address = address;
(beginning + *number)->id = call_id;
(beginning + *number)->times = times;

// count + 1
*number++;

// printf("%d\t%.1f\t0x%08x\t%d\n", operation_id, read_timestamp, address, call_id);
}

// do not forget to close the scream
fclose(input_scream);

}

0 comments on commit 1df365f

Please sign in to comment.