C code for LINE SEPARATING

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Closed Thread

Join Date: Jun 2008
Posts: 5
Reputation: singal.mayank is an unknown quantity at this point 
Solved Threads: 0
singal.mayank singal.mayank is offline Offline
Newbie Poster

C code for LINE SEPARATING

 
0
  #1
Jun 27th, 2008
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..
Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: C code for LINE SEPARATING

 
0
  #2
Jun 27th, 2008
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.
Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: C code for LINE SEPARATING

 
1
  #3
Jun 27th, 2008
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
Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: C code for LINE SEPARATING

 
0
  #4
Jun 29th, 2008
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
Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C code for LINE SEPARATING

 
0
  #5
Jun 29th, 2008
Originally Posted by jephthah View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: C code for LINE SEPARATING

 
0
  #6
Jun 29th, 2008
oh, dur.

you're right.

brainfart.
Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: singal.mayank is an unknown quantity at this point 
Solved Threads: 0
singal.mayank singal.mayank is offline Offline
Newbie Poster

Re: C code for LINE SEPARATING

 
0
  #7
Jun 29th, 2008
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...
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC