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*(size of(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 10 10253
Xinyi Yang <ic***@slds1.de .lucent.com> wrote: Hi,
I have to read information out of a file. The format will be string1,string 2,..., string3,string 4,..., ... (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*(size of(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.
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*(size of(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
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.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
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
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
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*(size of(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(nextch ar = 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;
} jo*******@my-deja.com (John Bode) wrote in message news:<43******* *************** ****@posting.go ogle.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*(size of(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(nextch ar = 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; } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: yo_mismo |
last post by:
Hi everyone,
I'm trying to read the first line of a file this way:
....
....
....
....
new_line=0;
while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){
if (strcmp(&info, "\n") != 0){
|
by: Mark Richards |
last post by:
I've been programming for many years, but have only recently taken a
deep "C" dive (bad pun, i know) and need a lot of explanation from an
expert. My questions center around those mysterious pointer beasties.
The following code is excerpted from digitemp, a program that reads
1-wire devices. Digitemp is the work of Brian C. Lane.
As I walk through this code, I have a number of questions. I'm hoping
someone with experience in...
|
by: Magix |
last post by:
Hi,
I have these string data: str_data1, str_data2, str_data3, which capture
some value after a routine process A. Then I would like to write (append)
these 3 string values into a text file each time after routine process A,
the text file is named "mytext.dat" in following format with "#####" as
separator.
The maximum entries of them is 5. When reaching the fifth entry, it will
delete the very first entry.
|
by: Pete |
last post by:
I having a problem reading all characters from a file. What I'm trying to
do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want
to && with a mask 0xFF000000 then >> right shift 24 bits storing in result
then printing the result.
I thing a while or for loop is needed but I'm not quite sure how to go about
it. How do I step through each character in this case and store it for use
and passing to another function.
...
|
by: Dave Coate |
last post by:
Hello again,
I am going to re-post a question. I got some excellent suggestions from
Rob and Mattias on this but their ideas did not solve the problem. Here is
the original post:
*****************************************************
I am looking for a way to 'override' file security and read the Owner of
a file to which I have no access. I am a system administrator, as such I
have administrative rights to all the computers in the...
| |
by: Adi |
last post by:
Hello eveyone,
I wanna ask a very simple question here (as it was quite disturbing
me for a long time.)
My problem is to read a file line by line. I've tried following
implementations but still facing problems:
Assume that
FILE* filePointer;
unsigned char lineBuffer;
|
by: Matrixinline |
last post by:
Hi All,
File Text.txt
Contains following text as :
"C:\program file\application data\details\app" "D:\Program File"
I tried to read that data as
fscanf(oFp, "%s %s", sCopyDirectory, sToDirectory);
|
by: sam |
last post by:
same as subject?
|
by: ma740988 |
last post by:
I'm perusing a question and curiosity got the best of me in part
because I cant solve the problem
Consider:
typedef unsigned int word_type ;
class foo {
public:
void set_mx_digits_1 ( word_type wt ) {}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |