Hi all and thanks a lot in advance, what am trying to acomplish here is that am reading a file with ansi C, this file contains text and for each line contains a string like this case:
andreuga|460325945878913024|Y sorry por los que no querían pero ITESO ahí te voy|1398585232|0|0
and what am already doing is that am reading that file.txt and split that string into this output:
res[0] = andreuga
res[1] = 460325945878913024
res[2] = Y sorry por los que no querÝan pero ITESO ahÝ te voy
res[3] = 1398585232
res[4] = 0
res[5] = 0
res[6] = (null)
so what i want to do is read the file and split the string while am reading each line and the save that value to a struct so later i can use that struct and insert into a database with another function i have. But my main issue is the spliting the string while reading each line from the file. This is the code:
#include <string.h>
int main(){
char str[]= "andreuga|460325945878913024|Y sorry por los que no querían pero ITESO ahí te voy|1398585232|0|0";
char ** res = NULL;
char * p = strtok (str, "|");
int n_spaces = 0, i;
/* split string and append tokens to 'res' */
while (p) {
res = realloc (res, sizeof (char*) * ++n_spaces);
if (res == NULL)
exit (-1); /* memory allocation failed */
res[n_spaces-1] = p;
p = strtok (NULL, "|");
}
/* realloc one extra element for the last NULL */
res = realloc (res, sizeof (char*) * (n_spaces+1));
res[n_spaces] = 0;
/* print the result */
for (i = 0; i < (n_spaces+1); ++i)
printf ("res[%d] = %s\n", i, res[i]);
/* free the memory allocated */
free (res);
return 0;
}
///////////////////////OUTPUT:
res[0] = andreuga
res[1] = 460325945878913024
res[2] = Y sorry por los que no querÝan pero ITESO ahÝ te voy
res[3] = 1398585232
res[4] = 0
res[5] = 0
res[6] = (null)
Also to mention that in here i have an failed attempt to join the two codes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "libpq-fe.h"
#define LONG_MAX_LINEA 1024
#define NOM_ARCHIVO "twitsDB.txt"
typedef struct tweet
{
int IDTweet;
char IDCreator[20];
char IDSentimentAnalysis[20];
char HashTag[141];
char Content[141];
char CreationDate[30]; // time.h!!!
char Retweet[20];
char Favorites[20];
}Tweet;
int main(void)
{
FILE *entrada;
char linea[LONG_MAX_LINEA];
char str[200];
char ** res = NULL;
char * p = strtok (str, "|");
int n_spaces = 0, i;
int j = 0;
printf("\n programa para leer una archivo");
printf("\n-------------------------------");
if ((entrada = fopen(NOM_ARCHIVO, "r")) == NULL){
perror(NOM_ARCHIVO);
return EXIT_FAILURE;
}
//Tweet tweet1;
while (fgets(linea, LONG_MAX_LINEA, entrada) != NULL)
{
printf("%d %s", j,linea);
//
str[j] = linea;
/* split string and append tokens to 'res' */
while (p) {
res = realloc (res, sizeof (char*) * ++n_spaces);
if (res == NULL)
exit (-1); /* memory allocation failed */
res[n_spaces-1] = p;
p = strtok (NULL, "|");
}
/* realloc one extra element for the last NULL */
res = realloc (res, sizeof (char*) * (n_spaces+1));
res[n_spaces] = 0;
/* print the result */
for (i = 0; i < (n_spaces+1); ++i)
printf ("res[%d] = %s\n", i, res[i]);
/* free the memory allocated */
free (res);
//
// strcpy(tweet1.Content, "prueba tweet");
// strcpy(tweet1.IDCreator, "1");
// strcpy(tweet1.Favorites, "1");
// strcpy(tweet1.Retweet, "1");
// strcpy(tweet1.CreationDate, "2014-01-01");
// strcpy(tweet1.HashTag, "1");
// strcpy(tweet1.IDSentimentAnalysis, "1");
//insertTweet(tweet1);
// llenar la struct tweet
j++;
break;
}
fclose(entrada);
puts("eso es todo el archivo");
return EXIT_SUCCESS;
}
Hope i get to the point if not am constantly cheking for a better explanation. Cheers.