473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrap up Text

I need to help in my assignment. I need to wrap a text with the
following indications:
1.If you have reached the end of the line (number of characters on this
line >= 40) AND you have reached the end of the current word, start a
new line and reset the number of characters on the line to 0. The end
of a word will be denoted by one of these: blank space, '\n',
'.', ',' or ';'.

#include <stdio.h>
#include <ctype.h>

main()
{
FILE *fp1,*fp2; //fp1 input file, fp2 output file
char filename[30]; //filename character and x is character
char line[80];
char x;
int count=0; // count the line of the file 1
int j=0; //count of character of file 2

printf("Enter the filename to be scanned: "); // ask the user
scanf("%s", filename);

fp1= fopen(filename, "r"); //open input file,read-only
fp2= fopen("output.t xt", "w"); //open outp file,write-only

while((x=getc(f p1))!=EOF) //get next char from input file, store
in x
{
if(x=='\n' || x=='*') //count the lines of the file1
count++;
if(x=='\n') //separete words

putc(' ' ,fp2);

else if(x=='*'){ //new line with an indentation \t

putc('\t',fp2);
}
else if(x!='*') //delete *
{

j++;

putc(x,fp2); //output file

//Begin the Wrap up text but I have problem here. Help!!!
if((j>=40) && (x==' ' || x==';' || x=='.' || x==',' || x=='\n'))

do{
putc('\n',fp2); //start new line
count1++;
j=0;
}while(j!=0);
if(x=='\n')
putc('\n',fp2);
}

}

Mar 3 '06 #1
8 3686
In article <11************ *********@t39g2 000cwt.googlegr oups.com>,
FUGATO <fu*******@hotm ail.com> wrote:
I need to help in my assignment. 1.If you have reached the end of the line (number of characters on this
line >= 40) AND you have reached the end of the current word, start a
new line and reset the number of characters on the line to 0. The end
of a word will be denoted by one of these: blank space, '\n',
'.', ',' or ';'. #include <stdio.h>
#include <ctype.h> main()
Minus 1 point for not using integer main(void)
{
FILE *fp1,*fp2; //fp1 input file, fp2 output file
Noted: C99 style comments, not valid in C89.
char filename[30]; //filename character and x is character
char line[80];
char x;
int count=0; // count the line of the file 1
int j=0; //count of character of file 2

printf("Enter the filename to be scanned: "); // ask the user
Minus 1 point for not ending the prompt with '\n' and another for
not using fflush() to ensure that the prompt will be displayed before the
read.
scanf("%s", filename);
Automatic 2 grade-letter deduction using scanf() to read a string you
don't control the size of. What happens if the user enters a 35
character filename?

Minus 1 point for using scanf("%s") to read a filename that might
contain a blank. The %s format skips leading whitespace, then reads
consequative non-whitespace, stopping at the first whitespace. That's
not going to be able to handle a filename such as

C:\Program Files\Fugato\Wo rdWrap.data
fp1= fopen(filename, "r"); //open input file,read-only
Minus 1 point for not checking that the input file was opened properly.
fp2= fopen("output.t xt", "w"); //open outp file,write-only
Minus 1 point for not checking that the out file was opened properly.
while((x=getc(f p1))!=EOF) //get next char from input file, store
Minus 1 point for assigning the result of getc() to a char variable
that might be unsigned and thus not able to store a value equal to
EOF (which is certain to be negative.)
in x
{
if(x=='\n' || x=='*') //count the lines of the file1
count++;
Minus 1 point for using the magic test against '*' which is not
documented in the requirements as being one of the special characters
to be tested for.

Minus 1 point for counting * as indicating the end of a line -- it
isn't. If there was a special requirement that it be treated
as a newline, then instead minus 1 point for not fully stating
the requirements.

if(x=='\n') //separete words
Typo in comment. Noted but we'll let it go this time.

putc(' ' ,fp2);
Minus 1 point for bug of assuming that end of line in the
input will always result in a space in the output file. If you've
reached the natural end of a word and you are over the column
limit, then there should not be a space emitted before the output
lineline.
else if(x=='*'){ //new line with an indentation \t

putc('\t',fp2);
Minus 3 points: 1 for not adjusting the character count to take
into account the indentation; 1 for not documenting
what the indentation size is nor allowing the user to control
the indentation; and 1 for outputting indentation even if there is
no further output to go on the line (e.g., end of file.)

}
else if(x!='*') //delete *
Minus 1 point for redundant condition: if x was '\n' or '*' then
you could not have gotten here through the chain of if/else
so testing for non-'*' is unnecessary.
{

j++;

putc(x,fp2); //output file

//Begin the Wrap up text but I have problem here. Help!!!
if((j>=40) && (x==' ' || x==';' || x=='.' || x==',' || x=='\n'))
Minus 1 point for not recognizing that x cannot be '\n' here because
of the if/else chains.

do{
putc('\n',fp2); //start new line
count1++;
Minus 3 points for using an undeclared variable (count1). This would
not compile, so you have submitted an answer which is not the one
you have actually run: minus 7 points for that.
j=0;
}while(j!=0);
Minus 1 point for not recognizing that j will always be 0 after the
do{}, so the while() condition is not useful, and the construct
should just be replaced with a plain statement block {}.
if(x=='\n')
x cannot be '\n' here, but we already docked you for that.
putc('\n',fp2);
On the other hand, this would get you a double newline on output if you
did manage to get here, so that's another logic bug and another
point docked. }
Minus 1 point for failing to return an exit status to the
operating system, which is recommended for C89 programs.

Minus another point for failing to return an exit status to the
operating system, which is mandatory for C99 programs. We know
that you are using C99 because of the comment style. If you wish
to claim that you should not be docked here because you are using
C89 and returning a value is optional for C89, then you will
instead be docked for using C99 style comments in a C89 program.
Minus 1 point for failing to ensure that the text file fp2 ends
in a newline.

Minus 1 point for failing to close the input and output files.

Minus 1 point for not using the result of counting the lines.
If you don't use the count, then don't do the count.

Minus 1 point for not using the declared array 'line'. }


--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Mar 3 '06 #2
On 2006-03-03, Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
Minus 1 point for not using integer main(void)
I guess you mean "int", there is no type named "integer" that I know
of :)
Minus 1 point for failing to return an exit status to the
operating system, which is recommended for C89 programs.

Minus another point for failing to return an exit status to the
operating system, which is mandatory for C99 programs. We know
that you are using C99 because of the comment style. If you wish
to claim that you should not be docked here because you are using
C89 and returning a value is optional for C89, then you will
instead be docked for using C99 style comments in a C89 program.
If I'm not mistaken you got this backwards.
C89 I believe required an explicit return statement at the end of main,
while C99 does not.

5.1.2.2.3 Program Termination
"reaching the } that terminates the main function returns a value of 0"
Minus 1 point for failing to close the input and output files.
That's more of an issue of style and hardly grounds for removing points,
as all open files are closed upon program exit.

7.19.3 Files
5) "... If the main function returns to its original caller or if the
exit function is called, all open files are closed (hence all output
streams are flushed) before program termination."
Minus 1 point for not using the result of counting the lines.
If you don't use the count, then don't do the count.


