format .txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anna1230
    New Member
    • Nov 2006
    • 2

    format .txt file

    I have a .txt file as following:

    012|23456|sourc e|N|
    01|23453|code|Y |
    2349|234|write| N|

    what I want to do is to format the file like following:

    012 23456 source N
    01 23453 code Y
    2349 234 write N

    I want to use C++ to replace "|" with " " and make each column align left. Can anyone help me about it? Thank you so much.
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Editted: see below

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      A (reaosnably) easy way without worrying about filehandling would be:

      Code:
      #include <stdio.h>
      
      int main (char *args)
      {
        char input=getchar();
        while(input!=null)
        {
          if(input=="|")
          {
             putch(' ');
          }
          else
          {
             putch(input);
          }
        }
      }
      This file uses standard inut, but you should (in a unix environment) be able to pipe input and output to files ie: (assuming you create a.out)
      a.out <inputtext >outputtext

      Comment

      • anna1230
        New Member
        • Nov 2006
        • 2

        #4
        I did it in a different way. Thank you anyway.

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          Fortunately I have now learnt to spell edited ;)

          Comment

          • Hero Doug
            New Member
            • Nov 2006
            • 16

            #6
            Would you mind posting what C++ function you used to do it,

            Comment

            Working...