By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

How to read string out of file with pointer?

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Xinyi Yang <ic***@slds1.de.lucent.com> wrote:
Hi,

I have to read information out of a file. The format will be
string1,string2,...,
string3,string4,...,
...
(the string sould not contain ' ' anyway) [ Please cut down your line length, text reformatted:]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.

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.
Nov 13 '05 #2

P: n/a

why don't you try sscanf() ?
--
Posted via http://dbforums.com
Nov 13 '05 #3

P: n/a
Xinyi Yang wrote:
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.


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

Nov 13 '05 #4

P: n/a
Xinyi Yang wrote:

Hi,

I have to read information out of a file. The format will be
string1,string2,...,
string3,string4,...,
...
(the string sould not contain ' ' anyway)


[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:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5

P: n/a
On Fri, 19 Sep 2003 08:26:35 -0400, dreamcatcher
<me*********@dbforums.com> wrote in comp.lang.c:

why don't you try sscanf() ?


....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
Nov 13 '05 #6

P: n/a
Joe Wright wrote:

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?
Yes, sth like that.
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.
I just want to read the section of characters out as a string and later
store them in a structure.
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?


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
Nov 13 '05 #7

P: n/a
dreamcatcher wrote:

why don't you try sscanf() ?


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

--
Xinyi Yang
Nov 13 '05 #8

P: n/a
Xinyi Yang <ic***@slds1.de.lucent.com> wrote in message news:<3F***************@slds1.de.lucent.com>...
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


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;
}
Nov 13 '05 #9

P: n/a
jo*******@my-deja.com (John Bode) wrote in message news:<43**************************@posting.google. com>...
Xinyi Yang <ic***@slds1.de.lucent.com> wrote in message news:<3F***************@slds1.de.lucent.com>...
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
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))

) /* oops */ /* 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++] = nextchar; /* oops^2 */

nextchar = fgetc (in); /* oops^3 */ }
p[cur_size] = 0; /* terminate the string */
}

return p;
}

Nov 13 '05 #10

P: n/a
jo*******@my-deja.com (John Bode) wrote in message news:<43**************************@posting.google. com>...
Xinyi Yang <ic***@slds1.de.lucent.com> wrote in message news:<3F***************@slds1.de.lucent.com>...
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
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))

) /* oops */ /* 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++] = nextchar; /* oops^2 */

nextchar = fgetc (in); /* oops^3 */ }
p[cur_size] = 0; /* terminate the string */
}

return p;
}

Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.