Minus 1 point for keeping a point counter during your whole reply, and
not telling us the final count at the end :)
--
John Tsiombikas (Nuclear / Mindlapse)
nu*****@siggrap h.org
http://nuclear.demoscene.gr/
Mar 3 '06 #3
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ *********@t39g2 000cwt.googlegr oups.com>,
FUGATO <fu*******@hotm ail.com> wrote:

[...]
printf("Enter the filename to be scanned: "); // ask the user


Minus 1 point for not ending the prompt with '\n' and another for
not using fflush() to ensure that the prompt will be displayed before the
read.


With a call to fflush(stdout) after the printf(), a newline is not
necessary. (In fact, I prefer not to have a newline at the end of a
prompt; it makes things clearer to the user.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #4
"John Tsiombikas (Nuclear / Mindlapse)" <nu*****@siggra ph.org> writes:
On 2006-03-03, Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:

[...]
Minus 1 point for failing to close the input and output files.


That's more of an issue of style and hardly grounds for removing points,
as all open files are closed upon program exit.


Style issues can certainly be grounds for removing points; after all,
good style is one of the things we're trying to teach.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #5
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ *********@t39g2 000cwt.googlegr oups.com>,
FUGATO <fu*******@hotm ail.com> wrote:
printf("Enter the filename to be scanned: "); // ask the user
Minus 1 point for not ending the prompt with '\n' and another for
not using fflush() to ensure that the prompt will be displayed before the
read.
With a call to fflush(stdout) after the printf(), a newline is not
necessary.
For text files, the status of the last line is officially indeterminate
if there is no newline at the end of the line. fflush() does not
introduce that newline: it just spills the accumulated buffer.
Hence to be -sure- that prompt will be visible, you must terminate
it with a newline.
(In fact, I prefer not to have a newline at the end of a
prompt; it makes things clearer to the user.)


Yeah, but it relies upon system-specific behaviour.

Though this discussion does lead to the question of whether
stdout is a text stream or a binary stream ?
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Mar 3 '06 #6
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
ro******@ibd. nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ *********@t39g2 000cwt.googlegr oups.com>,
FUGATO <fu*******@hotm ail.com> wrote: printf("Enter the filename to be scanned: "); // ask the user Minus 1 point for not ending the prompt with '\n' and another for
not using fflush() to ensure that the prompt will be displayed before the
read.
With a call to fflush(stdout) after the printf(), a newline is not
necessary.


For text files, the status of the last line is officially indeterminate
if there is no newline at the end of the line. fflush() does not
introduce that newline: it just spills the accumulated buffer.
Hence to be -sure- that prompt will be visible, you must terminate
it with a newline.


Only if it's the last line of output.
(In fact, I prefer not to have a newline at the end of a
prompt; it makes things clearer to the user.)


Yeah, but it relies upon system-specific behaviour.

Though this discussion does lead to the question of whether
stdout is a text stream or a binary stream ?


It's a text stream.

C99 7.19.1p3:

stderr
stdin
stdout

which are expressions of type "pointer to FILE" that point to the
FILE objects associated, respectively, with the standard error,
input, and output streams.

C99 7.19.3p7:

At program startup, three text streams are predefined and need not
be opened explicitly -- _standard input_ (for reading conventional
input), _standard output_ (for writing conventional output), and
_standard error_ (for writing diagnostic output). As initially
opened, the standard error stream is not fully buffered; the
standard input and standard output streams are fully buffered if
and only if the stream can be determined not to refer to an
interactive device.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #7
On Fri, 3 Mar 2006 06:01:43 UTC, "John Tsiombikas (Nuclear /
Mindlapse)" <nu*****@siggra ph.org> wrote:
Minus 1 point for failing to close the input and output files.
That's more of an issue of style and hardly grounds for removing points,
as all open files are closed upon program exit.


