How to copy part of text file into a string in C? - 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;
}

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;
}

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 );

Tokenizer not working

I am trying to tokenize a string to give an array of strings but it seems like my code is wrong.
Here is my code:
asmInstruction *tokenizeLine(char *charLine) {
int words = countTokens(charLine);
char *tokens = (char*) malloc(MAX_LINE_LENGTH);
asmInstruction *instr = (asmInstruction*) malloc(sizeof(asmInstruction*));
instr->args = (char**) malloc(MAX_LINE_LENGTH);
int count = 1;
tokens = strtok(charLine, " ,");
while (count <= words) {
tokens = strtok(NULL, " ,");
instr->args[count - 1] = (char*)malloc(MAX_LINE_LENGTH);
instr->args[count - 1] = tokens;
++count;
}
free(tokens);
return instr;
}
/* Reads a file and returns the number of lines in this file. */
uint32_t countLines(FILE *file) {
uint32_t lines = 0;
int32_t c;
while (EOF != (c = fgetc(file))) {
if (c == '\n') {
++lines;
}
}
/* Reset the file pointer to the start of the file */
rewind(file);
return lines;
}
And the structure:
typedef struct {
char **args; /* An array of strings*/
} asmInstruction;
My main is here:
int main() {
char s[] = "ldr r2,r1";
asmInstruction *instr = tokenizeLine(s);
printf("%s", instr->args[0]);
}
/* Counts the number of tokens in a line */
uint32_t countTokens(char line[]) {
/* The correct way to do this! */
uint32_t numberOfTokens = 0;
/* Split at spaces and commas */
char *tokens = strtok(line, " ,");
while (tokens != NULL) {
tokens = strtok(NULL, " ,");
numberOfTokens++;
}
return numberOfTokens;
}
So, this should print ldr.
However, it prints null.
If I loop over the tokens it doesn't print them out but null.
I'm expecting to print out the tokens
ldr
r2
r1
But only the first one gets printed out.
It seems like instr->args[count-1] never gets assigned something because apparently tokens hasn't been assigned something either.
Why is that?
Thanks.
the following code:
handles errors
has many/ most of the logic errors corrected
properly defines the struct asmInstruction
performs the functionality indicated in the question.
suggest elimination of the struct asmInstruction as it is not needed, just use a char** args = NULL; in the tokenizeLine() function and return args.
it is not necessary, nor desirable to malloc memory for 'tokens'. Because that memory pointer will be overlayed each time 'tokens' is set from the returned value from strtok()
If a malloc is done, then there will be a memory leak.
In the following code, there still needs to be some
additional logic for freeing malloc'd memory and for closing the file before calling 'exit( EXIT_FAILURE );'
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct asmInstruction
{
char **args; /* An array of strings*/
};
#define MAX_LINE_LENGTH (100)
// prototypes
uint32_t countTokens(char line[]);
uint32_t countLines(FILE *file);
struct asmInstruction *tokenizeLine(char *charLine);
int main( void )
{
char s[] = "ldr r2,r1";
struct asmInstruction *instr = tokenizeLine(s);
printf("%s", instr->args[0]);
return( 0 );
} // end function: main
/* Counts the number of tokens in a line */
uint32_t countTokens(char line[])
{
/* The correct way to do this! */
uint32_t numberOfTokens = 0;
/* Split at spaces and commas */
char *tokens = strtok(line, " ,");
while (tokens != NULL)
{
tokens = strtok(NULL, " ,");
numberOfTokens++;
}
return numberOfTokens;
} // end function: countTokens
struct asmInstruction *tokenizeLine(char *charLine)
{
int words = countTokens(charLine);
char *tokens = NULL;
struct asmInstruction *instr = NULL;
if( NULL == (instr = malloc(sizeof( struct asmInstruction)) ) )
{ // then malloc failed
perror( "malloc for struct asmInstruction failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
instr->args = NULL;
if( NULL == (instr->args = malloc(words*sizeof(char*)) ) )
{ // then malloc failed
perror( "malloc for array of char pointers failed:" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
memset( instr->args, '\0', words*sizeof(char*) );
int count = 0;
tokens = strtok(charLine, " ,");
while ( tokens )
{
if( NULL == (instr->args[count] = malloc(strlen(tokens)+1) ) )
{ // then, malloc failed
perror( "malloc for arg failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
strcpy(instr->args[count], tokens );
++count;
tokens = strtok(NULL, " ,");
} // end while
return instr;
} // end function: tokenizeLine
/* Reads a file and returns the number of lines in this file. */
uint32_t countLines(FILE *file)
{
uint32_t lines = 0;
int32_t c;
while (EOF != (c = fgetc(file)))
{
if (c == '\n') {
++lines;
}
}
/* Reset the file pointer to the start of the file */
rewind(file);
return lines;
} // end function: countLines
asmInstruction *tokenizeLine(char *charLine) {
int words = countTokens(charLine);
char *tokens;//don't need malloc for this, because just pointer holder.
asmInstruction *instr = (asmInstruction*) malloc(sizeof(asmInstruction));//allocate size isn't sizeof(asmInstruction*)
instr->args = (char**) malloc((words+1) * sizeof(char*));//+1 for NULL, or add member E.g instr->numOfWords = words
int count = 0;
tokens = strtok(charLine, " ,");
while (tokens) {
instr->args[count] = malloc(strlen(tokens)+1);
strcpy(instr->args[count++], tokens);
//or process for each line
//instr->args[count++] = tokens;//no need allocate for word
tokens = strtok(NULL, " ,");//get next tokens
}
instr->args[count] = NULL;//set sentinel
return instr;
}

Parsing a complex string in C

I have a String like this:
"00:00:00 000~00:02:00 0000|~00:01:00 0000;00:01:00 0000~",
I want to get each of the items like "00:00:00 000".
My idea is that first, split the string by ";", then split by "|", and finally split by "~".
But the problem is that I can't get it if it's null, such like "00:01:00 0000~", the part after "~", I wanna get it and set a default value to it then store it somewhere else, but the code doesn't work. What is the problem?
Here is my code:
int main(int argc, char *argv[])
{
char *str1, *str2, *str3, *str4, *token, *subtoken, *subt1, *subt2;
char *saveptr1, *saveptr2, *saveptr3;
int j;
for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
token = strtok_r(str1, ";", &saveptr1);
if (token == NULL)
break;
printf("%d: %s\n", j, token);
int flag1 = 1;
for (str2 = token; ; str2 = NULL) {
subtoken = strtok_r(str2, "|", &saveptr2);
if (subtoken == NULL)
break;
printf(" %d: --> %s\n", flag1++, subtoken);
int flag2 = 1;
for(str3 = subtoken; ; str3 = NULL) {
subt1 = strtok_r(str3, "~", &saveptr3);
if(subt1 == NULL) {
break;
}
printf(" %d: --> %s\n",flag2++, subt1);
}
}
}
exit(EXIT_SUCCESS);
} /* main */
You can simplify your algorithm if you first make all delimiters uniform. First replace all occurrences of , and | with ~, then the parsing will be easier. You can do this externally via sed or vim or programmatically in your C code. Then you should be able to get the 'NULL' problem easily. (Personally, I prefer not to use strtok as it modifies the original string).
It is indeed easier to just write a custom parser in this case.
The version below allocates new strings, If allocating new memory is not desired, change the add_string method to instead just point to start, and set start[len] to 0.
static int add_string( char **into, const char *start, int len )
{
if( len<1 ) return 0;
if( (*into = strndup( start, len )) )
return 1;
return 0;
}
static int is_delimeter( char x )
{
static const char delimeters[] = { 0, '~', ',', '|',';' };
int i;
for( i=0; i<sizeof(delimeters); i++ )
if( x == delimeters[i] )
return 1;
return 0;
}
static char **split( const char *data )
{
char **res = malloc(sizeof(char *)*(strlen(data)/2+1));
char **cur = res;
int last_delimeter = 0, i;
do {
if( is_delimeter( data[i] ) )
{
if( add_string( cur, data+last_delimeter,i-last_delimeter) )
cur++;
last_delimeter = i+1;
}
} while( data[i++] );
*cur = NULL;
return res;
}
An example usage of the method:
int main()
{
const char test[] = "00:00:00 000~00:02:00 0000|~00:01:00 0000;00:01:00 0000~";
char **split_test = split( test );
int i = 0;
while( split_test[i] )
{
fprintf( stderr, "%2d: %s\n", i, split_test[i] );
free( split_test[i] );
i++;
}
free( split_test );
return 0;
}
Instead of splitting the string, it might be more suitable to come up with a simple finite state machine that parses the string. Fortunately, your tokens seem to have an upper limit on their length, which makes things a lot easier:
Iterate over the string and distinguish four different states:
current character is not a delimiter, but previous character was (start of token)
current character is a delimiter and previous character wasn't (end of token)
current and previous character are both not delimiters (store them in temporary buffer)
current and previous character are both delimiters (ignore them, read next character)
It should be possible to come up with a very short (10 lines?) and concise piece of code that parses the string as specified.

Resources