From 87ff9a2bf19df167a94de76387621482bdf2ea79 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Tue, 3 Jan 2023 15:26:01 +0100 Subject: [PATCH] feat: adjust write-location-file --- write-location-file/location.c | 16 +++++++ write-location-file/location.h | 16 +++++++ write-location-file/readme.md | 4 ++ .../write-location-file-test.c | 43 ++++++++++++++----- write-location-file/write-location-file.c | 24 +++++++++++ write-location-file/write-location-file.h | 4 ++ 6 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 write-location-file/location.c create mode 100644 write-location-file/location.h diff --git a/write-location-file/location.c b/write-location-file/location.c new file mode 100644 index 0000000..dd5b2ab --- /dev/null +++ b/write-location-file/location.c @@ -0,0 +1,16 @@ +#include "location.h" + +struct location getLocation(char lat[11], char lon[10]){ + struct location lctn; + int i,j; + + for(i=0; i<11; i++){ + lctn.lat[i] = lat[i]; + } + + for(j=0; j<10; j++){ + lctn.lon[j] = lon[j]; + } + + return lctn; +} diff --git a/write-location-file/location.h b/write-location-file/location.h new file mode 100644 index 0000000..41ef140 --- /dev/null +++ b/write-location-file/location.h @@ -0,0 +1,16 @@ +/*It is a tradition to use CPP tricks to avoid parsing the same header file more than once*/ + +#ifndef LOCATION_H +# define LOCATION_H + +/*use struct statement to define structure*/ +struct location { + /*latitide like +052.26594*/ + char lat[11]; + /*longiture like +10.52673*/ + char lon[10]; +}; + +struct location getLocation(char lat[11], char lon[10]); + +#endif /* LOCATION_H */ diff --git a/write-location-file/readme.md b/write-location-file/readme.md index d98bfb7..c3f80b4 100644 --- a/write-location-file/readme.md +++ b/write-location-file/readme.md @@ -4,4 +4,8 @@ ``` gcc -Wall write-location-file-test.c write-location-file.c && ./a.out +``` + +``` +gcc -Wall write-location-file-test.c write-location-file.c location.c && ./a.ou ``` \ No newline at end of file diff --git a/write-location-file/write-location-file-test.c b/write-location-file/write-location-file-test.c index a31376e..940bed6 100644 --- a/write-location-file/write-location-file-test.c +++ b/write-location-file/write-location-file-test.c @@ -13,15 +13,16 @@ char lat[11]="52.26594"; char lon[10]="10.52673"; -static void test_write_location_file(){ - FILE* read; - char buf[17]; +static void test_write_stuct_location_file(){ + FILE* read; + char buf[17]; - /*create file*/ - write_location_file(lat, lon); + /*create file*/ + struct location lctnGet=getLocation(lat, lon); + write_struct_location_file(&lctnGet); /*read file*/ - read = fopen("location-file.csv", "a+"); + read = fopen("struct-location-file.csv", "a+"); if (NULL == read) { /*failed to open file and associate stream*/ @@ -31,17 +32,39 @@ static void test_write_location_file(){ //printf("content of this file are \n"); fscanf(read, "%17s", buf); - printf("test_write_location_file() buf: %17s\n", buf); + printf("test_write_struct_location_file() buf: %17s\n", buf); fclose(read); char result[18] = "52.26594,10.52673"; - printf("test_write_location_file() result: %17s\n", result); + printf("test_write_struct_location_file() result: %17s\n", result); - assert(strcmp(buf, result)==0 && "test_write_location_file()"); - //assert(write_location_file("52.26594", "10.52673")==0 && "test_write_location_file()"); + assert(strcmp(buf, result)==0 && "test_write_struct_location_file()"); +} + +static void test_write_location_file(){ + FILE* read; + char buf[17]; + /*create file*/ + write_location_file(lat, lon); + /*read file*/ + read = fopen("location-file.csv", "a+"); + if (NULL == read) { + /*failed to open file and associate stream*/ + fprintf(stderr, "Value of errno: %d\n", errno); + fprintf(stderr, "Error opening file: %s\n", strerror(errno)); + } + //printf("content of this file are \n"); + fscanf(read, "%17s", buf); + printf("test_write_location_file() buf: %17s\n", buf); + fclose(read); + char result[18] = "52.26594,10.52673"; + printf("test_write_location_file() result: %17s\n", result); + assert(strcmp(buf, result)==0 && "test_write_location_file()"); + //assert(write_location_file("52.26594", "10.52673")==0 && "test_write_location_file()"); } int main(){ test_write_location_file(); + test_write_stuct_location_file(); } diff --git a/write-location-file/write-location-file.c b/write-location-file/write-location-file.c index 6e2d431..5f0dfd6 100644 --- a/write-location-file/write-location-file.c +++ b/write-location-file/write-location-file.c @@ -6,6 +6,30 @@ #include #include +int write_struct_location_file(struct location *lctn){ + FILE *fp; + + printf("\nwrite-struct-location-file() Start...\n"); + + printf("\nwrite-struct-location-file() lat: %s, lon: %s\n", lctn->lat, lctn->lon); + + fp = fopen("struct-location-file.csv", "w"); + if (fp==NULL) + { + /*failed to open file and associate stream*/ + fprintf(stderr, "Value of errno: %d\n", errno); + fprintf(stderr, "Error opening file: %s\n", strerror(errno)); + } + + /*latitude e.g. 52.26594*/ + /*longitude e.g. 10.52673*/ + fprintf(fp, "%s,%s\n", lctn->lat, lctn->lon); + fclose(fp); + + printf("\nwrite-struct-location-file() Done!\n"); + return 0; +} + int write_location_file(char *lat, char *lon){ FILE *fp; diff --git a/write-location-file/write-location-file.h b/write-location-file/write-location-file.h index 694de74..817f06c 100644 --- a/write-location-file/write-location-file.h +++ b/write-location-file/write-location-file.h @@ -3,6 +3,10 @@ #ifndef WRITE_LOCATION_FILE_H # define WRITE_LOCATION_FILE_H +#include "location.h" + int write_location_file(char *lat, char *lon); +int write_struct_location_file(struct location *lctn); + #endif /* WRITE_LOCATION_FILE_H */