Like Tree4Likes

Text Formatting: alignment FILE

This is a discussion on Text Formatting: alignment FILE within the C Programming forums, part of the General Programming Boards category; hi guys.. Must to write a program that takes 2 parameters from the command line: A) the name of a ...

  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
    32,823
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,832
    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
    32,823
    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.
    jimblumberg likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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
    2,765
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,765
    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.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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..

Page 1 of 2 12 LastLast
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, 07:31 PM
  2. Text Alignment
    By LoveTractor in forum C Programming
    Replies: 5
    Last Post: 09-19-2010, 06:36 PM
  3. Need help formatting data from a text file.
    By civix in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2008, 09:34 PM
  4. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  5. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM

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