473,796 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read string out of file with pointer?

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
Nov 13 '05
10 10254
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;
}

Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2498
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){
19
2084
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...
1
4311
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.
5
2930
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. ...
3
4018
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...
9
5212
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;
1
9364
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);
3
8911
by: sam | last post by:
same as subject?
6
2287
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 ) {}
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
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...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7548
isladogs
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...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
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...
1
4118
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.