Thread: Text Formatting: alignment FILE

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    7

    Exclamation Text Formatting: alignment FILE

    hi guys..
    Must to write a program that takes 2 parameters from the command line:
    A) the name of a text file, and
    B) N the number of characters per line.
    The program should open the data file, reads the text, and the printed
    TAKES the main output aligned in rows of length N.
    how i will do that?
    THANKS

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    thanks man.. but i know this part of the problem..
    how i can do the others?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about you show us all the parts you CAN do (reading command line arguments, opening files, reading words, counting lengths of words or whatever).

    Then we can help you with the issue which is really bugging you, as opposed to you replying "yeah, I knew that already".

    Instead of this guessing game which I'm already bored of.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    salem please dont reply again in my thread..
    guys(expect salem) my main problem is how to do the aligment.. maybe some standar fanctions?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Look up fopen(), fgets(), fclose(). How do you mean align the rows? Right-align each word to a right margin for example?

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    i mean middle margin...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps it's time you read this
    Announcements - C Programming

    > salem please dont reply again in my thread..
    Like I'm going to pay any attention to that kind of demand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    So you mean you want to align words to be centered? If the width is N (number of characters per line), then you can print out each word by preceding them with (N - strlen(word)) / 2 spaces.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Consider the example of using example.txt file that contains the text:
    As you know, I’m quite keen of comic books, especially the
    ones about superheroes. I find the whole mythology surrounding
    superheroes fascinating. Take my favorite superhero; Superman, not a
    great comic book, not particularly well-drawn, but the mythology;
    the mythology is not only great, it’s unique.

    Then the command ./ask example.txt 60 should produce the output:

    As you know, I’m quite keen of comic books, especially the
    ones about superheroes. I find the whole mythology
    surrounding superheroes fascinating. Take my favorite
    superhero; Superman, not a great comic book, not
    particularly well-drawn, but the mythology; the mythology is
    not only great, it’s unique.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up the string functions from string.h and print from stdio.h.

    strcat - C++ Reference
    sprintf - C++ Reference

    You are very likely to need strcat (or maybe strncat) but you might be able to use sprintf (or maybe snprintf) instead.
    The other functions will depend on your design.

    The above is needed for one way of solving the problem.

    The other obvious way to to use fgetc and fputc functions.

    Tim S.
    Last edited by stahta01; 12-11-2012 at 02:26 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    and? how this function will help me? i think you havent undrestard what i want to do.. the user will gives a file..

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by JIM13 View Post
    and? how this function will help me? i think you havent undrestard what i want to do.. the user will gives a file..
    I understand perfectly, you want someone to hand you code instead of looking at links and learning to program.

    You are now on my ignore list, goodbye.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Oh OK. You want to read the file word for word and try to place as many words in one line so that the line length is <= 60, output each line.

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    yes my friend.. the 60<= its example the user willgives the number of words in line..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text Alignment
    By stdq in forum C Programming
    Replies: 3
    Last Post: 05-04-2011, 06:31 PM
  2. Text Alignment
    By LoveTractor in forum C Programming
    Replies: 5
    Last Post: 09-19-2010, 05:36 PM
  3. Need help formatting data from a text file.
    By civix in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2008, 08:34 PM
  4. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  5. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM