How to read string out of file with pointer?

X

Xinyi Yang

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
 
I

Irrwahn Grausewitz

Xinyi Yang said:
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
 
M

Morris Dovey

Xinyi said:
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.
 
J

Joe Wright

Xinyi said:
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?
 
J

Jack Klein

X

Xinyi Yang

Joe said:
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!
 
X

Xinyi Yang

dreamcatcher said:
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.
 
J

John Bode

Xinyi Yang said:
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;
}
 
J

John Bode

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;
}
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top