473,800 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file access - design thoughts.

Hi, I'm hoping to get some advice on this design query to see if I'm
heading in the right direction.

I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.

Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?

Jay

May 20 '07 #1
22 1327
J.*******@gmail .com wrote:
Hi, I'm hoping to get some advice on this design query to see if I'm
heading in the right direction.

I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.

Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?
If portability isn't an issue, you will probably be better of using your
operating system's means of memory mapping the file and simply using the
result as an array.

--
Ian Collins.
May 20 '07 #2

<J.*******@gmai l.comwrote in message
news:11******** *************@w 5g2000hsg.googl egroups.com...
Hi, I'm hoping to get some advice on this design query to see if I'm
heading in the right direction.

I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.

Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?
Google for ggets and Chuck Falconer.
Chuck, a reg in this group, has kindly provided exactly what you want. I
believe entirely rights free, certainly for nothing.

May 20 '07 #3
J.*******@gmail .com wrote:
>
Hi, I'm hoping to get some advice on this design query to see if I'm
heading in the right direction.

I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.

Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?
get_line uses getc to read lines, and realloc to size the array.
Truncates lines at (size_t)-1) bytes.

http://www.mindspring.com/~pfilandr/...ine/get_line.c

--
pete
May 20 '07 #4
J.*******@gmail .com wrote:
Hi, I'm hoping to get some advice on this design query to see if I'm
heading in the right direction.

I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.
Your problem, is well known for compiler writers, they call it lexical
analysis. fgets() is not normally used in hand-written lexer's, but
getchar() and ungetc() is, because you analyze single character at a time.
Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?
Yes, there is standard tool for this, called lex. Here is a lex
specification that print all the words in a file:

$ cat ex_lex.l
/*
Compile:
flex ex_lex.l
cc lex.yy.c -ll

Run:
../a.out < ex_lex.l
*/

%{
%}
%%

[^ \n\t]+ { printf("Word: '%s', len = %d\n", yytext, yyleng); }
..|\n ; /* ignore anything else */

%%

int main(void)
{
yylex();
}

--
Tor <torust [at] online [dot] no>
May 20 '07 #5
Malcolm McLean wrote:
<J.*******@gmai l.comwrote in message
>Hi, I'm hoping to get some advice on this design query to see if I'm
heading in the right direction.

I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.

Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?

Google for ggets and Chuck Falconer.
Chuck, a reg in this group, has kindly provided exactly what you want.
I believe entirely rights free, certainly for nothing.
try: <http://cbfalconer.home .att.net/download/>
--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

May 21 '07 #6
Tor Rustad wrote:
J.*******@gmail .com wrote:
.... SNIP ...
>>
I would like to read a file of many lines and then search for a
particular character to break up the sentence into separate words.
Once done I will sort them.

I thought of using fgets but the line could be very very long and I
won't know it's length of n characters that could be in a line.

Your problem, is well known for compiler writers, they call it lexical
analysis. fgets() is not normally used in hand-written lexer's, but
getchar() and ungetc() is, because you analyze single character at a time.
>Using fgetc seems the better bet and put each character into an array
so I can traverse the array.

However,if I put each character into an array, I still won't know how
large to make the array.

Is there a better strategy ?

Yes, there is standard tool for this, called lex. Here is a lex
specification that print all the words in a file:

$ cat ex_lex.l
/*
Compile:
flex ex_lex.l
cc lex.yy.c -ll

Run:
./a.out < ex_lex.l
*/

%{
%}
%%

[^ \n\t]+ { printf("Word: '%s', len = %d\n", yytext, yyleng); }
.|\n ; /* ignore anything else */

%%

int main(void)
{
yylex();
}
Why all that tortuous mess to copy text?.

#include <stdio.h>

int main(void) {
int ch;

while (EOF != (ch = getchar())) putchar(ch);
return 0;
}

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 22 '07 #7
CBFalconer wrote:

[...]
Why all that tortuous mess to copy text?.

Hand-written lexer in C is easy, but writing a lex spec is easier.

Try again, this time print each *word* and it's length on a separate line on
the output. Please follow your own bogus advice, by using ggets().

--
Tor <torust [at] online [dot] no>

May 22 '07 #8

"Tor Rustad" <to****@online. nowrote in message
news:ir******** *************@t elenor.com...
CBFalconer wrote:

[...]
>Why all that tortuous mess to copy text?.


Hand-written lexer in C is easy, but writing a lex spec is easier.

Try again, this time print each *word* and it's length on a separate line
on
the output. Please follow your own bogus advice, by using ggets().
Why is ggets() a problem for this task?
Granted les / flex is a dedicated lexical analyser and in its element with
token generation, but most people don't have the inclination to learn yet
another language for such a simple spec.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 23 '07 #9
Malcolm McLean wrote:
>
"Tor Rustad" <to****@online. nowrote in message
news:ir******** *************@t elenor.com...
>CBFalconer wrote:

[...]
>>Why all that tortuous mess to copy text?.


Hand-written lexer in C is easy, but writing a lex spec is easier.

Try again, this time print each *word* and it's length on a separate line
on the output. Please follow your own bogus advice, by using ggets().
Why is ggets() a problem for this task?
OP needed to split the file content into words, not lines! Hence, this is
what he wanted in C:

int lexer(char *word, size_t *wlen)
{
int t;

while (1)
{
t = getchar();

/* Eat white space */
if (t == ' ' || t == '\t' || t == '\n')
;
else if (t == EOF)
return EOF;

etc...
}
}

adding logic to handle lines, just complicate matters. Hence, using fgets(),
ggets(), getline() etc. was all bad advice.

Granted les / flex is a dedicated lexical analyser and in its element with
token generation, but most people don't have the inclination to learn yet
another language for such a simple spec.
OP asked if there was a better strategy, well there is, he can ignore the
line issue and look into using a dedicated tool for such problems.

Since I had already done the complete lex skeleton, all he needed to do, was
really to add that C sorting code. That hardly qualify as learning a new
language. <g>

--
Tor <torust [at] online [dot] no>

May 24 '07 #10

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

Similar topics

9
11236
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has only one column: txtOutput I want to use the DB front end (MS Access) to send the text string to the SQL backend, then have the SQL Server create a file to a path, such as F:/myfiledate.txt that holds the text in txtOutput, then the trigger...
1
1701
by: Tim | last post by:
Hi, We are downloading a few thousand rows of data for users to choose from and need to speed up the operation. The data is related in four levels. The current design allows the user to select the first level from about 20 choices and then the next level's choices are retrieved from the server. This is taking about 4 seconds. We are required to make it faster. We need to be able to download all the data once and save it for use
2
1903
by: moller | last post by:
Im looking in to the possibility of moving from mySQL to an access database. My reasons are: (1) Database is single user. (2) Database local on users PC. (3) Database has only 8 tables where 4 are filled at database creation with aprox 20 rows each and are never added to after that. (4) Database grows with ca 6000 rows/week. (5) No data is ever deleted, exept... (6)
3
3812
by: sea | last post by:
If a runtime installation of Access is created and installed on a client machine that has a full verion of access, will it still be possible to view the tables and queries in design view but not the forms? Thank you very much, Sue
0
959
by: JTS | last post by:
I need to create three ASP.NET applications - each one will run on a different production server. Each application does basically the same thing with respect to data access; the apps differ primarily in presentation and audience. Each app will need to interact with a SQL Server database that is running on its own server. All servers will initially be on the same subnet, however there is no guarantee that they will remain on the same subnet...
5
4603
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want to add a dictionary of name-value pairs. In other words, instead of a single name-value, I want to add a name-value collection in there. I could do this:
18
1554
by: Wayne | last post by:
I have a potential client who has requested design access to the prospective database so that they can change reports if and when required. Their reasons for this are: 1. They don't want to be locked into a situation where it will cost them money every time management wants a report changed and 2. They are concerned that they will be locked into relying on one person for any future changes that may be required, especially if that...
13
2407
by: NickName | last post by:
"For the vision impaired, SVG offers tremendous potential for interactive Internet mapping applications as discussed by Gardner and Bulatov (2001).". Now, here's an SVG file with fair/medium complexity, http://www.carto.net/papers/svg/samples/canvas.svg, even as a sighted person, I find its source code albeit in xml format hard to "absorb" at the first glance, let alone a visually impaired individual. The most important element seems...
0
9690
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, 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...
1
10251
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,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, 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...
1
7576
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
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
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.