Thread: Need help with reading and writing a problem

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    4

    Need help with reading and writing a problem

    I havent programmed in quite a bit. Took some C courses in college and forgot most of the things I have learned and I am sure I can pick up a book and use references to do what I need done. I just am not sure how to approach this situation.

    I started a new job and they are a small startup and do not have in house developers. They are using employees to manually transfer information from a text file into an excel file. I am sure this can be done automatically.

    What is the logic to get this done? Here is a sample of the input file:

    """MM-139",1423/3000/TOT,USER DEFINED IN,COME,GRID,MERCHANT,INDUSTR,"Y, LLC",,"11/30/09 PAGE 2,773 """
    ,,MASTER FILE,LIS,TING,,,,,12/2/2009
    ,,,,,,,,,
    GRID ID,1,,,,,,,,
    ,,,,,,,,,
    USER,STATEMENT,MM-101,CHG,BASE,CHARGE,TERM,OPER,DATE,TIME
    DEFINED,DESCRIPTION,DESCRIPTION,IND,CALC,RATE,ID,C ODE,,
    1,WIRELESS FEE,WF,F,,25,TG0J,S03,9/25/2009,09:17:14:84
    2,ONETIME SETUP FEE,SETUPFEE,F,,25,TG0J,S03,9/25/2009,09:17:14:84
    3,AUG WIRELESS FEE,AUG WF,F,,25,TG0J,S03,9/25/2009,09:17:14:84
    ,,,,,,,,,
    GRID ID,74,,,,,,,,
    ,,,,,,,,,
    USER,STATEMENT,MM-101,CHG,BASE,CHARGE,TERM,OPER,DATE,TIME
    DEFINED,DESCRIPTION,DESCRIPTION,IND,CALC,RATE,ID,C ODE,,
    1,QUICKBOOKS SETUP FEE, QKBSETUP,F,,100,TO2U,MDC,6/29/2009,11:15:46:63
    ,,,,,,,,,
    GRID ID,195,,,,,,,,
    ,,,,,,,,,
    USER,STATEMENT,MM-101,CHG,BASE,CHARGE,TERM,OPER,DATE,TIME
    DEFINED,DESCRIPTION,DESCRIPTION,IND,CALC,RATE,ID,C ODE,,
    1,TERMINAL COST 1,TERM1,F,,799.99,TH25,MDC,6/24/2009,12:39:32:24
    2,TERMINAL COST 2,TERM2,F,,799.99,TH25,MDC,6/24/2009,12:39:32:24
    3,TERMINAL COST 3,TERM3,F,,799.99,TH25,MDC,6/24/2009,12:39:32:24
    4,SUPPLIES,SUPPLIES,F,,50,TH25,MDC,6/24/2009,12:39:32:24
    5,PLATINUM CLUB,PLATCLUB,F,,14.95,TH25,MDC,6/24/2009,12:39:32:24
    I need the output file to be an excel file and to read like this:
    Grid WIRELESS FEE ONETIME SETUP FEE AUG WIRELESS FEE
    1 25 25 25

    Then 74 should go under grid and QUICKBOOKS SETUP FEE should go after AUG wireless fee and under that column, on the same row as 74 should go 100

    Then 195 should go under grid and TERMINAL COST 1 should be a new colum after quickbooks setup fee and under TERMINAL COST 1 column, on the row numbered 195 should go 799.99

    The only other important information in contained in this file is 1423/3000/TOT and the date (in this case 11/30/09) and I want the file to be named 1423/3000/TOT - 11/30/09

    in this file that contains hundreds of pages this text that looks like this "1423/3000/TOT" changes and is found several times. Everytime the file reads this in can it create a new output file named as such with the new date?

    Any help is appreciated!
    This is what I have to do
    read in the file, store the necessary text and then output it appropriately. How do I output it in the method that I need.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    It would be simpler to just reformat this file with a c program and then import it into excel. Tons less work.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    4
    Quote Originally Posted by Dino View Post
    It would be simpler to just reformat this file with a c program and then import it into excel. Tons less work.
    How can I do that? What is the exact logic? I dont want to ask for the coding but a helping hand, some psuedo code perhaps...

    Thank you!!!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Write out, in outline form, the steps you do manually today to piece this information into the required format. And, lay out a pictorial, in grid/spreadsheet layout, that helps you visualize the end result. Then, you assign whatever rules or mechanical movements that need to happen, depending on values, to reach your goal.

    In other words, you define your input, you define your desired output, and in between those, you lay out the steps (logic) that needs to happen to achieve the result.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    4
    Quote Originally Posted by Dino View Post
    Write out, in outline form, the steps you do manually today to piece this information into the required format. And, lay out a pictorial, in grid/spreadsheet layout, that helps you visualize the end result. Then, you assign whatever rules or mechanical movements that need to happen, depending on values, to reach your goal.

    In other words, you define your input, you define your desired output, and in between those, you lay out the steps (logic) that needs to happen to achieve the result.
    I have done that, hence my elaborate introduction to my problem. Can anyone give me a starting point please?
    input file
    read the text
    write the text to the output file
    loop until end of input

    Does this sound correct?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can do this. I will help. Nice break from student's homework assignments.

    We want to take the output of the original data producer, in whatever form it is in, at that time, and make it into the format needed so it can be imported right into Excel.

    Do you know how to import a file into Excel? We want to set this up so that the data goes from the originator ==> your program ==> into Excel, with no human labor needed except a few keystrokes to tell Excel to import the file.

    As was mentioned above, we'll need two files:

    1) The original file - before it's had ANY formatting done to it, of any kind. Was your post above, the original format of the file?

    2) the final file ready to be imported into Excel. Just a sample for testing. It doesn't need to be the whole thing.

    How we get #2, is important. I need to SEE the data, **NOT** read a description of it. So how you get file #2 is very important.

    I'd like to get it right from Excel itself - that is, take a small sample, that is now ready to be formatted - and that will be our original file. Then get a small sample of data, from Excel, by exporting it.

    It's very important that the export from Excel have **NO** formatting info included. Comma separated variables style, is fine.

    Once I get those two files, I'll help you make the original file formatted into what Excel needs. It will be ready to import.

    It will be up to you to closely examine and test the final file, to see if it is all correct, and help with the code.

    Any data that you post here, be sure to put in between code tags. Otherwise, the forum's software will make hash of it.

    So, ready when you are.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there supposed to be a header for each sub item, or are your sub items lines of their owns? Your first example seems like you want to read the three sub items, and file them each in their own column next to grid. But your TERMINAL example confuses that issue. And no, you don't seem to have done outlining what you would do to split the data:
    Code:
    """MM-139",1423/3000/TOT,USER DEFINED IN,COME,GRID,MERCHANT,INDUSTR,"Y, LLC",,"11/30/09 PAGE 2,773 """
    ,,MASTER FILE,LIS,TING,,,,,12/2/2009
    ,,,,,,,,,
    GRID ID,1,,,,,,,,
    ,,,,,,,,,
    USER,STATEMENT,MM-101,CHG,BASE,CHARGE,TERM,OPER,DATE,TIME
    DEFINED,DESCRIPTION,DESCRIPTION,IND,CALC,RATE,ID,C ODE,,
    1,WIRELESS FEE,WF,F,,25,TG0J,S03,9/25/2009,09:17:14:84
    2,ONETIME SETUP FEE,SETUPFEE,F,,25,TG0J,S03,9/25/2009,09:17:14:84
    3,AUG WIRELESS FEE,AUG WF,F,,25,TG0J,S03,9/25/2009,09:17:14:84
    ,,,,,,,,,
    GRID ID,74,,,,,,,,
    ,,,,,,,,,
    USER,STATEMENT,MM-101,CHG,BASE,CHARGE,TERM,OPER,DATE,TIME
    DEFINED,DESCRIPTION,DESCRIPTION,IND,CALC,RATE,ID,C ODE,,
    1,QUICKBOOKS SETUP FEE, QKBSETUP,F,,100,TO2U,MDC,6/29/2009,11:15:46:63
    ,,,,,,,,,
    GRID ID,195,,,,,,,,
    ,,,,,,,,,
    USER,STATEMENT,MM-101,CHG,BASE,CHARGE,TERM,OPER,DATE,TIME
    DEFINED,DESCRIPTION,DESCRIPTION,IND,CALC,RATE,ID,C ODE,,
    1,TERMINAL COST 1,TERM1,F,,799.99,TH25,MDC,6/24/2009,12:39:32:24
    2,TERMINAL COST 2,TERM2,F,,799.99,TH25,MDC,6/24/2009,12:39:32:24
    3,TERMINAL COST 3,TERM3,F,,799.99,TH25,MDC,6/24/2009,12:39:32:24
    4,SUPPLIES,SUPPLIES,F,,50,TH25,MDC,6/24/2009,12:39:32:24
    5,PLATINUM CLUB,PLATCLUB,F,,14.95,TH25,MDC,6/24/2009,12:39:32:24
    Assuming this is the valid file--where's the "text file" coming from anyway, since you mention that they manually transfer from a text file to xls? Anyway, assuming this is valid:

    Read first line. Ignore up through the first , then read through next , to get prefix of file name. Skip through the next eight , and then skip the next single character (the ". Scan for date. That is the suffix of the file name. Discard remainder of line. -- This job seems suited for strtok with , as your delimit.

    Now you have a string, open that as your file name for writing.
    Skip a line of your input file. You should now be at the first of the grid format block, which seems to always be:
    ,,,,,
    grid ...
    ,,,,,
    Code:
    while not at end of input file
        /* read grid */
        skip line
        scan for "grid id," then read a number, ignore the rest
        skip a line
        store that pair
    
        while read a line isn't eof and first character is a number
            skip through first ,
            store to next ,
            skip through three ,
            store number
    
        for each stored pair
            write stored word then something like a ,
        write a neline
        for each stored pair
            write stored number then something like a ,
        write a newline or two, whatever
    Something like that. See, that's what they really meant when they said you need to write out the steps to extract the data. You need to study it, and see what it is you really need to skip, and what you really need to keep, and see how you can easily do that.

    Now let's see your version of it, try getting the basics down--like for example, how to construct the file name string, that would be a good start.



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

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Dino View Post
    Write out, in outline form, the steps you do manually today to piece this information into the required format. And, lay out a pictorial, in grid/spreadsheet layout, that helps you visualize the end result. Then, you assign whatever rules or mechanical movements that need to happen, depending on values, to reach your goal.

    In other words, you define your input, you define your desired output, and in between those, you lay out the steps (logic) that needs to happen to achieve the result.
    This is good advice. You need to be very clear on the steps to transform the input data into the output data before you write the code. Half of the "development" for this can be done without writing any lines of actual code. It's largely in working out what the actual business logic translates into in terms of manipulation of the file contents.

    You could perhaps show the exact and entire output that would come from the snippet you've given, and show where each bit comes from, possibly by using different colors and using the same colors in bits of the input data.
    From that you not only have the information about how to transform the data but you also have a set of test data and the test result. You can use this input and output to ensure that the final program is working correctly. Keep this data around as your unit test data as it will be useful to retest the program if you were to say try and optimise it at a later date.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    4
    Quote Originally Posted by iMalc View Post
    This is good advice. You need to be very clear on the steps to transform the input data into the output data before you write the code. Half of the "development" for this can be done without writing any lines of actual code. It's largely in working out what the actual business logic translates into in terms of manipulation of the file contents.

    You could perhaps show the exact and entire output that would come from the snippet you've given, and show where each bit comes from, possibly by using different colors and using the same colors in bits of the input data.
    From that you not only have the information about how to transform the data but you also have a set of test data and the test result. You can use this input and output to ensure that the final program is working correctly. Keep this data around as your unit test data as it will be useful to retest the program if you were to say try and optimise it at a later date.
    I will work on the coding as best as I can. Look for my coding and many questions that follow lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. reading file problem
    By samsam1 in forum Windows Programming
    Replies: 4
    Last Post: 01-15-2003, 06:03 PM