Noways, failing to close the files is failing any chance to dedect
errors. Econ fclose() will return an error indicator showing that data
is not written and/or that the filecan't simply closed at all because
some error on the target device hinders that.
7.19.3 Files
5) "... If the main function returns to its original caller or if the
exit function is called, all open files are closed (hence all output
streams are flushed) before program termination."


Yeah - but what is when the last byte can't been written to the file
because device is full, gets crashed....? What is when the underlying
system is unable to write the management information to the file? A
failsave application will inform the user about any critical error
that it can not recover from itself.

So -30 points to the OP by leaving off each and any error handling.
-3,000 points to you by giving false hints to the OP and to the
community.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Mar 4 '06 #8
On 2006-03-04, Herbert Rosenau <os****@pc-rosenau.de> wrote:

On Fri, 3 Mar 2006 06:01:43 UTC, "John Tsiombikas (Nuclear /
Mindlapse)" <nu*****@siggra ph.org> wrote:
> Minus 1 point for failing to close the input and output files.
That's more of an issue of style and hardly grounds for removing points,
as all open files are closed upon program exit.


Noways, failing to close the files is failing any chance to dedect
errors.


I agree, still it's perfectly valid for a C program to not close them
explicitly.
Econ fclose() will return an error indicator showing that data
is not written and/or that the filecan't simply closed at all because
some error on the target device hinders that. .... snip ... A failsave application will inform the user about any critical error
that it can not recover from itself.
The stream (FILE*) *will* be closed, regardless of what the OS does with
the actual file.
Your point is just that you miss a chance to provide an error message to
the user (or error code to the caller) that some of the data may have
not being written. While this is a very good practice, and I definitely
want to encourage it, however the OP's program was not *wrong* in this
respect, just not very user-friendly.
So -30 points to the OP by leaving off each and any error handling.
-3,000 points to you by giving false hints to the OP and to the
community.


