Read Data from CSV and Put into Database - c

I have a table with 6 columns and i have CSV Export like this with 6 entries per Line
me;val1;val2;val3;val4;val5;
me;val1;val2;val3;val4;val5;
I need to read these entries and Insert it into the SQLITE3 Database.So inorder to parse the CSV I use
void readcsv()
{
FILE* stream = fopen("input.csv", "r");
char line[1024];
while (fgets(line, 1024, stream))
{
char* tmp = strdup(line);
printf("Field 3 would be %s\n", getcsvfield(tmp, 3));
// NOTE strtok clobbers tmp
free(tmp);
}
}
//Used for parsing CSV
const char* getcsvfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
So will i have to call getcsvfield(tmp, fieldnumber) again and again in the Insert Query like this or is there a cleaner and efficient way.
execute("INSERT INTO table(a,b,c,d,e,f) VALUES('%s','%s','%s');",getcsvfield(tmp, 1),getcsvfield(tmp, 2),....... and so on
Please advise.

A more efficient method would be to parse the line once, creating an array of pointers to the tokens. You can then pass the array entries to the execute function like this
execute("INSERT INTO table(a,b,c,d,e,f) VALUES('%s','%s','%s');",token[0],token[1],...
Here's an example that reads lines from a file, breaks the line into tokens, and then prints the tokens.
#include <stdio.h>
#include <string.h>
int getTokens( char *line, char *token[], int maxt )
{
int i;
char *ptr = strtok(line, ";\n");
for ( i = 0; i < maxt; i++ )
{
if ( ptr == NULL )
return i;
token[i] = ptr;
ptr = strtok( NULL, ";\n" );
}
return i;
}
int main( void )
{
const int maxt = 6;
char line[1024];
char *token[maxt];
FILE *fp;
if ( (fp = fopen( "input.csv", "r" )) == NULL )
return 1;
while ( fgets(line, sizeof line, fp) != NULL )
{
int count = getTokens( line, token, maxt );
for ( int i = 0; i < count; i++ )
printf( "%d: '%s'\n", i, token[i] );
printf( "\n" );
}
fclose( fp );
}

Related

Spliting a string into an array of strings completly dynamicly allocated

This question is really close to this to this topic but I prefer the lisibility and the pointers clarification I needed offered by this solution.
So I've got a data file and I get a very long array of char from it. I want to split this string into an array with, in each case, a string wich correspond to a line of this file.
I saw solutions but they all use limited arrays, since I don't know the lenght of each line, I really need to allocate all of them dynamicly but I can't find the lenght of the lines because strtokdoesn't put a null character \0at the end of each string.
What I've got for now is this two solutions but neither work:
int get_lines(char *file, char **lines) {
int nb_lines = 0;
char *token = strtok(file, "\n");
for(int i = 0; token != NULL; i++) {
token = strtok(NULL, "\n");
nb_lines = i;
}
nb_lines++;
lines = malloc((nb_lines + 1) * sizeof(char*));
lines[nb_lines] = '\0';
token = strtok(file, "\n");
for(int i = 0; token != NULL; i++) {
token = strtok(NULL, "\n");
int nb_char = 0;
for(int j = 0; token[j] != '\n'; j++) //This will cause SIGSEGV because strtok don't keep the '\n' at the end
nb_char = j;
nb_char++;
token[nb_char] = '\0'; //This cause SIGSEGV because token's allocation finish at [nb_char-1]
lines[i] = malloc(strlen(token) * sizeof(char)); //strlen cause SIGSEGV because I cannot place the '\0' at the end of token
printf("%s", token); //SIGSEGV because printf don't find the '\0'
lines[i] = token;
}
for(int i = 0; i < nb_lines; i++) {
printf("%s", lines[i]); //SIGSEGV
}
return nb_lines;
}
So you can see above the idea of what I want to do and why it doesn't work.
Below you will see an other try I made but I'm stuck at the same point:
int count_subtrings(char* string, char* separator) {
int nb_lines = 0;
char *token = strtok(string, separator);
for(int i = 0; token != NULL; i++) {
token = strtok(NULL, separator);
nb_lines = i;
}
return nb_lines + 1;
}
char** split_string(char* string, char* separator) {
char **sub_strings = malloc((count_subtrings(string, separator) + 1) * sizeof(char*));
for(int i = 0; string[i] != EOF; i++) {
//How to get the string[i] lenght to malloc them ?
}
}
My file is quite big and the lines can be too so I don't want to malloc an other table with a size of (strlen(file) + 1) * sizeof(char) to be sure each line won't SIGSEGV and I also find this solution quite dirty, if you guys had an other idea, I would be really happy.
(Sorry for the english mistakes, I'm not really good)
Your approach with strtok has two drawbacks: First, strtok modifies the string,so you can only pass the original string once. Second, it skips empty lines, because it tretas stretches of nelines as a single token separator.. (I don't know ehether that is a concern to you.)
You can countthe newlines with a single pass through the string. Allocate memory for your line array and make a second pass, where you split the string at newlines:
char **splitlines(char *msg)
{
char **line;
char *prev = msg;
char *p = msg;
size_t count = 0;
size_t n;
while (*p) {
if (*p== '\n') count++;
p++;
}
line = malloc((count + 2) * sizeof(*line));
if (line == NULL) return NULL;
p = msg;
n = 0;
while (*p) {
if (*p == '\n') {
line[n++] = prev;
*p = '\0';
prev = p + 1;
}
p++;
}
if (*prev) line[n++] = prev;
line[n++] = NULL;
return line;
}
I've allocated two more line pointers than the newlines count: One for the case that the last line doesn't end with a newline and another one to place a NULL sentinel at the end, so that you know where yourarray ends. (You could, of course, return the actual line count via a pointer to a size_t.)
the following proposed code:
cleanly compiles
(within the limits of the heap size) doesn't care about the input file size
echo's the resulting array of file lines, double spaced, just to show it worked. for single spacing, replace the puts() with printf()
and now the code
#include <stdio.h> // getline(), perror(), fopen(), fclose()
#include <stdlib.h> // exit(), EXIT_FAILURE, realloc(), free()
int main( void )
{
FILE *fp = fopen( "untitled1.c", "r" );
if( !fp )
{
perror( "fopen for reading untitled1.c failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
char **lines = NULL;
size_t availableLines = 0;
size_t usedLines = 0;
char *line = NULL;
size_t lineLen = 0;
while( -1 != getline( &line, &lineLen, fp ) )
{
if( usedLines >= availableLines )
{
availableLines = (availableLines)? availableLines*2 : 1;
char **temp = realloc( lines, sizeof( char* ) * availableLines );
if( !temp )
{
perror( "realloc failed" );
free( lines );
fclose( fp );
exit( EXIT_FAILURE );
}
// implied else realloc successful
lines = temp;
}
lines[ usedLines ] = line;
usedLines++;
line = NULL;
lineLen = 0;
}
fclose( fp );
for( size_t i = 0; i<usedLines; i++ )
{
puts( lines[i] );
}
free( lines );
}
Given the above code is in a file named: untitled1.c the following is the output.
#include <stdio.h> // getline(), perror(), fopen(), fclose()
#include <stdlib.h> // exit(), EXIT_FAILURE, realloc(), free()
int main( void )
{
FILE *fp = fopen( "untitled1.c", "r" );
if( !fp )
{
perror( "fopen for reading untitled1.c failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
char **lines = NULL;
size_t availableLines = 0;
size_t usedLines = 0;
char *line = NULL;
size_t lineLen = 0;
while( -1 != getline( &line, &lineLen, fp ) )
{
if( usedLines >= availableLines )
{
availableLines = (availableLines)? availableLines*2 : 1;
char **temp = realloc( lines, sizeof( char* ) * availableLines );
if( !temp )
{
perror( "realloc failed" );
free( lines );
fclose( fp );
exit( EXIT_FAILURE );
}
// implied else realloc successful
lines = temp;
}
lines[ usedLines ] = line;
usedLines++;
line = NULL;
lineLen = 0;
}
fclose( fp );
for( size_t i = 0; i<usedLines; i++ )
{
puts( lines[i] );
}
free( lines );
}

Read a large text file and quick sort alphabetically in C program

I can successfully show the text's words.
But I can't alphabetically sort.
How to text's words inset to character myArray = (char)malloc (size);
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static int compare (const void * a, const void * b);
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
int size = 0;
fp = fopen("data.txt", "r");
if (fp == NULL)
printf("\nFile unable to open ");
else
printf("\nFile opened ");
fseek(fp, 0, 2); /* file pointer at the end of file */
size = ftell(fp); /* take a position of file pointer un size variable */
//char *myArray = (char*)malloc(size * sizeof *myArray);
char *myArray = (char*)malloc(size);
static const char filename[] = "data.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL ){
int ch, word = 0, index= 0,index2 = 0;
while ( (ch = fgetc(file)) != EOF ){
if ( isspace(ch) || ispunct(ch) ){
if ( word ){
word = 0;
myArray[index++] = '\n';
//putchar('\n');
}
}else{
word = 1;
//putchar(ch);
myArray[index++] = ch;
index2++;
}
}
printf("%s", myArray);
fclose(file);
int i;
for(i = 0;i < sizeof(myArray);i++){
putchar(myArray[i]);
}
//qsort (array, 2, sizeof (const char *), compare);
//for (int i = 0; i < 2; i++) {
// printf ("%d: %s.\n", i, array[i]);
//}
}
}
static int compare (const void * a, const void * b)
{
return strcmp (*(const char **) a, *(const char **) b);
}
The qsort() call wants an array of pointers to character strings. So you would need to organize your words that way. For example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char myArray[] = "This\nis\na\nsample\ntext\nstring\nas\nthough\nfrom\nyour\nfile";
char **myWords;
//
// Compare two strings case insensitive
//
static int compare (const void * a, const void * b)
{
char *aStr, *bStr;
int i, retVal;
if ( !( aStr = strdup( *(const char **) a ) ) )
return 1;
if ( !( bStr = strdup( *(const char **) b ) ) ) {
free( aStr );
return -1;
}
for ( i=0; i<strlen( aStr ); i++ )
aStr[i] = (char)tolower( (int)aStr[i] );
for ( i=0; i<strlen( bStr ); i++ )
bStr[i] = (char)tolower( (int)bStr[i] );
retVal = strcmp( aStr, bStr );
free( aStr );
free( bStr );
return retVal;
}
int main ()
{
char *curWord, *nextWord;
long wordCount;
int i;
//
// myArray contains an array of newline delimited words as
// you have in your code after reading from the file
//
for ( i=0; i<strlen(myArray); i++ )
putchar( myArray[i] );
//
// If myArray is empty then no need to sort
//
if ( strlen( myArray ) == 0 ) {
printf( "Nothing to sort\n" );
return 0;
}
//
// Count how many newlines are in myArray - assume at least one
//
for ( wordCount=1, nextWord=myArray;
( nextWord = strchr( nextWord, (int)'\n' ) );
wordCount++, nextWord++ );
//
// If only one word then no need to sort
//
if ( wordCount == 1 ) {
printf ("\n0: %s.\n", myArray );
return 0;
}
//
// Allocate enough space to hold a new copy of each word
//
if ( !( myWords = calloc( wordCount, sizeof(char *) ) ) )
return 1;
//
// Load words from myArray into myWords
//
curWord = nextWord = myArray;
i = 0;
while ( ( nextWord = strchr( nextWord, (int)'\n' ) ) ) {
*nextWord++ = '\0';
myWords[i] = strdup( curWord );
if ( !myWords[i] ) {
printf( "\nFile too big for memory\n" );
return 1;
}
i++;
curWord = nextWord;
}
myWords[i] = curWord;
//
// Sort the array of words in myWords
//
qsort( myWords, wordCount, sizeof(char *), compare );
//
// Dump out sorted array
//
printf( "\n" );
for( i = 0; i < wordCount; i++ )
printf( "%d: %s.\n", i, myWords[i] );
//
// Free your memory here - but since exiting main() and
// program is ending no need to worry about it.
//
return 0;
}

Parsing csv with c

I have the following to parse a csv file, and it works fine until i changed
(getfield(tmp, 12));
to
(getfield(tmp, ver));
Is the problem in function declaration ?
here is the code :
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
int main() {
double frame_index[40][300];
int horz;
FILE* fp = fopen("output.txt", "r");
char line[1024];
for (int ver; ver<20;ver++) {
while (fgets(line, 1024, fp)) {
char* tmp = strdup(line);
frame_index[ver][horz] = atof(getfield(tmp, ver));
// works if "ver" was explicitly defined
printf("AA %f\n", frame_index[ver][horz]);
free(tmp);
horz++;
}
rewind(fp);
}
}
You never initialize the variable ver. For example you might want to change
for (int ver; ver<20;ver++)
to
for (int ver=0; ver<20;ver++)
Update The same with horz..

How to copy part of text file into a string in C?

I have a text file with:
recipName=Fork friend=Cup sonName=Spork feature=hair sendName=Spoon"
What I want to do is copy all the words before an = sign to one character array, and copy the stuff to the right of the = to another character array or string.
This is my code so far:
int main (int argc, char * argv[])
{
char data[100];
char line[5][100];
char key[5][100];
char value[5][100];
FILE * fdata = fopen(argv[1], "r"); //read data.txt file
FILE * ftemp = fopen(argv[2], "r"); //read and write to template.txt file
if (fdata == NULL)
{
printf("could not read file.\n");
}
int i = 0;
while (fgets(data, 100, fdata) != NULL)
{
printf("data: %s", data);
//this is where i get stuck, idk how to utilize this loop to copy the variable and variable names from the data.txt file i was given...thanks for the help
++i;
}
fclose(fdata);
fclose(ftemp);
return 0;
}
there might be some better functions that will do more of what you want in string.h
you will have to work out the logistics and count the number of '=' characters and decide how to handle that.
#include <string.h>
char *ptr1, *ptr2;
char tempstring[100];
char before[100];
char after[100];
/* you already have data[] filled... where you get stuck */
ptr1 = strchr( data, '=' ); /* find first occurence of = */
ptr2 = strrchr( data, '=' ); /* find last occurence of = */
if ( *ptr1 == '\0' )
{
/* did not find '=' print error message and stop */
}
if ( *ptr2 == '\0' )
{
/* did not find '=' print error message and stop */
}
/* below is what you are interested in */
strcpy( tempstr, data );
ptr1 = strchr( tempstr, '=' );
*ptr1 = '\0'; /* turn = into null */
strcpy( before, tempstr );
printf("everything before = character is %s\n", tempstr ); /* watch out if = is first character, nothing before it */
strcpy( tempstr, data );
ptr2 = strchr( tempstr, '=' );
ptr2++;
if ( *ptr2 != '\0' ) /* = might have been last character */
{
strcpy( after, tempstr );
printf("everything after = character is %s\n", tempstr );
}
so for the first strchr call,
before[] will have "recipName"
and after[] will have "Fork friend=Cup sonName=Spork feature=hair sendName=Spoon"
you can do a
sscanf( after, "%s", after2 );
to get just "Fork" into after2[] array assuming there will always be a space character separating things.
for chqrlie
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define MAX 5
int Count_Token ( char str[], char token )
{
char *p;
int cnt = 0;
/* str must be null terminated with '\0' otherwise infinite loop will happen */
p = str;
while ( *p != '\0' )
{
if ( *p == token )
cnt++;
p++;
}
return cnt;
}
void Get_KeyValue_at_Position( char str[], char token, int position, char k[], char v[] )
{
char *ptr;
char temp_str[100];
int i;
ptr = str;
/* assumes str[] passed in does not have leading space before first key */
/* this for loop puts ptr on start of key at position */
for ( i = 0; i < position; i++ }
{
/* move ptr to next space */
while (( *ptr != ' ' ) && ( *ptr != '\0' ))
ptr++;
/* account for more than one space separating key/value pairs and put ptr on beginning of next key */
while ( *ptr == ' ' )
ptr++;
}
/* get key */
strcpy( temp_str, ptr )
ptr_token = strchr( temp_str, token );
*ptr_token = '\0';
strcpy( k, tempstr );
/* get value */
strcpy( temp_str, ptr );
ptr_token = strchr( temp_str, token );
ptr_token++;
strcpy( v, ptr_token );
}
int main ( int argc, char * argv[] )
{
char data[100];
char key[MAX][100]; /* has string before token */
char value[MAX][100]; /* has string afte token */
FILE *fdata;
fdata = fopen(argv[1], "r"); //read data.txt file
if ( fdata == NULL )
{
printf("could not read file %s\n", argv[1] );
exit( 0 );
}
fgets( data, 100, fdata );
while ( ! feof( fdata ) )
{
printf("data: %s", data);
//this is where i get stuck, idk how to utilize this loop to copy the variable and variable names from the data.txt file i was given...thanks for the help
/* count how many token characters there are in data[] string */
num_token = Count_Token( data, '=' );
if ( num_token > MAX )
{
printf(" num token is %d, MAX is %d, stopping program.\n", num_token, MAX );
fclose( fdata );
exit( 0 );
}
for ( i = 0; i < num_token; i++ )
{
/* make sure i index does not exceend declaration size of key and value */
Get_KeyValue_at_Position( data, token, i, key[i], value[i] );
}
fgets( data, 100, fdata );
}
fclose(fdata);
return 0;
}

splitting the lines in a file and compare the array element

I have written the following code to split the lines, in which the strings are separated by symbol '#', and storing in the array named as'buf' here. This code is working fine if I am comparing any string present in the array[0] . but If I try to find the string present in array[3] for example here as 'admin', then it is having error as 'segmentation fault' . Could anyone help me to find why this code is not comparing any other argument.
I would be very thankful for this.
char **strsplit(const char* str, const char* delim, size_t* numtokens) {
char *s = strdup(str);
size_t tokens_alloc = 1;
size_t tokens_used = 0;
char **tokens = calloc(tokens_alloc, sizeof(char*));
char *token, *rest = s;
while ((token = strsep(&rest, delim)) != NULL) {
if (tokens_used == tokens_alloc) {
tokens_alloc *= 2;
tokens = realloc(tokens, tokens_alloc * sizeof(char*));
}
tokens[tokens_used++] = strdup(token);
}
if (tokens_used == 0) {
free(tokens);
tokens = NULL;
} else {
tokens = realloc(tokens, tokens_used * sizeof(char*));
}
*numtokens = tokens_used;
free(s);
return tokens;
}
The main() is as follows:
int main(void) {
char *line = NULL;
size_t linelen;
char **tokens;
size_t numtokens;
int ret=1;
FILE *ptr_file;
char buf[1000];
system("rm -rf input1.txt");
system("/www/stest/mds1 > input1.txt");
ptr_file =fopen("/www/stest/input1.txt","r");
if (!ptr_file)
return 1;
while (fgets(buf,1000, ptr_file)!=NULL)
{
tokens = strsplit(buf, "#\n", &numtokens);
size_t i;
for ( i = 0; i < numtokens; i++)
{
ret=strcmp(tokens[3],"member");
if(ret==0)
{
printf("%s",buf);
break;
}
}
fclose(ptr_file);
return 0;
return EXIT_SUCCESS;
}
Here is the output of the above code and I am having error if I want to print the lines which has 'member'as the third argument of the array.
abc#google.com#abdu#admin
abc#rediff.com#savya#cashier
abc#yahoo.com.com#andy#member
abc#google.com#susan#group
abc#facebook.com#andre#admin
Thanks.
I suspect what you want is something like this:
tokens = strsplit(buf, "#\n", &numtokens);
if(numtokens >= 4 && strcmp(tokens[3],"member") == 0)
{
printf("%s",buf);
}
As shown in George Houpis's answer it would also be good to double-check that strsplit does not return a null pointer.
This is not an answer, but the comment is not big enough. If you run this as your main, what is the output?
int main( int argc, char ** argv )
{
FILE * fin = fopen( "/www/stest/input1.txt", "rt" );
if( !fin )
return 1;
char buffer[1000];
while( fgets( buffer, 1000, fin ) )
{
size_t token_count;
char ** tokens = strsplit( buffer, "#\n", &token_count );
if( tokens )
{
size_t index;
for( index = 0; index < token_count; ++index )
if( !strcmp( tokens[index], "member" ) )
printf( "%d: %s | %s", index, tokens[index], buffer );
for( index = 0; index < token_count; ++index )
free( tokens[index] );
free( tokens );
}
}
fclose( fin );
return 0;
}
When I run with those 5 lines in the text file, I get:
3: member | abc#yahoo.com.com#andy#member
EDIT:
You can tokenize the string without malloc by keeping a second buffer and using pointers to that (still uses memory on the stack).
int main( int argc, char ** argv )
{
FILE * fin = fopen( argv[1], "rt" );
if( !fin )
return 1;
char buffer[1000];
while( fgets( buffer, 1000, fin ) )
{
char buffer_copy[1000];
strcpy( buffer_copy, buffer );
char * tokens[1000];
size_t token_count = 0;
char * ptr = tokens[token_count] = buffer_copy;
while( *ptr )
if( ( *ptr == '#' ) || ( *ptr == '\n' ) )
*ptr++ = '\0', tokens[++token_count] = ptr;
else
++ptr;
size_t index;
for( index = 0; index < token_count; ++index )
if( !strcmp( tokens[index], "member" ) )
printf( "%d: %s | %s", index, tokens[index], buffer );
}
fclose( fin );
return 0;
}
If you go with fixed position (always 3rd (0 indexed)), you can just scan and check:
int is_member( const char * buffer )
{
if( !buffer )
return 0;
size_t index;
const char * ptr = buffer;
for( index = 0; index < 3; ++index, ++ptr )
if( !( ptr = strchr( ptr, '#' ) ) )
return 0;
return !strncmp( ptr, "member", 6 ) && ( ( *( ptr + 7 ) == 0 ) || ( *( ptr + 7 ) == '#' ) || ( *( ptr + 7 ) == '\n' ) );
}
int main( int argc, char ** argv )
{
FILE * fin = fopen( argv[1], "rt" );
if( !fin )
return 1;
char buffer[1000];
while( fgets( buffer, 1000, fin ) )
if( is_member( buffer ) )
printf( "%s", buffer );
fclose( fin );
return 0;
}
Or if the position is not known, then you can find each token on the fly. This takes no significant additional memory (a couple pointers) and does not modify your buffer:
int is_member( const char * buffer, const char * delimiters )
{
size_t index;
const char * ptr = buffer, * eptr = buffer;
for( ; eptr; ptr = eptr + 1 )
// If the next delimiter is found and it is 6 characters away or not found, and the substring matches 'member', return 1.
if( ( !( eptr = strpbrk( ptr, delimiters ) ) || ( eptr - ptr == 6 ) ) &&
!strncmp( ptr, "member", 6 ) )
return 1;
return 0;
}
int main( int argc, char ** argv )
{
FILE * fin = fopen( argv[1], "rt" );
if( !fin )
return 1;
char buffer[1000];
while( fgets( buffer, 1000, fin ) )
if( is_member( buffer, "#\n" ) )
printf( "%s", buffer );
fclose( fin );
return 0;
}
I did it it without using file * since I have the buffer already. It is working. thanks.
char buffer_copy[1000];
strcpy( buffer_copy, buffer );
char * tokens[1000];
size_t token_count = 0;
char * ptr = tokens[token_count] = buffer_copy;
while( *ptr )
if( ( *ptr == '#' ) || ( *ptr == '\n' ) )
*ptr++ = '\0', tokens[++token_count] = ptr;
else
++ptr;
size_t index;
for( index = 0; index < token_count; ++index )
if( !strcmp( tokens[index], "member" ) )
printf( "%d: %s | %s", index, tokens[index], buffer );

Resources