473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to parse the line

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!

Nov 14 '05 #1
9 1584
QQ wrote:
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?

--
Sean
Nov 14 '05 #2


QQ wrote:
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

Nov 14 '05 #3
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!

Nov 14 '05 #4


QQ wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #5
On 6 Jun 2005 13:48:40 -0700, "QQ" <ju****@yahoo.com> wrote:
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.
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!


Nov 14 '05 #6


QQ wrote:
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

Nov 14 '05 #7


QQ wrote:
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;
}

Nov 14 '05 #8
On 6 Jun 2005 19:26:25 -0700, "David Resnick" <ln********@gmail.com>
wrote:


QQ wrote:
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.

Nov 14 '05 #9


sc******@oz.net wrote:
On 6 Jun 2005 19:26:25 -0700, "David Resnick" <ln********@gmail.com>
wrote:


QQ wrote:
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

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
22
by: Illya Havsiyevych | last post by:
Hello How easily parse VB/VBA code by VB/VBA code ? Is any ready solutions ? Thank's, illya
22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
5
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.