Charming ... :)

--
John Tsiombikas (Nuclear / Mindlapse)
nu*****@siggrap h.org
http://nuclear.demoscene.gr/
Mar 7 '06 #9

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

Similar topics

1
3158
by: Guru | last post by:
Hi, I am using FOP for creating pdf. I am having a table which has 14 columns. And each column contents may contain any number of chars. My problem is : When the text is having no spaces in between like "thisisalongwordandcouldnotbewrapped", it is overlapping
3
10769
by: JHR | last post by:
Hey all, I'm trying to make a sidebar box float to the right of various items, and for those items to wrap if a user shrinks his browser window. Instead, in every browser I've tried except for Internet Explorer (in Safari, Firefox, and other Mozilla-types), the box ends up overlapping with the second and third rows of the text (which are in an ordered list with some 'display: table-cell' formats defined in order to allow specific...
3
3416
by: mike | last post by:
I have a script that is not rendering the textwrap in svg properly. //does not work ..... var svgLand = svgObj.getElementById("NarrDisplay"); mytextwrap= document.createElement("text:wrap"); mytextwrap.setAttribute("x","20"); mytextwrap.setAttribute("y","30"); mytextwrap.setAttribute("width","640"); mytextwrap.setAttribute("style","fill:#000000; font-family:arial; font-size:14; text-align:left; line-interval:1.25em;");...
3
6773
by: Fluffy Convict | last post by:
I am trying to write a script that changes a textarea wrap realtime, basically like you can switch to and from "wordwrap" in Microsofts Notepad. Because of a bug (https://bugzilla.mozilla.org/show_bug.cgi?id=302710), FireFox does not listen to the textarea.wrap = 'soft' command. Because I want the script to be cross-browser compatible, someone suggested to remove and immediately add a node to the DOM, so I now have: ...
5
2598
by: name | last post by:
Back for more critique. ---------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define MAX 10000
3
14027
by: ColinC | last post by:
Hi, as a newbie to Visual Studio C# Forms development, I'd like some advice on the following. I have a form with a tab control. I have some tab titles that are three words e.g "house & garden" This makes the tab quite long compared to other tabs and means that not all tabs can be displayed on the screen. I want these to be displayed on the tab title as
3
14582
by: Jason | last post by:
Anyone know how to make the text wrap in a text box of a DetailsView?
1
5766
by: duzhidian | last post by:
Hello: I just want to use a two columns web. If I put sidebar on the right, it there is a list, it's fine. If it is a paragraph, the width will not work? Why? In the following example, width is 200 px, but the contents is much long than that.
1
3075
by: bussasrinivas | last post by:
How to wrap up text shown with a table cell after a certain length intervals ? -------------------------------------------------------------------------------- I have a requirement to wrap a text shown within a table after every 12 characters here is what I have <td width="76"> This is a text that needs to be wrapped after every 12 characters.This is a text that needs to be wrapped after every 12 characters.This is a text that needs to...
15
11606
by: removeps-groups | last post by:
How to wrap text in <ptag if the text has no spaces and is very long? Here is an example: <p>VeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLongVeryVeryLong</ p> The above line is so long that it ought to wrap. But because it has no spaces, it does not wrap. Instead the entire text...
0
9530
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...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
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
8237
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
6793
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
4593
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...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
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.