Read a string and get it in (int) in C - c

Can you help me? I have a string 23;56;36.6;run in a txt file.Then I am reading this string in order to use it for some work: I would like to take this values from a string, then compare them with some values in a code and output my result in console. I think,I should use atoi() function that make my string in numbers, for picking out , I am using strtok(). But how correctly should I record my tokens in loop while and the last token is a type of char. How can I do this work?
CODE:
void printInfo(int note)
{
int i;
FILE *out;
char str[250];
char sp[10]=";";
char *istr;
if ((out =fopen("test.txt","r"))==NULL)
printf("Error open, file\n");
else
{
for (i=0;i<note;i++)
{
fgets(str,250,out);
istr=strtok(str,sp);
while (istr != NULL)
{
printf("%d\n",atoi(istr));
istr=strtok(NULL,sp);
// I think, I need to create a variable for recording my values.
}
}
}
fclose(out);
}

I would use sscanf to convert the string to the three floats:
#include <stdio.h> // sscanf
#include <stdlib.h> // EXIT_SUCCESS
#include <string.h> // memset
int main(void) {
const char *input = "23;56;36.6;run";
int i;
float numbers[3] = {0, 0, 0};
char buf[10];
int nElementsRead;
// init buf
memset(buf, 0, sizeof(buf));
// sscanf returns the number of read elements
// or EOF on error
nElementsRead = sscanf(input, "%f;%f;%f;%9s", &numbers[0], &numbers[1], &numbers[2], buf);
if (nElementsRead == 4) {
printf("Successfully read %d elements\n", nElementsRead);
for (i = 0; i < 3; ++i) {
printf("number[%d]: %f\n", i, numbers[i]);
}
printf("Buffer is: %s\n", buf);
} else {
printf("Something went wrong!");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Another solution, using comparable records:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct MyRecord_s {
int f1;
int f2;
float f3;
char f4[10];
} MyRecord;
static int MyRecordInit( MyRecord * out, char * line ) {
if( out == NULL ) {
return 0;
}
char * f1 = strtok( line, ";" );
char * f2 = strtok( NULL, ";" );
char * f3 = strtok( NULL, ";" );
char * f4 = strtok( NULL, ";" );
if( f1 && f2 && f3 && f4 ) {
char * err = NULL;
out->f1 = strtol( f1, &err, 10 );
if( err && *err ) {
return 0;
}
out->f2 = strtol( f1, &err, 10 );
if( err && *err ) {
return 0;
}
out->f3 = strtof( f1, &err );
if( err && *err ) {
return 0;
}
strncpy( out->f4, f4, 10 );
out->f4[9] = '\0';
return 1;
}
return 0;
}
int MyRecordCmp( const MyRecord * r1, const MyRecord * r2 ) {
int diff = r1->f1 - r1->f2;
if( diff ) {
return diff;
}
diff = r1->f2 - r2->f2;
if( diff ) {
return diff;
}
float d = r1->f3 - r2->f3;
if( d > 0.000001 ) {
return +1;
}
if( d < -0.000001 ) {
return -1;
}
return strcmp( r1->f4, r2->f4 );
}
int main() {
char line1[] = "23;56;36.6;run";
char line2[] = "24;57;37.6;stop";
MyRecord r1, r2;
if( MyRecordInit( &r1, line1 ) && MyRecordInit( &r2, line2 )) {
printf( "cmp: %d\n", MyRecordCmp( &r1, &r2 ));
}
return 0;
}

Related

malloc in a loop using valgrind

I am having troubles with memory leaks (error detected by valgrind).
So here is my first function:
message *creationCellule() {
message *cellule;
cellule = (message *)malloc(sizeof(message));
if (cellule != NULL) {
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
}
return cellule;
}
It returns cellule which is allocated by malloc.
Now I have this:
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
message *test;
test = creationCellule();
int k = 0;
if (fp != NULL) {
k = fscanf(fp, "%d %d ", &(test->dateDeb), &(test->dateFin));
while (k != EOF) {
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
}
}
}
In which I call creationCellule() in a loop.
My problem is that, if I put free(test) inside a loop I lose all the context of my code and valgrind shows me ERROR SUMMARY:213 errors from 19 contexts.
What should I do ?
This is the complete code and the valgrind output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tp1.h"
message *creationCellule() {
message *cellule;
cellule = (message *)malloc(sizeof(message));
if (cellule != NULL) {
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
}
return cellule;
}
message **recherche(message *tete, int date) {
message **prec = tete;
message *cour = *tete;
while (cour != NULL && cour->dateDeb < date) {
prec = &(cour->suivant);
cour = cour->suivant;
}
return prec;
}
void insertion(message **tete, message *cellule) {
message **prec;
if (cellule != NULL) {
prec = recherche(tete, cellule->dateDeb);
cellule->suivant = *prec;
*prec = cellule;
}
}
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
message *test;
test = creationCellule();
int k = 0;
if (fp != NULL) {
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
while (k != EOF) {
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
}
}
}
void affichageListe(message **tete) {
if (tete != NULL) {
message *tmp = *tete;
while (tmp != NULL) {
//printf("jam ktu\n");
printf("DateDeb = %d \n", tmp->dateDeb);
printf("DateFin = %d \n", tmp->dateFin);
printf("Text = %s \n", tmp->text);
tmp = tmp->suivant;
}
}
}
void suppression(message **tete, int valeur, int dateDeb) {
message **prec;
prec = recherche(tete, dateDeb);
//printf("Prec text: %s , prec dateFin: %d\n", (*prec)->text, (*prec)->dateFin);
if ((*prec) != NULL && (*prec)->dateFin == valeur) {
(*prec) = (*prec)->suivant;
}
}
void supprimeObsoletes(message **tete) {
message *pt = *tete;
time_t temps;
struct tm *date;
int intNum;
temps = time(NULL);
date = localtime(&temps);
char buffer[9];
if ((date->tm_mon) < 10) {
sprintf(buffer, "%d0%d%d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
} else {
sprintf(buffer, "%d%d%d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
}
intNum = atoi(buffer);
while (pt != NULL) {
if ((pt->dateFin) < intNum) {
printf("KTU HYB %s\n", pt->text);
suppression(tete, pt->dateFin, pt->dateDeb);
}
pt = pt->suivant;
}
}
void changeDate(int dateChange, int dateInit, message **tete) {
message *point = *tete;
//printf("Kjo eshte tete %p:\n", (*point));
while (point != NULL) {
if ((point->dateDeb) == dateInit) {
printf("%d\n", point->dateDeb);
printf("%s\n", point->text);
point->dateDeb = dateChange;
}
point = point->suivant;
}
}
int main(int argc, char *argv[]) {
const char *name = argv[1];
message *pointeur = NULL;
message **tete = &pointeur;
int dateInit = 19973012;
int dateChange = 20003008;
FILE *fp = fopen(name, "r");
lectureFichier(name, tete);
//changeDate(dateChange, dateInit, tete);
supprimeObsoletes(tete);
affichageListe(tete);
return 0;
}
The header: tp1.h
#ifndef TP1_TEST_H
#define TP1_TEST_H
#define TAILLE_MAX 100
typedef struct cell {
int dateDeb;
int dateFin;
char text[TAILLE_MAX];
struct cell *suivant;
} message;
message *creationCellule();
message **recherche(message **tete, int date);
void affichageListe(message **tete);
void insertion(message **tete, message *cellule);
void lectureFichier(const char * nomFichier, message **tete);
The .txt file (added in execution)
19973012 20220512 TEXT 1
19980511 19001203 THIS
20011102 20301123 HOUSE
20020809 20301025 HELP
Valgrind output:
Function lectureFichier does not free unused nodes properly.
Here is a corrected version:
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
if (fp != NULL) {
for (;;) {
message *node = creationCellule();
if (fscanf(fp, "%d%d %99s", &node->dateDeb, &node->dateFin, node->text) == 3) {
insertion(tete, node);
} else {
free(node);
break;
}
}
}
}
the following proposed code:
cleanly compiles
properly handles errors
removed unused code
does NOT cleanup the linked list when an error occurs -- you will need to add that
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef TP1_TEST_H
#define TP1_TEST_H
#define TAILLE_MAX 100
struct cell
{
int dateDeb;
int dateFin;
char text[TAILLE_MAX];
struct cell * suivant;
};
typedef struct cell message;
message * creationCellule( void );
message ** recherche( message ** tete, int date );
void affichageListe( message ** tete );
FILE * ecrireFichier( message ** tete );
void dateNonExpires( message ** tete );
#endif // TPI_TEST_H
message * creationCellule()
{
message * cellule = malloc(sizeof(message));
if( !cellule )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
return cellule;
}
message ** recherche( message ** tete, int date )
{
message ** prec = tete;
message * cour = *tete;
while( cour != NULL && cour->dateDeb < date )
{
prec = &(cour->suivant);
cour = cour->suivant;
}
return prec;
}
void insertion(message ** tete, message * cellule)
{
(void)tete;
(void)cellule;
// insert code that does something reasonable
}
void lectureFichier(const char * nomFichier, message ** tete)
{
FILE * fp = fopen(nomFichier, "r");
if( !fp )
{
perror( "fopen failed" );
exit( EXIT_FAILURE );
}
test = creationCellule();
while( fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin)) == 2 )
{
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
}
}
void affichageListe( message ** tete )
{
if( tete )
{
message * tmp = *tete;
while( tmp )
{
//printf("jam ktu\n");
printf( "DateDeb = %d \n", tmp->dateDeb );
printf( "DateFin = %d \n", tmp->dateFin );
printf( "Text = %s \n", tmp->text );
tmp = tmp->suivant;
}
}
}
void suppression( message**tete, int valeur, int dateDeb )
{
message **prec;
prec = recherche( tete, dateDeb );
//printf("Prec text: %s , prec dateFin: %d\n",(*prec)->text,(*prec)->dateFin);
if( (*prec) != NULL && (*prec)->dateFin == valeur )
{
(*prec)=(*prec)->suivant;
}
}
void supprimeObsoletes(message **tete)
{
message *pt = *tete;
time_t temps;
struct tm *date;
temps=time(NULL);
date=localtime(&temps);
char buffer[9];
if((date->tm_mon)<10)
{
sprintf(buffer,"%d0%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
else
{
sprintf(buffer,"%d%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
int intNum=atoi(buffer);
while( pt )
{
if( (pt->dateFin) < intNum )
{
printf( "KTU HYB %s\n", pt->text );
suppression( tete, pt->dateFin, pt->dateDeb );
}
pt=pt->suivant;
}
}
int main(int argc, char * argv[])
{
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s filename\n", argv[0] );
exit( EXIT_FAILURE );
}
const char * name = argv[1];
message * pointeur = NULL;
lectureFichier( name, &pointeur );
supprimeObsoletes( &pointeur );
affichageListe( &pointeur );
while( pointeur )
{
message *current = pointeur;
pointeur = pointeur->suivant;
free( current );
}
return 0;
}

Only printing last line of txt file when reading into struct array in C

I am reading from a txt file into an array of structures. Example txt:
-4.5 -1 0 0
4.0 1 0 0
8 0 1 2
12.1 0 -6 1
-3.2 2.5 -3.0 4
The 4 values of each line correspond to the 4 values in the structure. The file may contain up to 100 lines (MAX is defined as 100). With the following code I am trying to store each line into the respective index of the struct array and then print:
FILE *fileName = NULL;
typedef struct chargeData_struct {
double Q, x, y, z;
} ChargeData;
ChargeData values[MAX], *p = values;
fileName = fopen("charge2.txt", "r");
if (fileName == NULL)
{
printf("ERROR: Could not open file.");
}
int k = 0;
while (fscanf(fileName, "%lf %lf %lf %lf", &p[k].Q, &p[k].x, &p[k].y, &p[k].z) != EOF);
{
printf("%f %f %f %f\n", p[k].Q, p[k].x, p[k].y, p[k].z);
k++;
}
fclose(fileName);
However, only the last line of the txt file is printed. Is the same index of the struct array being overwritten each time?
You are using an extra semicolon which makes all the trouble, here:
while (fscanf(...) != EOF);
{
...
Remove it and you should be fine.
What happens with your code is that while(..); is equivalent to this:
while(...)
{
; // do nothing
}
thus does not enter the body (the one you think is the body) of your loop (since the actual body does nothing). However scanf() continues to parse the file, and then this section of your code executes:
{
printf("%f %f %f %f\n", p[k].Q, p[k].x, p[k].y, p[k].z);
k++;
}
independently, where the curly braces are treated like they wanted to state scope.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LINE_BUFFER_LEN (512)
#define RESERVE_NEWLINDE 0
#define AUTO_FILTER_NEWLINDE 1
typedef int (* LINE_READER)(char * pstrLine, int uiBufferLen, void * pvData);
typedef struct st_HW_SSP_CONFIG
{
const char * pstrConfigPath;
LINE_READER pfLineReader;
FILE * pstFile;
void * pvData;
int CurrentLine;
int Flag;
} CONFIG_ST;
int CloseConfig(CONFIG_ST * pstConfig)
{
if (!pstConfig)
{
// record error
return -1;
}
if (fclose(pstConfig->pstFile))
{
// record error
}
return 0;
}
int OpenConfigFile(const char * pstrFilePath, CONFIG_ST * pstConfig)
{
FILE * pstFile = NULL;
if ((!pstrFilePath) || (!pstConfig))
{
return -1;
}
pstFile = fopen(pstrFilePath, "r");
if (!pstFile)
{
return -1;
}
pstConfig->pstFile = pstFile;
pstConfig->pstrConfigPath = pstrFilePath;
pstConfig->Flag = RESERVE_NEWLINDE;
return 0;
}
int IsNullStr(const char *pcStr)
{
const char *pcTmp = pcStr;
while ('\0' != *pcTmp)
{
if (!isspace(*pcTmp))
{
return 0;
}
pcTmp++;
}
return 1;
}
int IsEffectiveLine(char acFileLineBuffer[LINE_BUFFER_LEN])
{
if (0 == strlen(&acFileLineBuffer[0]))
{
return 0;
}
if ('#' == acFileLineBuffer[0]) // strip as a comment line
{
return 0;
}
if (IsNullStr(&acFileLineBuffer[0]))
{
return 0;
}
return 1;
}
void FilterNewLine(char* pcLine, int MaxNumLen)
{
int uiLen = strlen(pcLine);
if (uiLen > 1)
{
if ('\n' == pcLine[uiLen - 1])
{
pcLine[uiLen - 1] = '\0';
if (uiLen > 2)
{
if ('\r' == pcLine[uiLen - 2])
{
pcLine[uiLen - 2] = '\0';
}
}
}
}
return;
}
int ReadConfigFile(CONFIG_ST * pstConfig)
{
char acFileLineBuffer[LINE_BUFFER_LEN] = {0};
char * pstrRead = NULL;
int Ret = 0;
if (!pstConfig)
{
return -1;
}
if ((!pstConfig->pstFile) || (!pstConfig->pfLineReader))
{
return -1;
}
rewind(pstConfig->pstFile);
pstConfig->CurrentLine = 0;
do
{
memset((void *)&acFileLineBuffer[0], 0, LINE_BUFFER_LEN);
pstrRead = fgets(&acFileLineBuffer[0], LINE_BUFFER_LEN - 1, pstConfig->pstFile);
if (pstrRead)
{
pstConfig->CurrentLine ++;
if (0 == IsEffectiveLine(acFileLineBuffer))
{
continue;
}
if (AUTO_FILTER_NEWLINDE == pstConfig->Flag)
{
FilterNewLine(acFileLineBuffer, LINE_BUFFER_LEN - 1);
}
if (pstConfig->pfLineReader)
{
Ret = pstConfig->pfLineReader(&acFileLineBuffer[0],
LINE_BUFFER_LEN,
pstConfig->pvData);
if (Ret)
{
break;
}
}
}
}
while (pstrRead);
return Ret;
}
int ReadConfigFileEx(const char * pFilePath,
LINE_READER pfReader,
void * pData, int Flag)
{
int Ret = 0;
CONFIG_ST stConfig = {0};
Ret = OpenConfigFile(pFilePath, &stConfig);
if (Ret)
{
return Ret;
}
stConfig.pfLineReader = pfReader;
stConfig.pvData = pData;
stConfig.Flag = Flag;
Ret = ReadConfigFile(&stConfig);
CloseConfig(&stConfig);
return Ret;
}
int StringSplit(char *pcStr, char cFlag,
char * pstArray[], int MaxNum,
int *pNum)
{
char * pcStrTemp = 0;
unsigned int uiIndex = 0;
pcStrTemp = pcStr;
while (pcStrTemp)
{
pstArray[uiIndex] = pcStrTemp;
pcStrTemp = strchr(pcStrTemp, cFlag);
if (pcStrTemp)
{
*pcStrTemp = '\0';
pcStrTemp ++;
uiIndex ++;
}
if (uiIndex >= MaxNum)
{
break;
}
}
if (0 != MaxNum)
{
*pNum = uiIndex >= MaxNum ? (MaxNum - 1) : uiIndex;
}
else
{
*pNum = 0;
}
return 0;
}
int MyLineReader(char * pstrLine, int uiBufferLen, void * pvData)
{
printf("Read line:[%s]\r\n", pstrLine);
char *pArray[8] = {0};
int Num = 0;
int index = 0;
StringSplit(pstrLine, ' ', pArray, 8, &Num);
for (index = 0; index <= Num; index ++)
{
printf("Get value :[%s]\r\n", pArray[index]);
}
return 0;
}
int main(int argc, char * argv[])
{
int ret = 0;
if (argc != 2)
{
printf("Please input file to read.\r\n");
return 0;
}
ret = ReadConfigFileEx(argv[1], MyLineReader, NULL, AUTO_FILTER_NEWLINDE);
if (ret)
{
printf("Open file error.\r\n");
}
return 0;
}

Cannot understand why program crashes after a couple iterations of while loop?

I am writing the following program to parse the text file (attached) but it keeps crashing after couple of iterations of while loop or it seems that the buffer storing file contents is corrupted somehow?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char * pick_question(char *, char, int);
char * print_answer(char *, char, int);
int no_of_questions(char*, char);
void clear();
int debug = 0;
int main(int argc, char* argv[])
{
system("cmd /c chcp 1252");
// system("cmd /c chcp 65001");
if (argc < 2)
{
perror("Please enter a filename!\n");
exit(0);
}
if (argc > 2)
{
debug = atoi(argv[2]);
}
char const* const fileName = argv[1];
FILE* file = fopen(fileName, "r");
if (!file)
{
perror("Unable to read file!\n");
exit(0);
}
else
{
if (debug == 1)
{
printf("File opened successfully\n");
}
}
static char *buffer;
int fileSz;
fseek(file, 0, SEEK_END);
fileSz = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = (char*) malloc((fileSz + 1) * sizeof(char));
if (!buffer)
{
perror("Unable to allocate buffer!");
exit(0);
}
fread(buffer, sizeof(char), fileSz, file);
while (1)
{
time_t t;
srand((unsigned) time(&t));
int sub = rand() % 5 + 1;
char del;
switch (sub)
{
case 1:
del = 'A';
break;
case 2:
del = 'B';
break;
case 3:
del = 'C';
break;
case 4:
del = 'D';
break;
case 5:
del = 'E';
}
int nrOfQues = no_of_questions(buffer, del);
if (nrOfQues == 0)
{
perror("main(): no_of_questions() returned 0. Unsupported text structure in file or incorrect file encoding!");
fclose(file);
exit(0);
}
int qNo = rand() % nrOfQues + 1;
char *ques = pick_question(buffer, del, qNo);
if (ques)
{
printf("\n\n");
puts(ques);
printf("\n\n");
}
else
{
perror("main(): pick_question() returned NULL. Unsupported text structure in file!");
fclose(file);
exit(0);
}
printf("\n\n");
printf("Do you want to see the answer(y/n)?");
char ans, repeat;
scanf("%c", &ans);
if ( ans == 'Y' || ans == 'y')
{
char *ans = print_answer(buffer, del, qNo);
if (ans)
{
printf("\n\n");
puts(ans);
printf("\n\n");
}
else
{
printf("\n\n");
perror("main(): print_answer() returned NULL. Unsupported text structure in file!");
fclose(file);
exit(0);
}
}
printf("Do you want to try more questions (y/n)?");
clear();
scanf("%c", &repeat);
if (repeat == 'N' || repeat == 'n')
{
break;
}
clear();
}
printf("\n\n");
printf("******** Thank you for using TULE Master! ********");
printf("\n\n");
fclose(file);
return 0;
}
char * pick_question(char * buffer, char sub, int qNo)
{
char tmpBuff[20];
char tmpBuff2[20];
const char * searchStr = "FRÅGA";
const char * searchStr2 = "A 1 SVAR:";
const char * searchStr3 = "*****************************************";
char *pStr, *currPos, *nStr, *tmpStr, *tmpStr2;
currPos = buffer;
int count = snprintf(tmpBuff, 20, "FRÅGA %c %d", sub, qNo);
if (count >= 0 || count < 20)
{
if (debug)
{
printf("tmpBuff is %s\n", tmpBuff);
}
currPos = strstr(currPos, tmpBuff);
if (currPos)
{
pStr = currPos;
nStr = currPos + 1;
nStr = strstr(nStr, searchStr);
if (!nStr)
{
nStr = currPos;
nStr = strstr(nStr, searchStr2);
if (!nStr)
{
printf("pick_qestion(): nStr is NULL. Unsupported "
"text structure");
return NULL;
}
}
// Check if it is a scenario based question
count = snprintf(tmpBuff2, 20, "FRÅGA %c %d", sub, qNo-1);
if (count >= 0 || count < 20)
{
tmpStr = strstr(buffer, tmpBuff2);
tmpStr2 = strstr(tmpStr, searchStr3);
if (tmpStr < tmpStr2 && tmpStr2 < pStr)
{
pStr = tmpStr2;
}
}
int qLen = nStr - pStr;
char *ques = malloc(sizeof(char) * (qLen+1));
snprintf(ques,qLen,"%s", pStr);
return ques;
}
else
{
printf("pick_qestion(): string \"FRÅGA\" not found in file!");
return NULL;
}
}
printf("pick_qestion(): snprintf was not successful!");
return NULL;
}
char * print_answer(char * buffer, char sub, int qNo)
{
char tmpBuff[20];
char *pStr, *currPos, *nStr;
int count = snprintf(tmpBuff, 20, "%c %d SVAR:", sub, qNo);
if (count >= 0 || count < 20)
{
currPos = strstr(buffer, tmpBuff);
if (!currPos)
{
printf("print_answer(): string \"SVAR\" not found in file!");
}
pStr = currPos;
nStr = currPos + 1;
char tmpBuff2[20];
int count = snprintf(tmpBuff2, 20, "%c %d SVAR:", sub, qNo+1);
if (count < 0 || count >= 20)
{
printf("print_answer(): snprint was not successful!");
return NULL;
}
nStr = strstr(nStr, tmpBuff2);
if (!nStr)
{
nStr = buffer + strlen(buffer);
}
int ansLen = nStr - pStr;
char *ans = malloc(sizeof(char) * (ansLen+1));
snprintf(ans, ansLen, "%s", pStr);
return ans;
}
printf("print_answer(): snprint was not successful!");
return NULL;
}
int no_of_questions(char *buffer, char sub)
{
char tmpBuff[20];
char *currPos, *pStr;
int count = snprintf(tmpBuff, 20, "FRÅGA %c", sub);
if (count >= 0 || count < 20)
{
if (debug)
{
printf("tmpBuff is %s\n", tmpBuff);
}
currPos = strstr(buffer, tmpBuff);
while (currPos != NULL)
{
pStr = currPos;
currPos = currPos + 1;
currPos = strstr(currPos, tmpBuff);
}
if (pStr != buffer)
{
pStr += 9;
char tmpBuff2[20];
memcpy(tmpBuff2, pStr, 2);
if (debug)
{
printf("No. of questions for %c DEL is are %d\n", sub,
atoi(tmpBuff2));
}
return atoi(tmpBuff2);
}
return 0;
}
return 0;
}
void clear()
{
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
}
This is the file that is given as input to the program:
Link

how to implement next() PHP function in C

I am tried to implement next() PHP function in C. if I have
The different to my implementation is I want to do this work with more one points. For example:
if I have two char * like:
char * a = "ac\0";
char * b = "bd\0";
and I call:
printf("%c", cgetnext(a));
printf("%c", cgetnext(b));
printf("%c", cgetnext(a));
printf("%c", cgetnext(b));
gets an output like: abcd
but I get abab
here is my code:
`#include <string.h>
#include <stdlib.h>
typedef struct
{
int pLocation;
int myindex;
int lastIndex;
} POINTERINFORMATION;
int __myIndex = 0;
int pointersLocationsLength = 0;
char * temparr = NULL;
POINTERINFORMATION pointersLocations[256];
int
plAdd (int p)
{
if (pointersLocationsLength >= sizeof(pointersLocations)) {
return -1;
} else {
pointersLocations[pointersLocationsLength].pLocation = p;
pointersLocations[pointersLocationsLength].lastIndex = 0;
pointersLocationsLength ++;
return pointersLocationsLength;
}
}
int
getPointer (int p, POINTERINFORMATION * out)
{
int i;
for (i = 0; i < pointersLocationsLength; i++)
{
if(pointersLocations[pointersLocationsLength].pLocation == p)
{
pointersLocations[i].myindex = i;
*out = pointersLocations[i];
return 1;
}
}
return 0;
}
void
getPointerIndex(char ** variable, int * val)
{
char * buf = malloc(256);
if(sprintf(buf,"%p", &variable) > 0){
*val = strtol(buf, NULL, 16 );
} else {
*val = -1;
}
}
int
inArrayOfPointers (int pointer)
{
POINTERINFORMATION pi;
return getPointer(pointer, &pi);
}
char
cgetnext(char * arr)
{
char * myarr;
const size_t size = sizeof(char *) + 1;
int pof;
myarr = malloc(size);
getPointerIndex (&arr, &pof);
if (inArrayOfPointers(pof)){
POINTERINFORMATION pi;
if (getPointer(pof, &pi))
{
myarr = (char *)*(int *)pi.pLocation;
__myIndex = pi.lastIndex;
++pointersLocations[pi.myindex].myindex;
} else {
return 0;
}
} else {
if (plAdd(pof) == -1) {
printf(" CANNOT ADD ELEMENT TO ARRAY\n");
exit(0);
} else {
myarr = arr;
__myIndex = 0;
}
}
if (strlen(myarr) == __myIndex) {
return 0;
} else {
temparr = malloc(size);
temparr = strdup(myarr);
return myarr[__myIndex];
}
}`
how to fix this? differents solutions for solve it are very apreciated! Thanks in advance.
If you are specifically interested in char* "arrays", then a simple solution might be to just increment the pointer. Note that if using dynamically allocated memory, you need to save the original pointer to free it. The same idea could be used for other data types as well.
This is an example of what I mean. cgetnext here just returns the character at the current position in the array and increments pointer (that is passed by address).
char cgetnext( char **p )
{
assert( p != NULL );
// check for end of string
if ( **p == '\0' )
return '\0';
return *(*p)++;
}
int main( int argc, char* argv[] )
{
char *a = "ac";
char *b = "bd";
printf( "%c", cgetnext(&a));
printf( "%c", cgetnext(&b));
printf( "%c", cgetnext(&a));
printf( "%c", cgetnext(&b));
}

Geting xml data using xml parser expat

I have managed to parse ok. But now I am having trouble getting the values
that I need. I can get the element and the attributes. But cannot get the values.
I would like to get the value of frame in this xml it is 20.
/* track the current level in the xml tree */
static int depth = 0;
/* first when start element is encountered */
void start_element(void *data, const char *element, const char **attribute)
{
int i;
for(i = 0; i < depth; i++)
{
printf(" ");
}
printf("%s", element);
for(i = 0; attribute[i]; i += 2)
{
printf(" %s= '%s'", attribute[i], attribute[i + 1]);
}
printf("\n");
depth++;
}
/* decrement the current level of the tree */
void end_element(void *data, const char *el)
{
depth--;
}
int parse_xml(char *buff, size_t buff_size)
{
FILE *fp;
fp = fopen("start_indication.xml", "r");
if(fp == NULL)
{
printf("Failed to open file\n");
return 1;
}
XML_Parser parser = XML_ParserCreate(NULL);
int done;
XML_SetElementHandler(parser, start_element, end_element);
memset(buff, 0, buff_size);
printf("strlen(buff) before parsing: %d\n", strlen(buff));
size_t file_size = 0;
file_size = fread(buff, sizeof(char), buff_size, fp);
/* parse the xml */
if(XML_Parse(parser, buff, strlen(buff), XML_TRUE) == XML_STATUS_ERROR)
{
printf("Error: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
}
fclose(fp);
XML_ParserFree(parser);
return 0;
}
<data>
<header length="4">
<item name="time" type="time">16</item>
<item name="ref" type="string">3843747</item>
<item name="port" type="int16">0</item>
<item name="frame" type="int16">20</item>
</header>
</data>
Output from parsing
Element: data
Element: header length= '4'
Element: item name= 'time' type= 'time'
Element: item name= 'ref' type= 'string'
Element: item name= 'port' type= 'int16'
Element: item name= 'frame' type= 'int16'
It is quite difficult with expat. expat is better when you are only interested with the structure, not the content of the elements. Why not using libxml instead? What are your reasons for using an even-based parser like expat, rather than a tree-based one?
Anyway, the way to do it is to set a character data handler. Here is an example, based on your code:
#include <expat.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 100000
/* track the current level in the xml tree */
static int depth = 0;
static char *last_content;
/* first when start element is encountered */
void
start_element(void *data, const char *element, const char **attribute)
{
int i;
for (i = 0; i < depth; i++) {
printf(" ");
}
printf("%s", element);
for (i = 0; attribute[i]; i += 2) {
printf(" %s= '%s'", attribute[i], attribute[i + 1]);
}
printf("\n");
depth++;
}
/* decrement the current level of the tree */
void
end_element(void *data, const char *el)
{
int i;
for (i = 0; i < depth; i++) {
printf(" ");
}
printf("Content of element %s was \"%s\"\n", el, last_content);
depth--;
}
void
handle_data(void *data, const char *content, int length)
{
char *tmp = malloc(length);
strncpy(tmp, content, length);
tmp[length] = '\0';
data = (void *) tmp;
last_content = tmp; /* TODO: concatenate the text nodes? */
}
int
parse_xml(char *buff, size_t buff_size)
{
FILE *fp;
fp = fopen("start_indication.xml", "r");
if (fp == NULL) {
printf("Failed to open file\n");
return 1;
}
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, start_element, end_element);
XML_SetCharacterDataHandler(parser, handle_data);
memset(buff, 0, buff_size);
printf("strlen(buff) before parsing: %d\n", strlen(buff));
size_t file_size = 0;
file_size = fread(buff, sizeof(char), buff_size, fp);
/* parse the xml */
if (XML_Parse(parser, buff, strlen(buff), XML_TRUE) == XML_STATUS_ERROR) {
printf("Error: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
}
fclose(fp);
XML_ParserFree(parser);
return 0;
}
int
main(int argc, char **argv)
{
int result;
char buffer[BUFFER_SIZE];
result = parse_xml(buffer, BUFFER_SIZE);
printf("Result is %i\n", result);
return 0;
}
The 'value' 20 is the character data "20" in the element whose tagname is "item" and whose name attribute is "frame".
To receive character data events, register a callback with the XML_SetCharacterDataHandler function.
This callback will receive the character data. The parser may split character data - typically to handle reaching the end of a buffer, or for entities (so for foo&bar your handler will get three calls - "foo", "&" and "bar"), so you have to paste the string parts together again if you need the whole of the data.
You know when you have all the character data inside a node when you receive the next element start or close callback.
When you have all the character data, you can process it.
A stand-alone example simplified from your code:
#include <expat.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
static const char* xml =
"<data>\n"\
" <header length=\"4\">\n"\
" <item name=\"time\" type=\"time\">16</item>\n"\
" <item name=\"ref\" type=\"string\">3843747</item>\n"\
" <item name=\"port\" type=\"int16\">0</item>\n"\
" <item name=\"frame\" type=\"int16\">20</item>\n"\
" </header>\n"\
"</data>\n";
void reset_char_data_buffer ();
void process_char_data_buffer ();
static bool grab_next_value;
void start_element(void *data, const char *element, const char **attribute) {
process_char_data_buffer();
reset_char_data_buffer();
if ( strcmp("item", element) == 0 ) {
size_t matched = 0;
for (size_t i = 0; attribute[i]; i += 2) {
if ( ( strcmp("name", attribute[i]) == 0 ) && ( strcmp("frame", attribute[i+1]) == 0 ) )
++matched;
if ( ( strcmp("type", attribute[i]) == 0 ) && ( strcmp("int16", attribute[i+1]) == 0 ) )
++matched;
}
if (matched == 2) {
printf("this is the element you are looking for\n");
grab_next_value = true;
}
}
}
void end_element(void *data, const char *el) {
process_char_data_buffer();
reset_char_data_buffer();
}
static char char_data_buffer[1024];
static size_t offs;
static bool overflow;
void reset_char_data_buffer (void) {
offs = 0;
overflow = false;
grab_next_value = false;
}
// pastes parts of the node together
void char_data (void *userData, const XML_Char *s, int len) {
if (!overflow) {
if (len + offs >= sizeof(char_data_buffer) ) {
overflow = true;
} else {
memcpy(char_data_buffer + offs, s, len);
offs += len;
}
}
}
// if the element is the one we're after, convert the character data to
// an integer value
void process_char_data_buffer (void) {
if (offs > 0) {
char_data_buffer[ offs ] = '\0';
printf("character data: %s\n", char_data_buffer);
if ( grab_next_value ) {
int value = atoi( char_data_buffer );
printf("the value is %d\n", value);
}
}
}
int main (void ) {
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, start_element, end_element);
XML_SetCharacterDataHandler(parser, char_data);
reset_char_data_buffer();
if (XML_Parse(parser, xml, strlen(xml), XML_TRUE) == XML_STATUS_ERROR)
printf("Error: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
XML_ParserFree(parser);
return 0;
}

Resources