Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to break a line in C?

Suppose I have an array char Description[500]

I want to write this in a file but maximum column I can write in file per line is 80 Column. I want to know how to break Description into pieces.Any idea?

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    the other solutions work but I find the multiple loops and multiple arrays rather much for such a simple task, this (in my opinion) is a simpler solution. I'll put psuedo code and you can convert to C

    create a simple for loop, going through each char in description

    for loop

    print one char in Description

    if (index+1) % 80 is 0 then

    print '\n';

    ok the % sign is called the modulo operator and what that does is it divides a number and returns the remainder, so 4 % 2 is 0 because 4 divided by 2 is 2 with no remainder. so if it divides by 80 and has no remainder we know that it is a multiple of 80

    now we do (index + 1) to solve two problems,

    #1 it avoids doing 0 % 0 which would cause a divide by zero erros

    #2 since arrays start at 0 then the 80th character would be 79, so we print array element 79 (the 80th character) and then print a newline, and we do this every 80 characters

  • 1 decade ago

    You must do it in two steps. First, define the file format specifying exactly what each byte of the file should be. Then write code to implement the file format you as you wrote it. The reason this is important is because otherwise, you may run into trouble when you try to read the file and have trouble figuring out which side is to blame.

    You could, for example, make the first line consist of a number, the number of lines the string is spread over. Then the next that many lines would contain parts of the string. So if the string was "I am happy." You could encode it like this:

    3

    I a

    m hap

    py.

    Then when you read it back, you read the first line and know that you need to read the next 3 lines.

    Why can you only write 80 columns? That's an extremely unusual limitation. Is this file meant to be read by a person or another program? If a person, you may want to specifically break it in "nice" places, like the way paragraphs are broken as you type in Y!A.

  • 1 decade ago

    I'll explain the gist...

    Create an integer variable as an indexer, initialize it to 0.

    Then you will loop, and each time the loop executes, you will read 80 characters and store them in an array. Keep looping until the entire array is processed. You then have a bunch of arrays that each have 80 characters, or one line of your Description[500].

    "I leave the implementation in C to the reader as an exercise."

    Source(s): Daily programming
  • ?
    Lv 4
    4 years ago

    There are 2 consumer-friendly a thank you to do it:      std::cout << " n"; and      std::cout << " " << std::endl; whether, you are going to be conscious that the 2nd line is only shorthand for      std::cout << " " << 'n" << std::flush; , which ability it outputs n (the transportable end of line, whether there's a faux rumor that endl is someway extra transportable than n), and then outputs the IO manipulator std::flush(), which forces the output buffer to transmit all its contents to the OS for exhibit. Flushing of cout is finished right away (by ability of default, it is grew to become off) once you study something from cin or while this technique terminates. Flushing of fstream buffers is finished right away while the document is closed. Repeated flushing is the two pointless and extremely sluggish, so once you're searching for swifter a thank you to do it, use n.

  • How do you think about the answers? You can sign in to vote the answer.
  • Anonymous
    1 decade ago
Still have questions? Get your answers by asking now.