J
Jase Schick
Hi all,
I have written the following code:
/* strtok example */
#include <stdio.h>
#include <string.h>
static const char * const resultFileName = "param.txt";
int main ()
{
FILE *fPtr;
char str[300];
char * pch;
int len;
char savedstr[300];
fPtr =fopen(resultFileName, "r");
if(fPtr == NULL)
{
printf("File doesnot exist");
return 1;
}
else
{
while(!feof(fPtr))
{
fgets(str,288,fPtr);
len = strlen(str);
str[len-1]=0;
for ( (pch=strtok(str,","));(pch = strtok(NULL,","));(pch
!=NULL))
{
strncpy(savedstr,pch,len);
printf("Saved String =%s\n",savedstr);
}
}
}
fclose(fPtr);
return 0;
}
I have a text file like the following:
param.txt:
"ram","lax","deepak"
10,20,30
40,80,100
70,90,100
and so on...
When I use strtok(delim ,) the problem is that the string "ram" never
gets stored in savedstr array while from "lax" onwards it gets stored.
1. Could any body suggest a better method so that I can store "ram"
also in the saved string and tokenize based on comma separated
delimiter?
2. Is there anything in C like (map in C++) so that I can store:
(key)
"ram"---->10,40,70(In an array of integer values)
"lax"---->20,80,90
Could anybody help me in this regard?
Best:
Jase
--
For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him shall not perish, but have eternal life. For
God did not send the Son into the world to judge the world, but that the
world might be saved through Him. He who believes in Him is not judged;
he who does not believe has been judged already, because he has not
believed in the name of the only begotten Son of God.
( John 3:16 )
I have written the following code:
/* strtok example */
#include <stdio.h>
#include <string.h>
static const char * const resultFileName = "param.txt";
int main ()
{
FILE *fPtr;
char str[300];
char * pch;
int len;
char savedstr[300];
fPtr =fopen(resultFileName, "r");
if(fPtr == NULL)
{
printf("File doesnot exist");
return 1;
}
else
{
while(!feof(fPtr))
{
fgets(str,288,fPtr);
len = strlen(str);
str[len-1]=0;
for ( (pch=strtok(str,","));(pch = strtok(NULL,","));(pch
!=NULL))
{
strncpy(savedstr,pch,len);
printf("Saved String =%s\n",savedstr);
}
}
}
fclose(fPtr);
return 0;
}
I have a text file like the following:
param.txt:
"ram","lax","deepak"
10,20,30
40,80,100
70,90,100
and so on...
When I use strtok(delim ,) the problem is that the string "ram" never
gets stored in savedstr array while from "lax" onwards it gets stored.
1. Could any body suggest a better method so that I can store "ram"
also in the saved string and tokenize based on comma separated
delimiter?
2. Is there anything in C like (map in C++) so that I can store:
(key)
"ram"---->10,40,70(In an array of integer values)
"lax"---->20,80,90
Could anybody help me in this regard?
Best:
Jase
--
For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him shall not perish, but have eternal life. For
God did not send the Son into the world to judge the world, but that the
world might be saved through Him. He who believes in Him is not judged;
he who does not believe has been judged already, because he has not
believed in the name of the only begotten Son of God.
( John 3:16 )