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

Paragraphs in a text file

I am only a beginner in C. I am trying to find the number of paragraphs in a text file, and so I tried to count the number of empty lines and increment the counter. I searched for ' \n ' in the file and incremented the counter but the program is telling me the number of lines.. not the number of paragraphs. What can I use as a condition in the loop to find empty lines in the file??
Jan 23 '08 #1
5 4858
oler1s
671 Expert 512MB
Consider what is an empty line. Let me show you an example:

Expand|Select|Wrap|Line Numbers
  1. This is a line.
  2.  
  3. This is another line.
  4.  
  5. This is a third line.
  6.  
Which the computer sees as (represented as text):

Expand|Select|Wrap|Line Numbers
  1. This is a line.\n
  2. \n
  3. This is another line.\n
  4. \n
  5. This is a third line.
  6.  
So clearly an empty line has nothing but a newline on it. Your line extraction mechanism probably works by grabbing data until it reaches a newline character. Then it increments a counter of some kind.

You have an empty line when the first thing you grab is a newline itself.

Do you see how to logically look at the problem?
Jan 23 '08 #2
Yes thats what my program is doing, grabbing data until it reaches a newline character, then it increments a counter. If only empty lines had a newline character it would work but thats not the case. I can't find anything to put as a condition for detecting an empty line. As you said an empty line has nothing but a newline character on it, so how can you detect an empty line??
Jan 23 '08 #3
The only thing I can see is that there is a new paragraph only when there are 2 consecutive newline characters.. Can I use that as a condition?
Jan 23 '08 #4
Ganon11
3,652 Expert 2GB
Try it and see if it works.
Jan 24 '08 #5
oler1s
671 Expert 512MB
The only thing I can see is that there is a new paragraph only when there are 2 consecutive newline characters.. Can I use that as a condition?
If you choose to look at it that way...

You could have 2 consecutive newlines. Or 4. Or 10. Or 100. Or 10000. It doesn't matter. So your code shouldn't be checking for a second newline, because the actual number of consecutive newlines doesn't matter. Because the layout of paragraphs looks something like:

non-newline data.One or more newlines.non-newline data.One or more newlines.non-newline data.One or more newlines.

If your data extraction method makes it easier for you to check consecutive newlines, what you shouldn't be doing is checking repeatedly for additional newlines. There's no point.

Think about it. There's two states your data extraction mechanism can be in. It can be extracting non-newline data. Or it can be extracting newline data. And your paragraph counter increases when you change from extracting non-newline data to extracting newline data.

So you need code that keeps repeatedly extracting non-newline data, checking if it has reached a newline or EOF. Then you increment a paragraph counter when you leave the loop. Then you start looping through the data, until you reach something that isn't a newline (or whitespace technically). You leave the loop when you reach a normal character. Then you go back to looping through normal characters, until you reach a newline or EOF. Then...so on.

One more thing. Are you sure that paragraphs are separated only by a single newline? I would imagine they would be separated by two or more newlines, but not a single newline.

In any case, your approach to the problem depends on how you extract the data. I answered your question assuming you inspected the data character by character.

If you're having trouble understanding the abstract, write it down on paper, and imagine you're the computer inspecting the data. What kind of logic are you following as you extract each character?
Jan 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sandman | last post by:
I am splitting a text block into paragraphs, to be able to add images and stuff like that to a specific paragraph in a content management system. Well, right now I'm splittin on two or more...
40
by: | last post by:
Could someone cite some offical rule or documentation with regard to the <P> tag? I've seen folks put it in between paragraphs... and others wrap it around a paragraph. I'd think to use it...
2
by: needhelp | last post by:
This one ought to be simple, but I have dug myself deeper and deeper, and gone farther and farther from what I wanted and I'm giving up. And my html that I've written is so bolloxed up now that...
8
by: lkrubner | last post by:
Am I wrong, or do paragraphs have extra space on top in FireFox, when compared to Microsoft IE. The top of this page is an example: http://www.publicdomainsoftware.org/index.php?pageId=299 The...
0
by: André Minhorst | last post by:
Hi, I use the RTF2-Control from Stephen Lebans to store richtext in memofields. The problem is that text longer than one page is cut and not continued on the next page. So I break the text...
7
by: Jukka K. Korpela | last post by:
After noticing that IE 7 beta supports selectors like p+p, I started wondering how to achieve a simple rendering of paragraphs so that 1) there is no vertical spacing between paragraphs (i.e. the...
16
by: danep | last post by:
Hi all, On my site I have a section of code that resembles the following: <p id="gear" style="display: none;"> <p>test</p> </p> This renders fine in Firefox (that is, nothing is...
1
by: vierow | last post by:
new paragraphs in php mysql I have a php form to upload stories into a mysql database when I retrieve the file "print $row'; all the paragraphs run together. How do I get carriage returns...
5
by: keoo | last post by:
Im putting the part of the code which is relevant to my question......The program is opening the texfile which i already have created..Im not quite managing to make the program count the number of...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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:
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
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...

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.