943,671 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 904
  • C RSS
Jun 27th, 2008
0

C code for LINE SEPARATING

Expand Post »
hello..
I m designing a STATISTICAL SUMMARIZER in C..
This software will create the summary of a text file submitted by
the user..
The first step is to separate line in the text file..
I hav almost completed this step..
but there are some limitations..
Suppose if text file hav this input..e.g

Hello! This is MAYANK. I am a Student of "nitk."
Working on a project.



then how i will separate these lines??
can u provide me with a code of line separation..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
singal.mayank is offline Offline
5 posts
since Jun 2008
Jun 27th, 2008
0

Re: C code for LINE SEPARATING

I would make an array of strings, one element for each line. Than use something like getchar() to read in one character at a time until you find a '\n' (newline) character. Then start using the next element and read in the next line likewise until you reach the end of the file.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jun 27th, 2008
1

Re: C code for LINE SEPARATING

Have a look at a function calle3d fgets which is defined under stdio.h library. This function will do what excatly you want. It read a line of text from the text file until it EOF which then return NULL. The sample usage of that function can be found bellow

  1. FILE *fp;
  2. char buffer[BUFSIZ];
  3.  
  4. if( ( fp = fopen( <filename>, "r") ) != NULL )
  5. {
  6. while( fgets( buffer, BUFSIZ, fp) != NULL )
  7. printf( "%s", buffer );
  8. /* You could backup this buffer
  9.   by copying it on to a different
  10.   char array */
  11. }

ssharish
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Jun 29th, 2008
0

Re: C code for LINE SEPARATING

the function that would be helpful for you here, i think, is STRTOK

in the string.h library.

it will separate any text into "tokens" based on a set of "delimiters"

your "tokens" will be individual lines, and your set of "delimiters" will simply be the newline character '\n'

once you have your individual lines you can insert anything you want in between them, and then rewrite them to a buffer or another text file as you desire.

http://www.cplusplus.com/reference/c...ng/strtok.html
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 29th, 2008
0

Re: C code for LINE SEPARATING

Click to Expand / Collapse  Quote originally posted by jephthah ...
the function that would be helpful for you here, i think, is STRTOK
Nope -- just use fgets() as previously suggested. No need to read the file one character at a time just to find line terminators because that's doing it the hard way.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005
Jun 29th, 2008
0

Re: C code for LINE SEPARATING

oh, dur.

you're right.

brainfart.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 29th, 2008
0

Re: C code for LINE SEPARATING

thnks all of u...
bt i m nt looking for separating the sentences..
for eg.
input
Hello! This is MAYANK. I am a Student of "nitk."
Working on a project.


output:
Hello! This is MAYANK.
I am a Student of "nitk."
Working on a project.


sorry for giving the heading as line separation
it would be rather sentence separation..
i hav started new thread with this name..
do reply to that thrad..
thnku...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
singal.mayank is offline Offline
5 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: Reversing doubly linked list?
Next Thread in C Forum Timeline: Sentence separation..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC