Seek details in SPIFFS for a specific date - esp8266

I have a ESP8266,
I've stored the below details in SPIFFS.
How can I seek all details like employee number ,name ,age for a specific date?
Structure I am using ,
struct
{
char date[4];
char emp_number;
char name;
char age;
}std_detail;
Details that stored in SPIFFS
2019-10-21,employe number1,name,age
2019-10-21,employe number2,name,age
2019-10-21,employe number3,name,age
2019-10-22,employe number1,name,age
2019-10-22,employe number2,name,age
2019-10-22,employe number3,name,age
2019-10-22,employe number4,name,age

Related

How do I convert NSString to an encoding other than UTF-8?

I'm working with c in iOS Project I'm trying to convert my string to respected type in c , below code is supposed to send to core Library
typedef uint16_t UniCharT;
static const UniCharT s_learnWord[] = {'H', 'e','l','\0'};
what i have done till now is string is the one what I'm passing
NSString * string = #"Hel";
static const UniCharT *a = (UniCharT *)[string UTF8String];
But it is failing to convert when more than one character , If i pass one character then working fine please let me where i miss, How can i pass like s_learnWord ?
and i tried in google and StackOverFLow none of the duplicates or answers didn't worked for me like this
Convert NSString into char array I'm already doing same way only.
Your question is a little ambiguous as the title says "c type char[]" but your code uses typedef uint16_t UniCharT; which is contradictory.
For any string conversions other than UTF-8, you normally want to use the method getCString:maxLength:encoding:.
As you are using uint16_t, you probably are trying to use UTF-16? You'll want to pass NSUTF16StringEncoding as the encoding constant in that case. (Or possibly NSUTF16BigEndianStringEncoding/NSUTF16LittleEndianStringEncoding)
Something like this should work:
include <stdlib.h>
// ...
NSString * string = #"part";
NSUInteger stringBytes = [string maximumLengthOfBytesUsingEncoding];
stringBytes += sizeof(UniCharT); // make space for \0 termination
UniCharT* convertedString = calloc(1, stringBytes);
[string getCString:(char*)convertedString
maxLength:stringBytes
encoding:NSUTF16StringEncoding];
// now use convertedString, pass it to library etc.
free(convertedString);

