473,383 Members | 1,925 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,383 software developers and data experts.

Question about fgets.

Tcc
Hi all,

Assume there are some data in a file "a.txt":

abc def 11<---------------------data in a.txt

is it possible for me to use "fgets" function to get the string "abc", "def"
and "11" individulely?
and How?

if can't, what function can I use in order to solve this problem?

Thanks.
Nov 14 '05 #1
3 1321
"Tcc" <as**@yahoo.com> wrote in message
news:cj***********@news.hgc.com.hk...
Hi all,

Assume there are some data in a file "a.txt":

abc def 11<---------------------data in a.txt

is it possible for me to use "fgets" function to get the string "abc", "def" and "11" individulely?
No. By definition, 'fgets()' reads up to a newline character
or end-of-file (or error occurs), whichever comes first.
and How?

if can't, what function can I use in order to solve this problem?


'fscanf()'

Or use 'fgets()' to read a whole line into a string, then
use 'sscanf()' against that string to extract the individual
strings.

-Mike
Nov 14 '05 #2

"Tcc" <as**@yahoo.com> wrote

Assume there are some data in a file "a.txt":

abc def 11<---------------------data in a.txt

is it possible for me to use "fgets" function to get the string "abc", "def" and "11" individulely?
and How?
fgets() isn't very suitable, since it is designed to read whole lines
terminated by a newline.
if can't, what function can I use in order to solve this problem?

You can use fscanf(), or you can use fgets() to read the whole line, and
then call strtok() to parse the input.
Nov 14 '05 #3
In <Uk***************@newsread1.news.pas.earthlink.ne t> "Mike Wahler" <mk******@mkwahler.net> writes:
"Tcc" <as**@yahoo.com> wrote in message
news:cj***********@news.hgc.com.hk...
Hi all,

Assume there are some data in a file "a.txt":

abc def 11<---------------------data in a.txt

is it possible for me to use "fgets" function to get the string "abc","def"
and "11" individulely?


No. By definition, 'fgets()' reads up to a newline character
or end-of-file (or error occurs), whichever comes first.
and How?

if can't, what function can I use in order to solve this problem?


'fscanf()'


Right, except that it needs to be *carefully* used (otherwise it is
very easy to lose track of your actual position in the input stream).
Or use 'fgets()' to read a whole line into a string, then
use 'sscanf()' against that string to extract the individual
strings.


Doesn't work very well when the number of items on a line is not known
in advance. If it is, reading the whole line with fscanf and parsing it
with sscanf is the simplest solution.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #4

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

Similar topics

11
by: herrcho | last post by:
int get_lines(char *lines) { int n = 0; char buffer; puts("Enter one line at time; enter a blank when done"); while ((n < MAXLINES) && (gets(buffer) !=0 ) && (buffer != '\0')) { if ((lines...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
3
by: Roman Mashak | last post by:
Hello, All! Is there a common rule to allocate memory for buffer for using in fgets() ? For example: fgets(buf, SIZE, stdin); Is it mandatory to call malloc() before using fgets() ? With...
4
by: danu | last post by:
I'm trying to read data from a file (line by line) and trying to assign the values to the structure members. I'm kind of lost in this case since the books I'm using for reference doesn't give any...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
19
by: Zach Heath | last post by:
I'm just starting C and haven't done programming for a few years...could you guys please take a look at this? Thanks for your time! I have an input file that looks like: 1.5 2.5 Bob, Joe...
26
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a...
14
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str'...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.