Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. fixed width text file

fixed width text file

Scheduled Pinned Locked Moved C#
questioncsharp
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Iman Mohtashemi
    wrote on last edited by
    #1

    Hello, How do I write a fixed width text file (say 80 characters per line) using the streamwriter in C#? shekee

    K S 2 Replies Last reply
    0
    • I Iman Mohtashemi

      Hello, How do I write a fixed width text file (say 80 characters per line) using the streamwriter in C#? shekee

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Seeing as you asked a general question, here is a general answer: Prepare the string for the current line. If it is too long, truncate and use the remainder for the next line. If it is too short, pad it with spaces. Repeat until you have written everything. To write to a file you'll need System.IO, there is a TextWriter class in there that you can use.

      ragnaroknrol The Internet is For Porn[^]
      Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      I 1 Reply Last reply
      0
      • K Keith Barrow

        Seeing as you asked a general question, here is a general answer: Prepare the string for the current line. If it is too long, truncate and use the remainder for the next line. If it is too short, pad it with spaces. Repeat until you have written everything. To write to a file you'll need System.IO, there is a TextWriter class in there that you can use.

        ragnaroknrol The Internet is For Porn[^]
        Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

        I Offline
        I Offline
        Iman Mohtashemi
        wrote on last edited by
        #3

        Sorry, What is the exact code to write a fixed width text file in C#? example: instead of this: IPLPPHPGHPGYINFSYEVLTPLKWYQSMMRHEYPSYGYEPMGGWLHHQIIPVLSQQHSPSHSLPPQHHIPI I want: IPLPPHPGHPG YINFSYEVLTP LKWYQSMMRHE YPSYGYEPMGG WLHHQIIPVLS QQHSPSHSLPP QHHIPI

        K 1 Reply Last reply
        0
        • I Iman Mohtashemi

          Sorry, What is the exact code to write a fixed width text file in C#? example: instead of this: IPLPPHPGHPGYINFSYEVLTPLKWYQSMMRHEYPSYGYEPMGGWLHHQIIPVLSQQHSPSHSLPPQHHIPI I want: IPLPPHPGHPG YINFSYEVLTP LKWYQSMMRHE YPSYGYEPMGG WLHHQIIPVLS QQHSPSHSLPP QHHIPI

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          I gave you a basic algorithm to do exactly what you want (and information about file IO) , I'm not going to write the code for you, that is your job / assignment. No-one is going to give you the code here, please read the FAQs and Sickies at the top of the forum.

          ragnaroknrol The Internet is For Porn[^]
          Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

          1 Reply Last reply
          0
          • I Iman Mohtashemi

            Hello, How do I write a fixed width text file (say 80 characters per line) using the streamwriter in C#? shekee

            S Offline
            S Offline
            Samuel Cherinet
            wrote on last edited by
            #5

            Try this code ...

            FileStream fs = new FileStream("<Full Path FileName>", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            string message = "<TextToWrite>";
            int charcount = 0;
            foreach(char ch in message)
            {
            sw.Write(ch);
            charcount++;
            if (charcount % 80 == 0)
            sw.WriteLine();
            }
            sw.Close();

            I 1 Reply Last reply
            0
            • S Samuel Cherinet

              Try this code ...

              FileStream fs = new FileStream("<Full Path FileName>", FileMode.OpenOrCreate);
              StreamWriter sw = new StreamWriter(fs);
              string message = "<TextToWrite>";
              int charcount = 0;
              foreach(char ch in message)
              {
              sw.Write(ch);
              charcount++;
              if (charcount % 80 == 0)
              sw.WriteLine();
              }
              sw.Close();

              I Offline
              I Offline
              Iman Mohtashemi
              wrote on last edited by
              #6

              Thanks! I'll try it

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups