Download presentation
Presentation is loading. Please wait.
1
Sorting Tutorial Using C On Linux
2
Requirements Sorting of data
Take input from a file (e.g. students.csv). File contains student names and percentage marks separated by comma. Sort the data according to the Name field. Store the output to another file. Output filename will be input file name suffixed by “.sorted” (e.g. students.csv.sorted) No limit on the number of records in the files. Code must handle very large data e.g. 1 Million records -D option to enable debug log. High level commentary to show the progress and total time taken by the program.
3
Goals How to design modules? Medium / Complex C programming concepts
Code formatting, indentation, comments File handling C functions: fopen(), fclose(), fgets(), fwrite() e.t.c. Argument checking using getopt() Sorting using qsort() Memory allocation for creating dynamic size arrays using malloc() and realloc() Variable arguments handling in a function (debug_log) How to design Test Cases and data for testing?
4
Prerequisites Basic and some advance concepts of C. Linux tutorial.
Man Page study. Basic knowledge of test cases designing.
5
Design Man page (manual / document) for the program Input / Output
Methods Data Structures Unix C API's
6
Guidelines Minimum global variables
Divide code in simple and self contained modules Add error checking at every place Meaningful names of all variables, functions, macros, input arguments. Not like tmp, tmp1. Call debug log at appropriate place File header, function header and block comments and line comments. Comments for each variable Give meaningful, grammatically correct error and debug messages.
7
Man Page of the Tool Syntax
c_sort_tutorial -f <file name> -D <option> e.g.:- c_sort_tutorial –f students.csv -D 1/2 -f argument must be present along with the file name. -D argument is optional. If present, Debug log must be enabled. If given with 1, only high level log must be present If given with 2, detailed log must occur.
8
Input / Output Input File Name given by the user with the arguments.(e.g.:-students.csv) The file contains the Name and Percentage marks of the students separated by comma Output File named suffixed .sorted to input file (e.g.:-students.csv.sorted) containing the record sorted by the name.
9
Sample Input / Output Input File – students.csv
Nick Massa,99.99 Mohit Garg,100.00 Anil Kumar,95.55 Output File – students.csv.sorted
10
Methods
11
main(int argc, char *argv[])
Purpose Main function of program Input int argc :– Number of argument. char *argv[]:-Array of arguments. Tasks Take start time stamp using get_cur_date_time() It will call check_argument(). It will call sort_data(). Take end time stamp using get_cur_date_time() Output Exit with 0 on success.
12
void check_arguments(int argc,char *argv[])
Purpose Parse, extract and validate input arguments. Input Same as main function. int argc :– Number of argument. char ** argv:-Array of arguments. Task Check for -f argument Must be present only once. followed by filename. (e.g. -f students.csv) Save filename in a global variable Check for –D argument is present with related option. No other argument is there. If any error occurs call usage() to show error and exit.
13
static void sort_data()
Purpose To sort the data according the field Name. Input None Task Open input file using fopen() in read mode. If any error occurs call usage() with following input. “Error Occurs in opening file” Call strerror() with concerned error no and exit with -1.
14
sort_data() continued
In a loop, read line by line using fgets() Check for error if any in fgets() Tokenize line using get_token() If number of fields is not 2, give warning and skip that line Add this record in array using add_record() Close file using fclose() If any error occurs print error message and call strerror(). Sort the data using Unix qsort() call. Save sorted data using save_sorted_data() method Output None
15
static int get_tokens(char *line, char *fields[], char *token )
Purpose Parse the token to each records of file and check for the valid records. Input Pointer to the string as input. Array of fields. Character token. Task Parse the field as token and insert these tokens to fields array. Output Returns the no. of tokens parsed.
16
void add_record(char *student_name, double student_percentage)
Purpose Add record in dynamic memory array Input Pointer to the char array name and double marks. Name: Char array of name. e.g.:-Ram Marks: Double marks. e.g.:-78.90 Task Call create_table_entry to check array space. Add entries in the array. Output None
17
int create_table_entry(int. row_num, int. total, int. max, char
int create_table_entry(int *row_num, int *total, int *max, char **ptr, int size, char *name) Purpose To create array entries dynamically. Input Integer Row Number ,Total entries ,Maximum Entries ,Size Char Array ptr , name Task Check for space in array. Reallocate array size if required. Check for debug_flag argument If greater than 1 than detail log must be enabled. Output Returns 0 on success.
18
static void save_sorted_data()
Purpose To store the sorted entries in the output file. Input None Task Open the output file(e.g.:-student.csv) using fopen(). If any error occurs call usage(). Store all entries from array to the file one by one. Close the file using fclose(). If any error occur print error message. Output
19
void usage(char *err_msg)
Purpose To print error and show usage of command Input Error message as input. Output Prints the error message and usage.
20
void debug_log(char *file_name, int line, char *fname, char *format, ...)
Purpose Create debug_log file and print log to it. Input string file_name ,int line ,string fname ,string formate … Task Call open_log to open/create debug.log file Print log to the file. Output None
21
static void open_log(char. name, int. fd, unsigned long max_size, char
static void open_log(char *name, int *fd, unsigned long max_size, char *header) Purpose Create debug.log.prev and print log to it. Input string file_name ,int *fd ,long max_size ,string header Task Close debug.log if open and rename it to debug.log.prev Create new debug.log file to print log to it. Output None
22
static char *get_cur_date_time()
Purpose To print the date and time at the time of function call. Input None Task Fetch the current system date and time. Output Returns date and time in string formate.
23
int comparator (const void *rec1,const void *rec2)
Purpose To compare the entries and find greater one. Input Pointers to the two element to compare. Task Compares these values and gives output Output An integer output with the following aspects. If rec1<rec2 then returns -1 integer. If rec1=rec2 then return zero. If rec1>rec2 then returns +1 integer.
24
Data Structure Dynamic Array
Dynamic Array is used to store and sort data from input file. Dynamic Array An array data structure ,can be resized during runtime Any number of elements can be added and removed during execution.
25
C API’s getopt(int argc, char * const argv[], const char *optstring)
This function parses the command line arguments. Its argument argc and argv are the argument count and array as passed to the main() function on program innovation. When getopt() is called repeatedly, it returns successively each of the option characters from each of the option elements. If there are no more option characters then getopt() returns -1 and exit.
26
fopen(const char *path, const char *mode)
Opens the file named as the string pointed to by path and associates a stream with it. Opens or create a new file ,overwrite it, append it depends on the mode string. If there was no error, the output file handle the file opened. If error occurs, it returns a NULL pointer. int fclose(FILE *stream) Closes the file associated with the stream and disassociates it. On successful completion it returns 0 otherwise EOF is returns. The global variable errno is set to indicate the error.
27
char *strerror(int errnum)
Returns a pointer to the string describing the error code passed in the argument errnum . an unknown error message if the error code is unknown. The error strings produced by strerror depends on the developing platform and compiler char *fgets(char *s, int size, FILE *stream) It reads the characters from stream and stores them into the buffer pointed to by s Reading stops after an EOF or a newline occurs. If a newline is read, it is stored into the buffer automatically.
28
int fputs(const char *s, FILE *stream)
It writes the string s to stream, without its trailing ’\0’. Returns a non-negative number on success, or EOF on error. void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) Sorts an array with nmem elements of size size. The base argument points to the start of the array. This function returns no value.
29
int fprintf ( FILE * stream, const char * format, ... )
Place output to a named output stream. Returns the no. of character outputted or a negative no. if an error occurs. int fscanf(FILE *stream, const char *format, ...) It reads input from the stream pointer stream. Returns the number of input items assigned.
30
Testing
31
Validation Different set of activities ensures the program is traceable to the client requirement. Quality assurance process ,provides the high degree of assurance of a program. There are two basic field where validation can be done File Validation Data Validation
32
File and Arguments test Cases
Case 1:-File not present. Explanation:-Should produce an error that File is not present at the location. Case 2:- -f argument is not present. Explanation:-Should produce an error that -f argument must be present an print the usage again. Case 3:-File name not given with -f argument. Explanation:-Should produce an error that file name must be given with the -f argument. Case 4:-File does not have read permission Explanation:-Should produce an error that Remove read permission and input again.
33
Case 5:-Extra argument given(e.g.:- -t).
Explanation:-Should produce an error that only -f and –D parameters are allowed. Case 6:- -D given with extra parameters(e.g.:- -D 3) Explanation:-Should produce an error that -D is allowed with only 1 / 2 argument.
34
Sorting Test Cases(Valid Records)
Case 1:- File is empty. Explanation:-Should produce an warning that file is empty. Case 2:-File has one record Explanation:-Output file should also have one record. Case 3:-File has two record that are already sorted. Eplanation:-Output file must contains only two record. Case 4:-File has ten record. Explanation:-Output must also have ten sorted record.
35
Sorting Test Cases( Some Invalid Records)
Case 1:- File has one invalid record. Explanation:-Output file must be empty. Case 2:- File has one valid and one invalid record. Explanation:-Output file should contain one valid record Case 3:-File has more than one invalid record and more than one valid records. Explanation:-Output file must have all valid record in sorted form.
36
Performance Testing Cases
Case 1:-Check performance if file contains 1000 records. Expected Results:-Program must work properly for 1000 of entries and should not produce any arror. Case 2:-Check performance if file contains records. Expected Results:- Program must work properly for users and should not fail. Case 3:-Check performance if file contains records or more. Expected Results:- Program must work properly and consistently for any number of file entries with respect to time and space.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.