How to parse the line

Q

QQ

Hello I have a string like this
"213200","0000","9999","204-033-105"

but I need to seperate them to be
s1 = 213200;
s2 = 0000;
s3 = 9999;
s4 = 204-033-105;

Is there any good way to do it?
Thanks a lot!
 
F

Fao, Sean

QQ said:
Hello I have a string like this
"213200","0000","9999","204-033-105"

but I need to seperate them to be
s1 = 213200;
s2 = 0000;
s3 = 9999;
s4 = 204-033-105;

Is there any good way to do it?

Looks pretty trivial. Ever heard of CSV?
 
D

David Resnick

QQ said:
Hello I have a string like this
"213200","0000","9999","204-033-105"

but I need to seperate them to be
s1 = 213200;
s2 = 0000;
s3 = 9999;
s4 = 204-033-105;

Is there any good way to do it?
Thanks a lot!

There are many. They range from using strtok(with its many cavaets) to

sscanf to writing some sort of parser. Give one a try and post some
code if you need help getting it to work.

-David
 
Q

QQ

I wanna find the standard method.
the data file format is
"213201","0000","9999","123456789"
This is my code
main()
{
FILE *fp;
char A[6],B[4],C[4],D[11];
char *StrFile;
int readStatus,LineNum=0;
int i;
if((fp = fopen( "Data.txt", "r"))==NULL)
{
fprintf(stdout,"\t File Reading Error\n");
exit(1);
}
else
{
readStatus = fscanf(fp,"%30s",StrFile);
while( readStatus ==1)
{
LineNum++;
memcpy(&A,&StrFile[1],6);
memcpy(&B,&StrFile[10],4);
memcpy(&C,&StrFile[17],4);
memcpy(&D,&StrFile[24],11);
printf("A is %s B %s C %s D %s\n", A,B,C,D);
readStatus=fscanf(fp,"%s",StrFile);
}
fprintf(stdout,"LineNumber is %d\n",LineNum);
}

However for this program the output is
A is 719203 B 0000719203 C 99990000719203 D 111-222-333

What's wrong with B & C?
Thanks a lot!
 
E

Eric Sosman

QQ said:
I wanna find the standard method.
the data file format is
"213201","0000","9999","123456789"
This is my code
main()
{
FILE *fp;
char A[6],B[4],C[4],D[11];
char *StrFile;
int readStatus,LineNum=0;
int i;
if((fp = fopen( "Data.txt", "r"))==NULL)
{
fprintf(stdout,"\t File Reading Error\n");
exit(1);
}
else
{
readStatus = fscanf(fp,"%30s",StrFile);

BZZZT! FAQ, Question 7.1.
 
S

schwarzb

I wanna find the standard method.
the data file format is
"213201","0000","9999","123456789"
This is my code
main()
{
FILE *fp;
char A[6],B[4],C[4],D[11];
char *StrFile;
int readStatus,LineNum=0;
int i;
if((fp = fopen( "Data.txt", "r"))==NULL)
{
fprintf(stdout,"\t File Reading Error\n");
exit(1);
}
else
{
readStatus = fscanf(fp,"%30s",StrFile);

StrFile doesn't point to memory you own. In fact it doesn't point to
anything since you never initialized it. Undefined behavior.
while( readStatus ==1)
{
LineNum++;
memcpy(&A,&StrFile[1],6);

This guarantees that your four arrays do not contain strings since
there is no room in the arrays for the terminating \0.
memcpy(&B,&StrFile[10],4);
memcpy(&C,&StrFile[17],4);
memcpy(&D,&StrFile[24],11);
printf("A is %s B %s C %s D %s\n", A,B,C,D);

%s requires strings but you are not providing same. More undefined
behavior.
 
D

David Resnick

QQ said:
I wanna find the standard method.
the data file format is
"213201","0000","9999","123456789"
This is my code
main()

int main(void)
is the proper way to do this
{
FILE *fp;
char A[6],B[4],C[4],D[11];
char *StrFile;

StrFile points nowhere. It needs to have space allocated.
You thus need either to use malloc or have StrFile be
an array, as in
char *StrFile[31];
int readStatus,LineNum=0;
int i;
if((fp = fopen( "Data.txt", "r"))==NULL)
{
fprintf(stdout,"\t File Reading Error\n");
exit(1);
}
else
{
readStatus = fscanf(fp,"%30s",StrFile);

You read some bytes here into the garbage pointer. Not good. If you
were
lucky, your program would have crashed in a way that would have pointed
that
out.
while( readStatus ==1)
{
LineNum++;
memcpy(&A,&StrFile[1],6);
memcpy(&B,&StrFile[10],4);
memcpy(&C,&StrFile[17],4);
memcpy(&D,&StrFile[24],11);

If your input is very rigid, this is mostly OK. But you are not
creating NUL terminated strings. You could get around this by making
the buffers one bigger and preinitializing the last byte to NUL, as in
char A[7];
A[sizeof A - 1] = '\0';

Or you could change your printf to have
"A is %6s"...

But why does D get 11 instead of 9?
printf("A is %s B %s C %s D %s\n", A,B,C,D);
readStatus=fscanf(fp,"%s",StrFile);
}
fprintf(stdout,"LineNumber is %d\n",LineNum);
}

However for this program the output is
A is 719203 B 0000719203 C 99990000719203 D 111-222-333

What's wrong with B & C?
Thanks a lot!

Anyway, this approach is OK if the input is going to be as rigid as
above.
If it is going to be highly variable, with irritating stuff like
"\"foo\",,"bar,baz" then some sort of parsing code which keeps track
of the state and handles escapes and empty ,, type things is better.
Or if you are good with sscanf (I don't use it much) you could probably
write it that way...

-David
 
C

c_learner

QQ said:
Hello I have a string like this
"213200","0000","9999","204-033-105"

but I need to seperate them to be
s1 = 213200;
s2 = 0000;
s3 = 9999;
s4 = 204-033-105;

Is there any good way to do it?
Thanks a lot!

$ cat 1.txt
"213200","0000","9999","204-033-105"


#include <stdio.h>
#include <string.h>

int main()
{
FILE *fp;
char A[7]={0}, B[5]={0}, C[5]={0}, D[12]={0};
char StrFile[36];
int readStatus, LineNum = 0;
int i;

if((fp = fopen("1.txt","r")) == NULL)
{
fprintf(stdout,"\t File Reading Error\n");
exit(1);
}
else
{
readStatus = fscanf(fp,"%36s",StrFile);
printf("%d\n",readStatus);
while(readStatus == 1)
{
LineNum++;
memcpy(&A, &StrFile[1],6);
memcpy(&B, &StrFile[10],4);
memcpy(&C, &StrFile[17],4);
memcpy(&D, &StrFile[24], 11);

printf("A: %s B: %s C: %s D: %s \n", A,B,C,D);
readStatus = fscanf(fp, "%s",StrFile);
}
fprintf(stdout,"LineNumber is %d\n",LineNum);
}
return 0;
}
 
S

schwarzb

I wanna find the standard method.
the data file format is
"213201","0000","9999","123456789"
This is my code
main()

int main(void)
is the proper way to do this
{
FILE *fp;
char A[6],B[4],C[4],D[11];
char *StrFile;

StrFile points nowhere. It needs to have space allocated.
You thus need either to use malloc or have StrFile be
an array, as in
char *StrFile[31];

An extra * crept into your statement.
 
D

David Resnick

I wanna find the standard method.
the data file format is
"213201","0000","9999","123456789"
This is my code
main()

int main(void)
is the proper way to do this
{
FILE *fp;
char A[6],B[4],C[4],D[11];
char *StrFile;

StrFile points nowhere. It needs to have space allocated.
You thus need either to use malloc or have StrFile be
an array, as in
char *StrFile[31];

An extra * crept into your statement.

Sigh. I started to write char *StrFile = malloc(31), and ended up
writing the above wrongness when I realized he didn't need dynamic
allocation.

-David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top