How to convert NSData to struct accurately [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I Got a data from device(BLE):<840100ec d5045715 00010014 00240018 00>
but the second byte can not convert accurately. Like these:
But I can do it use Uint8 array, Why? Thank you.
code like these:
// I got the data:<840100ec d5045715 00010014 00240018 00>
case SPK_FEEDBACK_HistoryDataPort:
// Log
NSLog(#"receive data:%#", [NSData dataWithBytes:originalCommandBytes length:sizeof(D2MHistoryDataPort)]);
// originalCommandBytes dataType:UInt8 *
D2MHistoryDataPort *historyData = (D2MHistoryDataPort *)originalCommandBytes;
// Log
NSLog(#"收到硬件返回的0x%x指令(历史数据体): 历史数据包的索引:%d; 时间戳:%d; 步数:%d; 卡路里:%d; 距离:%d; 睡眠:%d; 运动时长:%d",
historyData->cmd,
historyData->index,
(unsigned int)historyData->timeStamp,
historyData->steps,
historyData->calories,
historyData->distance,
historyData->sleep,
historyData->duration);
break;
// I declare this struct in another class
typedef struct {
UInt8 cmd;
UInt16 index;
UInt32 timeStamp;
UInt16 steps;// 步数
UInt16 calories;// 卡路里
UInt16 distance;// 距离,单位m
UInt16 sleep;// 睡眠
UInt16 duration;// 运动时长,单位minute
} D2MHistoryDataPort;
How the compiler lays out the individual fields of a struct in memory is implementation dependent. Usually the compiler has to add padding to properly align the fields and it might even reorder them (by grouping fields of the same size) to reduce the required padding and the overall size of the struct.
You can turn this behavior off using __attribute__((packed)):
typedef struct __attribute__((packed)) {
UInt8 cmd;
UInt16 index;
UInt32 timeStamp;
UInt16 steps;// 步数
UInt16 calories;// 卡路里
UInt16 distance;// 距离,单位m
UInt16 sleep;// 睡眠
UInt16 duration;// 运动时长,单位minute
} D2MHistoryDataPort;
What you are doing is quite guaranteed to not work. You are trying to take a struct, assume that you can interpret it as a sequence of bytes, write it and read it back. That's not going to work. Starting with structs having different layouts between compiler versions, between 32 and 64 bit compilers, and so on. People knew in the '90s that this was a bad idea.
Use the NSCoding protocol. Or convert the data to JSON. Do NOT try to interpret structs as a sequence of bytes.
If you absolutely cannot avoid using NSData, this is how it works safely:
Step 1: Define the external data format. The external data format is NOT "whatever the compiler decided to layout my struct". The external data format is "One unsigned byte cmd; two unsigned bytes index, most significant byte first. 4 unsigned bytes time stamp, most significant byte first, meaning the number of seconds since Jan 1st 1904, ... " and so on.
Then to read the struct, get a pointer to the first byte, check that you have enough bytes, and write
mystruct.cmd = p [0];
mystruct.index = (p [1] << 8) | p [2];
mystruct.timeStamp = (p [3] << 24) | (p [4] << 16) ...
and so on.

different restrictions on employee record

I want to make an employee record that inputs these members:
Int I'd
Char name
Car gender
Char phone number
Char address
Char salary
ID must be unique and can take only digits
Name must be alphabetic and can take only four spaces. Otherwise display a message
Gender can only F And M
Phone number can only digits and '-'. This short dash
Salary also can take digits
Please, help me as soon as possible.
You can set conditions based on the type of value.
For example:
final int ids[25];
int position;
//in the getid function
public void getid()
{
int i=0;
int id[position]=scanf("%d",&id);
for(i=20;i<=0;i--){
if(id[position]==ids[i])
{
//then show the message
printf("Duplicate id");
}
}
else{
printf("Nice!!");
}
}

Getting segmentation fault when giving input to structure members on run time

I have written a small code to fetch info of 'n' number of students.
But after running the program, I'm getting a segfault. Please find the code below.
struct students
{
char name[20];
int age;
int id;
}student[100];
int main()
{
int count;
int no_students;
printf("Enter no of students");
scanf("%d",&no_students);
for (count = 1 ; count <= no_students ; count++)
{
printf("Enter the details for student%d\n",count);
printf("Name:");
scanf("%s",student[count].name);
printf("Age:");
scanf("%d",student[count].age);
printf("ID:");
scanf("%d",student[count].id);
}
return 0;
}
root#debian:/home/renga/C_code# ./nike
Enter no of students3
Enter the details for student1
Name:renga
Age:12
Segmentation fault
Got the problem, missed reference operator in scanf.
printf("Age:");
scanf("%d",&(student[count].age));
printf("ID:");
scanf("%d",&(student[count].id));
This may not be the right group to ask C language help/syntax question. I would recommend reading a good C programming language book ex: http://www.cprogramming.com/tutorial/c-tutorial.html
Having said that, the problem is, you are trying to read your int data type (age & id) values incorrectly. You need to read the values into the address of the variables as shown below...
scanf("%d",&student[count].age);

C Programming: Linked Lists

I'm writing a program using linked list (such a nightmare).
Anyway, the purpose of the program is to enter 8 characters and have the program print the characters back out to you and also print the characters back out in reverse order, using linked lists of course.
I got this so far. There's a lot wrong with it (i think).
Problems are
When asking for characters from the user it should read in the amount of characters automatically without having to ask for how many characters
Also, when it it compiles it prints gibberish to the screen, for example I just ran it and it printed
¿r
(àõ($ê¿¿
a¿r
(àõ($ê¿¿
¿r
(àõ($ê¿¿
b¿r
(àõ($ê¿¿
Lots of help needed here. It would be so much appreciated!
Code of course
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strsize 30
typedef struct member
{
int number;
char fname[strsize];
struct member *next;
}
RECORD;
RECORD* insert (RECORD *it);
RECORD* print(RECORD *it, int j);
int main (void)
{
int i, result;
RECORD *head, *p;
head=NULL;
printf("Enter the number of characters: ");
scanf("%d", &result);
for (i=1; i<=result; i++)
head=insert (head);
print (head, result);
return 0;
}
RECORD* insert (RECORD *it)
{
RECORD *cur, *q;
int num;
char junk;
char first[strsize];
printf("Enter a character:");
scanf("%c", &first);
cur=(RECORD *) malloc(sizeof(RECORD));
strcpy(cur->fname, first);
cur->next=NULL;
if (it==NULL)
it=cur;
else
{
q=it;
while (q->next!=NULL)
q=q->next;
q->next=cur;
}
return (it);
}
RECORD* print(RECORD *it, int j)
{
RECORD *cur;
cur=it;
int i;
for(i=1;i<=j;i++)
{
printf("%s \n", cur->fname);
cur=cur->next;
}
return;
}
You have:
in insert:
char first[strsize];
scanf("%c", &first); /* note the %c */
strcpy(cur->fname, first);
in print
printf("%s \n", cur->fname);
You should have %s instead of %c and therefore change &format to format in the argument list, as format itself represents the address of the location the string is to be stored.
So the scanf call should be like below
scanf("%s", first);
Another thing. If you have specified a return type in the print function then you should return something, or make it return nothing (declare return type as void). This will not pose any problem in this case although.
Read the warning messages which the compiler throws to you and you would see the compiler actually had answered your questions.
You need to do some redesigns i think. For example to traverse the linked list you do not need to counter 'j'. you can detect the list termination by inspecting if the next link is NULL or not.
Your question was to print the characters or strings in reverse, so you need to write some other print function than what you have wrote.

Resources