473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how tp pass a stream into a structure

Hello,
I have to write a program that reads from a text file into a structure.
Every line in the file looks like this:
"1234","first.last","password"
I dont know if there is a function to pass the above line into a structure
emp{"1234","first.last","password"}.
If this is not the case, what is your suggestion to do the job?
Thanks in advance,

Uy

Nov 13 '05 #1
5 2646
"uydo(kiboga)" wrote:

Hello,
I have to write a program that reads from a text file into a structure.
Every line in the file looks like this:
"1234","first.last","password"
I dont know if there is a function to pass the above line into a structure
emp{"1234","first.last","password"}.
If this is not the case, what is your suggestion to do the job?


There is no pre-built function to do this task. You can
write one yourself, something like

struct emp line_to_emp(char *line) {
struct emp *new;
new = malloc(sizeof *new);
if (new != NULL) {
new->eins = duplicate(strtok(line, ",\""));
new->zwei = duplicate(strtok(NULL, ",\""));
new->drei = duplicate(strtok(NULL, ",\""));
if (new->eins == NULL || new->zwei == NULL
|| new->drei == NULL) {
free (new->eins);
free (new->zwei);
free (new->drei);
free (new);
new = NULL;
}
return new;
}

.... along with the "helper" function

char *duplicate(const char *old) {
char *new;
new = (old == NULL) ? NULL : malloc(strlen(old) + 1);
if (new != NULL)
strcpy (new, old);
return new;
}

Now, this is not necessarily the best way to proceed.
It may not even meet your needs -- you haven't stated
exactly what it is you're trying to do, and I'm just
making plausible guesses from one "like this" case. In
particular, this method assumes

- There are no embedded commas in the fields of
interest,

- You really do want to get rid of the quotation
marks,

- The strtok() function is not being used at a
higher level when line_to_emp() is called,

- It is permissible to modify the input line as
a by-product of filling in the struct.

If these assumptions are not met, you'll need to work
harder. Even if they *are* met, you may want to do things
differently anyhow. The number of different plausible
variations on the theme of "chop a line into fields" is
large -- and that may be one of the reasons that no pre-
built function exists to do the task.

<off-topic>

My use of `new' as an identifier is deliberate, in
keeping with my view that code should be either C or C++
but not "C/C++". Use a language, not a patois!

</off-topic>

--
Er*********@sun.com
Nov 13 '05 #2
Eric Sosman wrote:
[an error] struct emp line_to_emp(char *line) {


struct emp *line_to_emp( ...

--
Er*********@sun.com
Nov 13 '05 #3
Hi Eric,
Thanks for sharing with me. Your assumptions are right. What I tried to do
was to parse the string and
Any way, I found an easy way to deal with this. That is I used sscanf
function to parse the string into a formatted one for each line. Once I get
the formatted string, the remaining job is an easy part.
I'd appreciate it.
Uy

"Eric Sosman" <Er*********@sun.com> wrote in message
news:3F***************@sun.com...
Eric Sosman wrote:
[an error]

struct emp line_to_emp(char *line) {


struct emp *line_to_emp( ...

--
Er*********@sun.com

Nov 13 '05 #4
"uydo(kiboga)" wrote:

Hi Eric,
Thanks for sharing with me. Your assumptions are right. What I tried to do
was to parse the string and
Any way, I found an easy way to deal with this. That is I used sscanf
function to parse the string into a formatted one for each line. Once I get
the formatted string, the remaining job is an easy part.
I'd appreciate it.


A difficulty with using sscanf() for dividing input lines
into substrings is that you need to know how long the substrings
can be *before* you call sscanf(). For example, in

char field1[10], field2[10], field3[10];
sscanf (input, "%s %s %s", field1, field2, field3);

.... there will be Big Trouble if any of the input fields has
more than nine characters. If you're going to use sscanf()
this way on "uncontrolled" input, be sure to specify a length
limit for each "%s" conversion:

sscanf (input, "%9s %9s %9s", field1, field2, field3);

See Question 12.20 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com
Nov 13 '05 #5
I'll remember this.
Thanks again,
Uy
"Eric Sosman" <Er*********@sun.com> wrote in message
news:3F***************@sun.com...
"uydo(kiboga)" wrote:

Hi Eric,
Thanks for sharing with me. Your assumptions are right. What I tried to do was to parse the string and
Any way, I found an easy way to deal with this. That is I used sscanf
function to parse the string into a formatted one for each line. Once I get the formatted string, the remaining job is an easy part.
I'd appreciate it.


A difficulty with using sscanf() for dividing input lines
into substrings is that you need to know how long the substrings
can be *before* you call sscanf(). For example, in

char field1[10], field2[10], field3[10];
sscanf (input, "%s %s %s", field1, field2, field3);

... there will be Big Trouble if any of the input fields has
more than nine characters. If you're going to use sscanf()
this way on "uncontrolled" input, be sure to specify a length
limit for each "%s" conversion:

sscanf (input, "%9s %9s %9s", field1, field2, field3);

See Question 12.20 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com

Nov 13 '05 #6

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

Similar topics

3
by: Panda2 | last post by:
Say I have a structure such as: struct Barn{ char type; int number; }animal so for example we might have data like animal.type cow animal.number 10
8
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
6
by: yezi | last post by:
Hi: How to code for operation between 2 structures? The structure's defination is : struct sockaddr_in my_addr; I need call a subfunction and pass the value in my_addr to the subfuncition...
2
by: Steve Bottoms | last post by:
Is there any way to pass a login name/password when calling System.Web.Mail.SMTPMail? Can't find anything in the docs or KB... If not, any suggestions to get this functionality short of building...
2
by: prakashgkhaire | last post by:
i have two structure where first structure consists val and structure pointer(linklist), 2nd structure consists, val, a varialbe of first structure, pointer of structure. Now i found to pass the...
1
by: RGarg06 | last post by:
Hi, I am trying to read a buffer from a file and then pass that buffer to a C++ dll as follows. While debugging it tells me that byte array received is a Bad Ptr. Any help will be appreciated. ...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
5
by: gdarian216 | last post by:
can I pass grades.projects in my function call that is void get_scores(ifstream& infile, int num_scores, grades.projects) and the function would look like void get_scores(ifstream& infile, int...
8
by: Chetanhl | last post by:
Hey there in this program i am passing a unsigned char** from main fuction but stream to which is the argument is not taking value plz help figure out y? struct BinaryData*...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

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.