feat: adjust write-location-file
This commit is contained in:
parent
9aa39c1b53
commit
87ff9a2bf1
|
@ -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;
|
||||
}
|
|
@ -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 */
|
|
@ -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
|
||||
```
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -6,6 +6,30 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue