Connecting Tech Pros Worldwide Help | Site Map

How to read string out of file with pointer?

Xinyi Yang
Guest
 
Posts: n/a
#1: Nov 13 '05
Hi,

I have to read information out of a file. The format will be
string1,string2,...,
string3,string4,...,
....
(the string sould not contain ' ' anyway)
the size of each string is uncertain. I plan to create an array to contain the pointers to the strings. Yet I don't know how to read a string out from the file any let a pointer point to it. I thought about using char *p=(int *) (malloc(n*(sizeof(char)))), but it means I have to first read the string to get the length, then read again and save it to the array. Is there any better way?
I would not like to use array with certain length since I don't know how long could a string be.

Thanks in advance!

Xinyi
Irrwahn Grausewitz
Guest
 
Posts: n/a
#2: Nov 13 '05

re: How to read string out of file with pointer?


Xinyi Yang <icg_3@slds1.de.lucent.com> wrote:
[color=blue]
>Hi,
>
>I have to read information out of a file. The format will be
>string1,string2,...,
>string3,string4,...,
>...
>(the string sould not contain ' ' anyway)[/color]
[ Please cut down your line length, text reformatted:][color=blue]
>the size of each string is uncertain. I plan to create an array
>to contain the pointers to the strings. Yet I don't know how to
>read a string out from the file any let a pointer point to it.
>I thought about using char *p=(int *) (malloc(n*(sizeof(char)))),
>but it means I have to first read the string to get the length,
>then read again and save it to the array. Is there any better way?
>I would not like to use array with certain length since I don't
>know how long could a string be.
>[/color]
1. use a dynamically reallocated array of pointers-to-char

2. write a function, which
- reads one character at a time from the file
- store it in a dynamically reallocated character buffer
- terminates the buffer with '\0'
- assigns the pointer to the first character in the resulting
string to the next free place in the first array.

If you get stuck on the way, feel free to post your code (reduced to
the smallest posssible compilable version) here to exhibit your
particular problem.

Regards

Irrwahn
--
The generation of random numbers is too important to be left to chance.
dreamcatcher
Guest
 
Posts: n/a
#3: Nov 13 '05

re: How to read string out of file with pointer?



why don't you try sscanf() ?


--
Posted via http://dbforums.com
Morris Dovey
Guest
 
Posts: n/a
#4: Nov 13 '05

re: How to read string out of file with pointer?


Xinyi Yang wrote:
[color=blue]
> Hi,
>
> I have to read information out of a file. The format will be
> string1,string2,...,
> string3,string4,...,
> ....
> (the string sould not contain ' ' anyway)
> the size of each string is uncertain. I plan to create an
> array to contain the pointers to the strings. Yet I don't know
> how to read a string out from the file any let a pointer
> point to it. I thought about using char *p=(int *)
> (malloc(n*(sizeof(char)))), but it means I have to first read
> the string to get the length, then read again and save it to
> the array. Is there any better way? I would not like to use
> array with certain length since I don't know how long could a
> string be.[/color]

Xinyi...

You might get some ideas from the code at:

http://www.iedu.com/mrd/c/tokenize.c and
http://www.iedu.com/mrd/c/tokfile.c

"Better way" depends much on your input data and program
objectives. The example code assumes that there is sufficient
memory available.
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Joe Wright
Guest
 
Posts: n/a
#5: Nov 13 '05

re: How to read string out of file with pointer?


Xinyi Yang wrote:[color=blue]
>
> Hi,
>
> I have to read information out of a file. The format will be
> string1,string2,...,
> string3,string4,...,
> ...
> (the string sould not contain ' ' anyway)[/color]

[snip]

Hi Xinyi,

Your short description of the file format looks to me like a text file
consisting of lines of fields of characters, separated by commas. That
might describe a database table file. Does it?

Text files don't generally contain strings. A string is a memory thing,
an array of char terminated by and including the '\0' (NUL) character.

It is crucial to understand everything about the input file in order to
read anything useful from it. Maybe you can give us more information?
--
Joe Wright mailto:joewwright@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jack Klein
Guest
 
Posts: n/a
#6: Nov 13 '05

re: How to read string out of file with pointer?


On Fri, 19 Sep 2003 08:26:35 -0400, dreamcatcher
<member39454@dbforums.com> wrote in comp.lang.c:
[color=blue]
>
> why don't you try sscanf() ?[/color]

....because it sucks?

For some definitions of "suck", such as poor error recovery, potential
for undefined behavior, complexity of use, and extreme difficulty in
using correctly.

Yes, sucks.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Xinyi Yang
Guest
 
Posts: n/a
#7: Nov 13 '05

re: How to read string out of file with pointer?


