954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C code for LINE SEPARATING

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..

singal.mayank
Newbie Poster
5 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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.

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

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

FILE *fp;
char buffer[BUFSIZ];

if( ( fp = fopen( <filename>, "r") ) != NULL )
{
    while( fgets( buffer, BUFSIZ, fp) != NULL )
           printf( "%s", buffer );
           /* You could backup this buffer
              by copying it on to a different
              char array */
}


ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

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/clibrary/cstring/strtok.html

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

oh, dur.

you're right.

brainfart.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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...

singal.mayank
Newbie Poster
5 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You