473,320 Members | 2,109 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,320 software developers and data experts.

Reading words from a text file

I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1);
while (fscanf(fd, fmt, buf) != EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?
Thanks,
Mattia
Apr 9 '08 #1
9 2638
mattia <ge****@gmail.comwrites:
I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1);
while (fscanf(fd, fmt, buf) != EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?
Define "best"?! It is one way (I'd test fscanf(...) == 1 rather != EOF
but that is a small matter) but only you know if it is the best for
your purpose.

You can embed macro into a format string using the # operator and
string literal concatenation ("%" "32" "s" is the same literal as
"%32s"). But that requires an extra function-like macro and adding 1
when the macro is used to define the array. I think the details are
in the FAQ.

Personally, I'd just write a 'int get_word(FILE *fp, char *buf, size_t
size);' function. It is not hard, and you'll use it 1000 (and
re-write it 10) times before you retire. It is odd, but C seems to
encourage this sort of build your own private tool-kit approach.

--
Ben.
Apr 9 '08 #2
mattia wrote:
I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1);
while (fscanf(fd, fmt, buf) != EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?
You shouldn't use "%d" with a size_t value and you
should use fclose instead of close, but those are easily
fixed. I'd worry about the (non-Standard) strdup function
returning a NULL, and I'd probably write use_word(buf) and
leave it up to use_word to decide whether to make a copy,
but that's probably not what you're asking about.

An alternative (I don't say it's better or worse, just
that there are other ways):

#define MAXWORD 99
#define STRING(x) STRING_HELPER(x)
#define STRING_HELPER(x) #x
...
char buf[MAXWORD+1];
while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
...

--
Eric Sosman
es*****@ieee-dot-org.invalid
Apr 9 '08 #3
On Wed, 09 Apr 2008 08:29:07 -0400, Eric Sosman wrote:
mattia wrote:
>I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?

You shouldn't use "%d" with a size_t value and you
should use fclose instead of close, but those are easily fixed. I'd
worry about the (non-Standard) strdup function returning a NULL, and I'd
probably write use_word(buf) and leave it up to use_word to decide
whether to make a copy, but that's probably not what you're asking
about.

An alternative (I don't say it's better or worse, just
that there are other ways):

#define MAXWORD 99
#define STRING(x) STRING_HELPER(x)
#define STRING_HELPER(x) #x
...
char buf[MAXWORD+1];
while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
...
Great, I'll use this trick, thanks.

--
__mattia__
Apr 9 '08 #4
I just hope you free the reference to the string in use_word
On Apr 9, 12:14*pm, mattia <ger...@gmail.comwrote:
I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1);
while (fscanf(fd, fmt, buf) != EOF)
* * * * use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?
Thanks,
Mattia
Apr 9 '08 #5
On 09 Apr 2008 13:21:21 GMT, mattia <ge****@gmail.comwrote:
>On Wed, 09 Apr 2008 08:29:07 -0400, Eric Sosman wrote:
>mattia wrote:
>>I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?

You shouldn't use "%d" with a size_t value and you
should use fclose instead of close, but those are easily fixed. I'd
worry about the (non-Standard) strdup function returning a NULL, and I'd
probably write use_word(buf) and leave it up to use_word to decide
whether to make a copy, but that's probably not what you're asking
about.

An alternative (I don't say it's better or worse, just
that there are other ways):

#define MAXWORD 99
#define STRING(x) STRING_HELPER(x)
#define STRING_HELPER(x) #x
...
char buf[MAXWORD+1];
while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
...

Great, I'll use this trick, thanks.
You do realize that any use of *scanf to read a string will stop at
the first white space character. Since you mentioned a function
called use_word, maybe you really do want to stop at the space that
terminates the current word.

However if you want to read a string that contains embedded blanks,
you may find fgets more suitable.
Remove del for email
Apr 9 '08 #6
On Wed, 09 Apr 2008 08:20:44 -0700, Barry Schwarz wrote:
On 09 Apr 2008 13:21:21 GMT, mattia <ge****@gmail.comwrote:
>>On Wed, 09 Apr 2008 08:29:07 -0400, Eric Sosman wrote:
>>mattia wrote:
I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?

You shouldn't use "%d" with a size_t value and you
should use fclose instead of close, but those are easily fixed. I'd
worry about the (non-Standard) strdup function returning a NULL, and
I'd probably write use_word(buf) and leave it up to use_word to decide
whether to make a copy, but that's probably not what you're asking
about.

An alternative (I don't say it's better or worse, just
that there are other ways):

#define MAXWORD 99
#define STRING(x) STRING_HELPER(x)
#define STRING_HELPER(x) #x
...
char buf[MAXWORD+1];
while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
...