Joe Wright wrote:
[color=blue]
>
> Hi Xinyi,
>
> Your short description of the file format looks to me like a text file
> consisting of lines of fields of characters, separated by commas. That
> might describe a database table file. Does it?[/color]

Yes, sth like that.
[color=blue]
> Text files don't generally contain strings. A string is a memory thing,
> an array of char terminated by and including the '\0' (NUL) character.[/color]

I just want to read the section of characters out as a string and later
store them in a structure.
[color=blue]
> It is crucial to understand everything about the input file in order to
> read anything useful from it. Maybe you can give us more information?[/color]

Well, this text file contains sections of characters from 0-9, a-z, A-Z.
Each section is seperated by commas. The length of the section is unknown.
And now I would like to get the section as a string and later store a pointer
of the string in an array. That's it.

Thanks!



--
Xinyi Yang
Xinyi Yang
Guest
 
Posts: n/a
#8: Nov 13 '05

re: How to read string out of file with pointer?


dreamcatcher wrote:[color=blue]
>
> why don't you try sscanf() ?[/color]

Because the section is seperated with ',' , but not space. So if you use sscanf(), it will get the whole text for you.

--
Xinyi Yang
John Bode
Guest
 
Posts: n/a
#9: Nov 13 '05

re: How to read string out of file with pointer?


Xinyi Yang <icg_3@slds1.de.lucent.com> wrote in message news:<3F6ADD69.AD4B26BB@slds1.de.lucent.com>...[color=blue]
> Hi,
>
> I have to read information out of a file. The format will be
> string1,string2,...,
> string3,string4,...,
> ...
> (the string sould not contain ' ' anyway)
> the size of each string is uncertain. I plan to create an array to contain the pointers to the strings. Yet I don't know how to read a string out from the file any let a pointer point to it. I thought about using char *p=(int *) (malloc(n*(sizeof(char)))), but it means I have to first read the string to get the length, then read again and save it to the array. Is there any better way?
> I would not like to use array with certain length since I don't know how long could a string be.
>
> Thanks in advance!
>
> Xinyi[/color]

The way I'd attack this is to create a dynamic buffer of size X using
malloc(), then reading a single character at a time into the dynamic
buffer, extending it as necessary using realloc(), until you see
either a ',' or EOF.

Quick, dirty, untested example:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#define START_SIZE 20 /* Initial buffer size */
#define EXTENT 5 /* Size to extend buffer by */

