Thread: read in data, manipulate, and print back out

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    read in data, manipulate, and print back out

    Hi,

    I have some data in the form

    1 2 3
    4 5 6
    7 8 9

    in a txt file.

    How would I go about converting it into a list like:
    1
    2
    3
    4
    5
    6
    ...etc.

    and printing that to another txt file?

    Thanks!!

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Maybe something like this:

    1. Read the file to a string
    2. Replace any consecutive groups of 1 or more whitespace chars with a single '\n'
    3. Write the string to a file


    This may help you with file I/O (don't pay attention that is says C++ Reference):

    cstdio (stdio.h) - C++ Reference

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Even more simple:
    1. Create output file
    2. Read a character from input file
    3. If it is a <space> right a <newline> character in you output file. Otherwise write the character to your output file as it is
    4. Repeat step 2, 3 until EOF

    This should just take a few lines of code

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    That does seem more efficient.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You probably don't want to do this, at all - how's *that* for efficiency?

    Nobody (probably) cares how the data is arranged on the drive, unless it's part of a struct or record, which this is not. All they want is to view the data, like:

    1
    2
    3
    etc.

    So:

    Code:
    fscanf(ptrFile,"%d %d %d", &num1, &num2, &num3);
    printf("%d\n%d\n%d\n, num1, num2, num3);

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Hi,

    Read in the data from the original text file and the replace all the character ' ' with '\n'.
    Then put it in another file. That's it!

    Carle
    _______________
    Free chat software for you

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you just going through threads and repeating everything everyone else has already said?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ...not to mention threads old enough to be effectively dead...

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    Why are you just going through threads and repeating everything everyone else has already said?


    Quzah.
    I'm sure it couldn't possibly have anything to do with the link in his signature.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    MTK how would one go about doing your second step
    Replace any consecutive groups of 1 or more whitespace chars with a single '\n'
    Im having issues with that on my program, but i need it for more than just whitespace, I need more than one whitespace to be combined with commas tabs, etc to return only one '\n'

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (c = fgetc( somefile ) != EOF )
    {
        if( isdigit( c ) )
        {
            ...do whatever for digits...
        }
        else
        if( isspace( c ) )
        {
            while( (c = fgetc( somefile )) != EOF && isspace( c ) ) ;
            ...do whatever to output your newline...
        }
        else
        {
            ...do whatever for nonspace nondigit characters...
        }
    
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. Storing # from Data File in Arrays?
    By dakarn in forum C Programming
    Replies: 7
    Last Post: 11-13-2008, 09:15 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Help!!!!!!!!
    By Shy_girl_311 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 02:22 PM