Great, I'll use this trick, thanks.

You do realize that any use of *scanf to read a string will stop at the
first white space character. Since you mentioned a function called
use_word, maybe you really do want to stop at the space that terminates
the current word.

However if you want to read a string that contains embedded blanks, you
may find fgets more suitable.
Remove del for email
Actually I've got to read the words used in /usr/share/dict/words

--
__mattia__
Apr 9 '08 #7
On Wed, 09 Apr 2008 08:10:43 -0700, st****@gmail.com wrote:
I just hope you free the reference to the string in use_word On Apr 9,
12:14Â*pm, mattia <ger...@gmail.comwrote:
>I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
EOF)
Â* Â* Â* Â* use_word(strdup(buf));

close(fd);

Is it the best way? What do you think? Thanks,
Mattia
Hmn, actually no, maybe it is better if I call use_word(buf) and then
decide to copy the buffer using strdup(), then I'll free the pointer
returned.

--
__mattia__
Apr 9 '08 #8
Barry Schwarz wrote:
mattia <ge****@gmail.comwrote:
>Eric Sosman wrote:
>>mattia wrote:

I've came up with this:

FILE *fd;
char buf[100];
char fmt[10];

/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1);
while (fscanf(fd, fmt, buf) != EOF)
use_word(strdup(buf));

close(fd);

Is it the best way? What do you think?

You shouldn't use "%d" with a size_t value and you should use
fclose instead of close, but those are easily fixed. I'd worry
about the (non-Standard) strdup function returning a NULL, and
I'd probably write use_word(buf) and leave it up to use_word to
decide whether to make a copy, but that's probably not what
you're asking about.

An alternative (I don't say it's better or worse, just
that there are other ways):

#define MAXWORD 99
#define STRING(x) STRING_HELPER(x)
#define STRING_HELPER(x) #x
...
char buf[MAXWORD+1];
while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
...

Great, I'll use this trick, thanks.

You do realize that any use of *scanf to read a string will stop
at the first white space character. Since you mentioned a
function called use_word, maybe you really do want to stop at
the space that terminates the current word.

However if you want to read a string that contains embedded
blanks, you may find fgets more suitable.
He might consider using ggets() instead, which returns an allocated
buffer that you can use until you 'free' it. Written in standard C,
and fully portable. See:

<http://cbfalconer.home.att.net/download/>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Apr 10 '08 #9
On Wed, 09 Apr 2008 21:07:40 -0400, CBFalconer wrote:
Barry Schwarz wrote:
>mattia <ge****@gmail.comwrote:
>>Eric Sosman wrote:
mattia wrote:

I've came up with this:
>
FILE *fd;
char buf[100];
char fmt[10];
>
/* open fd */
sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
EOF)
use_word(strdup(buf));
>
close(fd);
>
Is it the best way? What do you think?

You shouldn't use "%d" with a size_t value and you should use fclose
instead of close, but those are easily fixed. I'd worry about the
(non-Standard) strdup function returning a NULL, and I'd probably
write use_word(buf) and leave it up to use_word to decide whether to
make a copy, but that's probably not what you're asking about.

An alternative (I don't say it's better or worse, just that there are
other ways):

#define MAXWORD 99
#define STRING(x) STRING_HELPER(x)
#define STRING_HELPER(x) #x
...
char buf[MAXWORD+1];
while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
...

Great, I'll use this trick, thanks.

You do realize that any use of *scanf to read a string will stop at the
first white space character. Since you mentioned a function called
use_word, maybe you really do want to stop at the space that terminates
the current word.

However if you want to read a string that contains embedded blanks, you
may find fgets more suitable.

He might consider using ggets() instead, which returns an allocated
buffer that you can use until you 'free' it. Written in standard C, and
fully portable. See:

<http://cbfalconer.home.att.net/download/>
Great, thanks.

--
Mattia
Apr 10 '08 #10

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

Similar topics

2
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
12
by: dough | last post by:
I want to read in lines from a file and then seperate the words so i can do a process on each of the words. Say the text file "readme.txt" contains the following: In the face of criticism from...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
3
by: FayeC | last post by:
I have the following code: <?php $username = $_POST; $myFile = "clientlist.txt"; $fp = fopen($myFile, 'r'); $content = fread( $fp, filesize( $myFile ) ); $inputString = $username; $arrFp =...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
1
by: ychen020 | last post by:
Hi all, I'm trying to read in a config file (text file) that looks something like this: %Number1 Paragraph with words. Paragraph with words. Paragraph with words.
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
1
by: shrimpy | last post by:
hi every one, i am new to python, and coz i want to write a handy command for my linux machine, to find a word in all the files which are under the current folder. the code is half done, but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.