Storing sentences in a 2-d array from a file

This is a discussion on Storing sentences in a 2-d array from a file within the C Programming forums, part of the General Programming Boards category; after my last thread, i think its appropriate that I have to make a new thread for this new problem ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35

    Storing sentences in a 2-d array from a file

    after my last thread, i think its appropriate that I have to make a new thread for this new problem since a new problem and not related to my last post...

    I have a group of sentences in a file. And this file is constantly written. There maybe an unknown number sentences in a paragraph. But you have to store those sentences in one paragraph. There are 38 paragraphs, with unknown number of sentences. Those sentences are then printed depending on what paragraph you choose....

    This problem is so hard.... I have no idea on how to do that, i used google search but there's no help.

    example file with three paragraphs but with different number of sentences.

    Code:
    the quick brown cow jump over the moon
    the quick brown cow jump over the moon
    the quick brown cow jump over the moon
    the quick brown cow jump over the moon
    the quick brown cow jump over the moon
    
    the cow used a jet pack to jump over the moon
    the cow used a jet pack to jump over the moon
    the cow used a jet pack to jump over the moon
    
    The cow bump the airplane and crashed
    The cow bump the airplane and crashed
    The cow bump the airplane and crashed
    The cow bump the airplane and crashed
    The cow bump the airplane and crashed
    The cow bump the airplane and crashed
    The cow bump the airplane and crashed
    can someone give me an idea or anything on how to do this????

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Read the whole file into an array, using any kind of seek function in a text file is worthless. One the file is in your array, you can iterate through that looking for and keeping track of a line that only contains the newline character '\n'. These define your paragraphs. So, let's say you wanted to print paragraph 3:

    1. read the file into a 2d char array
    2. Iterate throught the array, counting the lines that only contain a newline
    3. Once you find the second line that only contains a newline, start printing lines until you reach another line that just contains a newline character.

    Or if the file is to big, instead of dumping the entire file into an array, just perform above but iterate through the file using fgets into a temp buffer until you reach the area you want to print.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Quote Originally Posted by edmund085 View Post
    after my last thread, i think its appropriate that I have to make a new thread for this new problem since a new problem and not related to my last post...

    I have a group of sentences in a file. And this file is constantly written. There maybe an unknown number sentences in a paragraph. But you have to store those sentences in one paragraph. There are 38 paragraphs, with unknown number of sentences. Those sentences are then printed depending on what paragraph you choose....
    Are the sentences in each paragraph duplicates.
    If that is so it simplifies the job of storing them.
    Quote Originally Posted by edmund085 View Post
    This problem is so hard.... I have no idea on how to do that, i used google search but there's no help.

    example file with three paragraphs but with different number of sentences.
    .
    .
    .
    can someone give me an idea or anything on how to do this????
    It'll seem like a piece of cake once you devise its algorithm.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A file containing just 38 paragraphs can't be constantly written, unless one or more of those paragraphs has nearly an unlimited number of sentences.

    I believe you have the logic you need from Andrew's post. If not, post back.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    thanks.... AndrewHunter.... I'll post the program if I'm done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2010, 12:49 PM
  2. Reading sentences in with char array
    By AJOHNZ in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2009, 11:33 PM
  3. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 08:33 PM
  4. storing integers from a text file into a 1d array
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 09-30-2008, 10:04 PM
  5. Storing words from a file to an array
    By SIKCAR in forum C Programming
    Replies: 10
    Last Post: 09-09-2002, 07:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21