char *nextToken (FILE *in)
{
char *tmp, *p;
int phys_size = START_SIZE, cur_size = 0;
int nextchar;

/*
** Note: Do not cast the return value of malloc() unless
** you are using a pre-C89 compiler.
*/
p = malloc (START_SIZE);

if (p)
{
/*
** consume any leading whitespace
*/
while (isspace(nextchar = fgetc (in))
/* empty loop */;

while (nextchar != ',' && nextchar != EOF)
{
if (cur_size == phys_size - 1)
{
/*
** We've hit the end of the buffer.
** Use realloc() to extend it by
** EXTENT characters. For this example,
** a realloc() failure is a fatal error
** and we exit the program immediately.
** With a little effort you could make
** this handle such an error more gracefully.
*/
tmp = realloc (p, phys_size + EXTENT);
if (!tmp)
{
fprintf (stderr, "Buffer extend failed! Fatal
error!\n");
fflush (stderr);
exit (0);
}
phys_size += EXTENT;
p = tmp;
}
p[cur_size++] = c;
}
p[cur_size] = 0; /* terminate the string */
}

return p;
}
John Bode
Guest
 
Posts: n/a
#10: Nov 13 '05

re: How to read string out of file with pointer?


john_bode@my-deja.com (John Bode) wrote in message news:<43618c0e.0309240610.57ce3614@posting.google. com>...[color=blue]
> Xinyi Yang <icg_3@slds1.de.lucent.com> wrote in message news:<3F6ADD69.AD4B26BB@slds1.de.lucent.com>...[color=green]
> > Hi,
> >
> > I have to read information out of a file. The format will be
> > string1,string2,...,
> > string3,string4,...,
> > ...
> > (the string sould not contain ' ' anyway)
> > the size of each string is uncertain. I plan to create an array to contain the pointers to the strings. Yet I don't know how to read a string out from the file any let a pointer point to it. I thought about using char *p=(int *) (malloc(n*(sizeof(char)))), but it means I have to first read the string to get the length, then read again and save it to the array. Is there any better way?
> > I would not like to use array with certain length since I don't know how long could a string be.
> >
> > Thanks in advance!
> >
> > Xinyi[/color]
>
> The way I'd attack this is to create a dynamic buffer of size X using
> malloc(), then reading a single character at a time into the dynamic
> buffer, extending it as necessary using realloc(), until you see
> either a ',' or EOF.
>
> Quick, dirty, untested example:
>
> #include <stdio.h>
> #include <ctype.h>
> #include <stdlib.h>
>
> #define START_SIZE 20 /* Initial buffer size */
> #define EXTENT 5 /* Size to extend buffer by */
>
> char *nextToken (FILE *in)
> {
> char *tmp, *p;
> int phys_size = START_SIZE, cur_size = 0;
> int nextchar;
>
> /*
> ** Note: Do not cast the return value of malloc() unless
> ** you are using a pre-C89 compiler.
> */
> p = malloc (START_SIZE);
>
> if (p)
> {
> /*
> ** consume any leading whitespace
> */
> while (isspace(nextchar = fgetc (in))[/color]
) /* oops */[color=blue]
> /* empty loop */;
>
> while (nextchar != ',' && nextchar != EOF)
> {
> if (cur_size == phys_size - 1)
> {
> /*
> ** We've hit the end of the buffer.
> ** Use realloc() to extend it by
> ** EXTENT characters. For this example,
> ** a realloc() failure is a fatal error
> ** and we exit the program immediately.
> ** With a little effort you could make
> ** this handle such an error more gracefully.
> */
> tmp = realloc (p, phys_size + EXTENT);
> if (!tmp)
> {
> fprintf (stderr, "Buffer extend failed! Fatal
> error!\n");
> fflush (stderr);
> exit (0);
> }
> phys_size += EXTENT;
> p = tmp;
> }
> p[cur_size++] = c;[/color]
p[cur_size++] = nextchar; /* oops^2 */

nextchar = fgetc (in); /* oops^3 */[color=blue]
> }
> p[cur_size] = 0; /* terminate the string */
> }
>
> return p;
> }[/color]
John Bode
Guest
 
Posts: n/a
#11: Nov 13 '05

re: How to read string out of file with pointer?


john_bode@my-deja.com (John Bode) wrote in message news:<43618c0e.0309240610.57ce3614@posting.google. com>...[color=blue]
> Xinyi Yang <icg_3@slds1.de.lucent.com> wrote in message news:<3F6ADD69.AD4B26BB@slds1.de.lucent.com>...[color=green]
> > Hi,
> >
> > I have to read information out of a file. The format will be
> > string1,string2,...,
> > string3,string4,...,
> > ...
> > (the string sould not contain ' ' anyway)
> > the size of each string is uncertain. I plan to create an array to contain the pointers to the strings. Yet I don't know how to read a string out from the file any let a pointer point to it. I thought about using char *p=(int *) (malloc(n*(sizeof(char)))), but it means I have to first read the string to get the length, then read again and save it to the array. Is there any better way?
> > I would not like to use array with certain length since I don't know how long could a string be.
> >
> > Thanks in advance!
> >
> > Xinyi[/color]
>
> The way I'd attack this is to create a dynamic buffer of size X using
> malloc(), then reading a single character at a time into the dynamic
> buffer, extending it as necessary using realloc(), until you see
> either a ',' or EOF.
>
> Quick, dirty, untested example:
>
> #include <stdio.h>
> #include <ctype.h>
> #include <stdlib.h>
>
> #define START_SIZE 20 /* Initial buffer size */
> #define EXTENT 5 /* Size to extend buffer by */
>
> char *nextToken (FILE *in)
> {
> char *tmp, *p;
> int phys_size = START_SIZE, cur_size = 0;
> int nextchar;
>
> /*
> ** Note: Do not cast the return value of malloc() unless
> ** you are using a pre-C89 compiler.
> */
> p = malloc (START_SIZE);
>
> if (p)
> {
> /*
> ** consume any leading whitespace
> */
> while (isspace(nextchar = fgetc (in))[/color]
) /* oops */[color=blue]
> /* empty loop */;
>
> while (nextchar != ',' && nextchar != EOF)
> {
> if (cur_size == phys_size - 1)
> {
> /*
> ** We've hit the end of the buffer.
> ** Use realloc() to extend it by
> ** EXTENT characters. For this example,
> ** a realloc() failure is a fatal error
> ** and we exit the program immediately.
> ** With a little effort you could make
> ** this handle such an error more gracefully.
> */
> tmp = realloc (p, phys_size + EXTENT);
> if (!tmp)
> {
> fprintf (stderr, "Buffer extend failed! Fatal
> error!\n");
> fflush (stderr);
> exit (0);
> }
> phys_size += EXTENT;
> p = tmp;
> }
> p[cur_size++] = c;[/color]
p[cur_size++] = nextchar; /* oops^2 */

nextchar = fgetc (in); /* oops^3 */[color=blue]
> }
> p[cur_size] = 0; /* terminate the string */
> }
>
> return p;
> }[/color]
Closed Thread