Thread: [Dev-C++] newbie string parsing question
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <wor...@so...> - 2004-08-17 19:30:40
|
Hi all, Thank you in advance for any help that you can offer. I have a text file that has records like this: Firstname Lastname age //seperated by the \t character. I want to run a while loop and parse the record to populate a struct array. I have looked into the getline(string, max, delimiter) function but it needs char[]'s. Since I don't know the length of the string that I need can someone recommend an existing different class function or something? Best regards, Dane |
|
From: Matt V. <m-v...@on...> - 2004-08-17 23:15:48
|
Look up the fscanf() function.
pseudo-code:
#include <stdio.h>
char* first_name;
char* last_name;
int age;
FILE* in_file;
infile = fopen("data.txt","r");
while ( !eof(infile) ) {
fscanf( infile, "%s\t%s\t%d\r", first_name, last_name, &age);
// do something with the first_name, last_name, and age variables here.
}
close(infile);
I think that's right -- might want to double check all of the pointers
and stuff (the * and & operators). I'm just typing this off the top of
my head so double check the syntax.
-Matt
wor...@so... wrote:
> Hi all,
>
> Thank you in advance for any help that you can offer.
>
> I have a text file that has records like this:
>
> Firstname Lastname age //seperated by the \t character.
>
> I want to run a while loop and parse the record to populate a struct array.
> I have looked into the getline(string, max, delimiter) function but it needs char[]'s.
> Since I don't know the length of the string that I need can someone recommend
> an existing different class function or something?
>
> Best regards,
>
> Dane
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
> 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
> Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
> http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Per W. <pw...@ia...> - 2004-08-17 23:53:28
|
Note 1: fscanf will not allocate any memory. first_name and last_name must
already point to large enough memory buffers.
Note 2: that scanf functions are a common road of buffer overflow attacks.
If one of the names in the string is very long, it will overwrite
unallocated memory. Whenever strings are loaded, a maximum field width
should be specified in the formatting string!
One other common method of loading text lines using C, i.e. without use of
the string data type, is to load each text line with
fgets(buf,sizeof(buf),file)
and then use sscanf() on a sanitized string, or use strchr(), strtok() or
similar to find the separator character(s) between the tokens.
/Per W
On Tue, 17 Aug 2004, Matt Valerio wrote:
> Look up the fscanf() function.
>
> pseudo-code:
> #include <stdio.h>
>
> char* first_name;
> char* last_name;
> int age;
> FILE* in_file;
> infile = fopen("data.txt","r");
> while ( !eof(infile) ) {
> fscanf( infile, "%s\t%s\t%d\r", first_name, last_name, &age);
> // do something with the first_name, last_name, and age variables here.
> }
> close(infile);
>
>
> I think that's right -- might want to double check all of the pointers
> and stuff (the * and & operators). I'm just typing this off the top of
> my head so double check the syntax.
>
> -Matt
>
>
>
>
> wor...@so... wrote:
>
> > Hi all,
> >
> > Thank you in advance for any help that you can offer.
> >
> > I have a text file that has records like this:
> >
> > Firstname Lastname age //seperated by the \t character.
> >
> > I want to run a while loop and parse the record to populate a struct array.
> > I have looked into the getline(string, max, delimiter) function but it needs char[]'s.
> > Since I don't know the length of the string that I need can someone recommend
> > an existing different class function or something?
> >
> > Best regards,
> >
> > Dane
> >
> >
> > -------------------------------------------------------
> > SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
> > 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
> > Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
> > http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
> 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
> Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
> http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Scott S. <age...@co...> - 2004-08-18 00:06:04
|
This is probably the smartest solution. I was going to suggest C
functions. Also, you can include <cstdio> instead of stdio.h. That will
include all of the C headers in namespace std.
Per Westermark wrote:
>Note 1: fscanf will not allocate any memory. first_name and last_name must
>already point to large enough memory buffers.
>
>Note 2: that scanf functions are a common road of buffer overflow attacks.
>If one of the names in the string is very long, it will overwrite
>unallocated memory. Whenever strings are loaded, a maximum field width
>should be specified in the formatting string!
>
>One other common method of loading text lines using C, i.e. without use of
>the string data type, is to load each text line with
> fgets(buf,sizeof(buf),file)
>and then use sscanf() on a sanitized string, or use strchr(), strtok() or
>similar to find the separator character(s) between the tokens.
>
>/Per W
>
>On Tue, 17 Aug 2004, Matt Valerio wrote:
>
>
>
>>Look up the fscanf() function.
>>
>>pseudo-code:
>>#include <stdio.h>
>>
>>char* first_name;
>>char* last_name;
>>int age;
>>FILE* in_file;
>>infile = fopen("data.txt","r");
>>while ( !eof(infile) ) {
>> fscanf( infile, "%s\t%s\t%d\r", first_name, last_name, &age);
>> // do something with the first_name, last_name, and age variables here.
>>}
>>close(infile);
>>
>>
>>I think that's right -- might want to double check all of the pointers
>>and stuff (the * and & operators). I'm just typing this off the top of
>>my head so double check the syntax.
>>
>>-Matt
>>
>>
>>
>>
>>wor...@so... wrote:
>>
>>
>>
>>>Hi all,
>>>
>>>Thank you in advance for any help that you can offer.
>>>
>>>I have a text file that has records like this:
>>>
>>>Firstname Lastname age //seperated by the \t character.
>>>
>>>I want to run a while loop and parse the record to populate a struct array.
>>>I have looked into the getline(string, max, delimiter) function but it needs char[]'s.
>>>Since I don't know the length of the string that I need can someone recommend
>>>an existing different class function or something?
>>>
>>>Best regards,
>>>
>>>Dane
>>>
>>>
>>>-------------------------------------------------------
>>>SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
>>>100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
>>>Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
>>>http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
>>>_______________________________________________
>>>Dev-cpp-users mailing list
>>>Dev...@li...
>>>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>>
>>>
>>>
>>-------------------------------------------------------
>>SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
>>100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
>>Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
>>http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
>>
>
>
>
>-------------------------------------------------------
>SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
>100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
>Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
>http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
|
|
From: Daniel K. O. <dan...@ya...> - 2004-08-19 01:49:15
|
wor...@so... wrote:
>I have looked into the getline(string, max, delimiter) function but it needs char[]'s.
>Since I don't know the length of the string that I need can someone recommend
>an existing different class function or something?
>
>
You can use the getline() template, like this:
ifstream the_file(...);
string the_line;
...
while (getline(the_file, the_line))
do something;
Daniel K. O.
|