User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 429,898 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,351 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 390 | Replies: 6
Closed Thread
Join Date: Jun 2008
Posts: 5
Reputation: singal.mayank is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
singal.mayank singal.mayank is offline Offline
Newbie Poster

C code for LINE SEPARATING

  #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..
AddThis Social Bookmark Button
 
Join Date: Apr 2006
Posts: 35
Reputation: death_oclock is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
death_oclock death_oclock is offline Offline
Light Poster

Re: C code for LINE SEPARATING

  #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.
 
Join Date: Dec 2006
Posts: 232
Reputation: ssharish2005 is on a distinguished road 
Rep Power: 2
Solved Threads: 18
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: C code for LINE SEPARATING

  #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
 
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: C code for LINE SEPARATING

  #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
Why so serious?
 
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,250
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 939
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C code for LINE SEPARATING

  #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.
 
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: C code for LINE SEPARATING

  #6  
Jun 29th, 2008
oh, dur.

you're right.

brainfart.
Why so serious?
 
Join Date: Jun 2008
Posts: 5
Reputation: singal.mayank is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
singal.mayank singal.mayank is offline Offline
Newbie Poster

Re: C code for LINE SEPARATING

  #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...
 
Closed Thread

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC