Thread: Read from txt and write in an external program

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Smile Read from txt and write in an external program

    Hello,

    I have an excel file which is being used as a database. A new software was developed to replace this excel file and I want to avoid typing all the information again. So I want to make a program that will do all this for me.

    I'm thinking in putting everything in a txt file and then the program will read this (I know how to do that). But then there are my questions:

    - How do I tell to open the new database program?
    - How can I use hotkeys? (to access the desired options)
    - How can I use TAB and Enter to change between fields and confirm?
    - How can I write the data?

    Thanks in advance!

    PS: My C knowledge is basic-intermediate

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you mean you want to operate this program' GUI interface "automatically" you are probably out of luck, and might as well stop thinking about that right now.

    However, it should not be hard to write a program that will translate a file of one type into a file of another type, as long as you can find out (or figure out) how those two file formats are structured. If it is easier to dump the excel file to text first, do that.

    What is the other program? If it is SQL based, you are in luck...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Yeah, I was thinking in automatically operating the GUI. Too complex?

    Well, converting the files would suit my needs. It is SQL based.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sombrancelha View Post
    Yeah, I was thinking in automatically operating the GUI. Too complex?
    It would *probably* require modifying the program source and certainly some intimate knowledge of it. That will be much much harder than what I'm about to recommend, and it may still be impossible. It would also be slow, but amusing to watch in action, maybe.

    Well, converting the files would suit my needs. It is SQL based.
    I know there is an SQLite library for C, and there is probably one for mySQL too*. SQL is actually a (very simple) language; chances are your software has a Command Line Interface or some means of accepting text instructions in "Structured Query Language". If you had to type every line yourself, it would be even slower than using the GUI. But it makes it very easy to automate in exactly the manner you want.

    AFAIK the specifics of SQL are slightly different between (eg) SQLite and mySQL, but here is an example of an SQLite command:
    Code:
    create table Songs (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    title TEXT,
                    artist_id INTEGER,
                    album_id INTEGER,
                    kb_len INTEGER,
                    added INTEGER );
    To add rows you use a command like "insert" and to do queries, "select". The GUI uses those internally.

    So it will take some experimentation and research but it will not be that hard. Probably you want to make up your own little playtest databases and then examine them in the GUI to make sure they are compatible and work the way you intend.

    * such a library allows you to write the db file directly without using any other software.
    Last edited by MK27; 06-01-2009 at 03:53 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Export to csv (comma separated value) in excel and then import the csv file into the program of your choice. I would strongly recommend you use Python if you want to parse these data files. Python tutorial Here's an example of how to create a matrix of the cells from a csv file.

    Code:
    # open the file and store it in "spreadsheet"
    spreadsheet = open("filename.csv")
    # split "spreadsheet" into a one dimensional list of rows
    spreadsheet.split("\n") 
    # split those rows into columns, creating a matrix (two dimensional list)
    index = 0
    while index < len(spreadsheet):
        spreadsheet[index] = spreadsheet[index].split(",")
        index += 1
    # BAM a matrix of cells, Python is the perfect language for this kind of stuff
    EDIT:

    Or if you want to do it with one line:

    Code:
    spreadsheet = [row.split(",") for row in open("somename.csv").split("\n")]
    Now is that a powerful language or what? It beats the s*** out of Perl and Ruby, the two languages closest to Python.
    Last edited by ITAmember; 06-01-2009 at 04:13 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ITAmember View Post
    It beats the s*** out of Perl and Ruby, the two languages closest to Python.
    Only someone with insecure feelings would say this, since I doubt you are equally familiar with perl, python and ruby*. I think English beats the hell out of French, but my French is very bad, so it might be better to say, my English is much better than my French.

    @ sombrancelha: On a more serious note, it is true that this will be a much shorter, simpler task in a language like Python, about which I hear many good things inc. the fact that it is very easy to learn if you have some knowledge or understanding of Object Oriented Programming. So that is something to consider.

    Your most important priority should be the availability of an appropriate SQL library, which you will need to write the database. However, most likely *all* of these languages already have an SQLite and mySQL API, so that should not be hard.


    * The reason I doubt this is because your one liner looks almost identical to a functionally identical one liner in perl or ruby. All it does is place the file into an array of lines and break each array element/line into another array. That's pretty much run of the mill modern OO scripting, nothing particular to python...but with python, no CGI or web programming ;P
    Last edited by MK27; 06-01-2009 at 05:22 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Quote Originally Posted by MK27 View Post
    Only someone with insecure feelings would say this, since I doubt you are equally familiar with perl, python and ruby*. I think English beats the hell out of French, but my French is very bad, so it might be better to say, my English is much better than my French.
    Actually I have quite a bit of experience in all three, I started with Perl, then went to Python, then tried out Ruby. I ditched Perl because of the disgusting syntax that makes it impossible to maintain after about 100 lines of code, ditched ruby because it forces OOP on you and the syntax can also be quite nasty. Python has very elegant syntax that, in a nutshell, forces you to produce readable code.

    So that is a statement of a well educated person in the subject which implies that Python is better. Everybody who has used all three languages would agree that Python is easier to learn and read than Perl and most would say the same thing about Python and Ruby. (most would also argue Python is better than those two languages)

    Out of those three languages Python has the best web programming tool set. Google uses Python for everything from gmail to youtube.

    Also, you do not need any former knowledge of OOP to use Python as you can chose between functional programming and OOP, it does help though.

    Hope you don't take offense to this post.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ITAmember View Post
    Hope you don't take offense to this post.
    No, glad to hear it altho it means I get more egg on my face instead of yours Maybe.

    Love perl and will never give it up. I don't see why it would seem "unmaintainable" in relation to other languages. I would generally not recommend it because the syntax, etc, is weird and unique. Uniqueness does make unique things possible, of course, but it does make it dissimilar, somewhat out of paradigm, so harder to assimilate. As a paradigm unto itself, there are doors and possibilities there that do not exist in more generic languages.

    I suppose almost every language can and is used server side, thanks for exposing my ignorance there (I think I already knew this about google and had forgotten). I just started learning ruby because it can be embedded client side, and this is widely available and supported. Googling around it looks like this may actually have been derived from embedded python, but I don't know if that is very common or how well the later would be supported if you are looking for a site host. You can embed perl in HTML too, but I doubt many hosts will support it...feel free to put me in my place about all this, of course, I'm no pro. You might even change my mind. I will take all my Rails books back to the library and "grab the snake".

    Quote Originally Posted by ITAmember View Post
    Python has very elegant syntax that, in a nutshell, forces you to produce readable code.
    After arguing about this kind of thing way too much and observing it even more (some guy just started a thing about adopting "Pythonic Indentation" at a RonR forum, and it's grown to 200+ posts in less than a week. totally absurd. It's about indentation, and whether you like brackets and what not.), I've finally decided I could not feel very strongly about this issue in this sense. You could write "beautiful, elegant, readable code" in BASIC. So what? I guarantee that there is no python code in the world that would look beautiful, elegant and readable to me right now. In a week, maybe it could be fresh food -- yum! But neither your previous post nor anything else I could look at in python, or perl, or C, or C++, or ruby, or anything else I have ever heard of could possibly make me go:

    "Wow! That doesn't look like computer code at all! That just looks so beautiful, elegant, and readable I can hardly believe it! Miles apart from the normal programming tish! Wonder of wonders!"

    Beyond asm (now there's elegance!*) it all pretty much follows the same general concept, methinks. Nobody, or almost nobody, studies a language unless somebody has already done something "more impressive than you will ever do with it", anyway. If it makes you feel more beautiful, elegant, and readable, power to you ;{

    *probably. I just don't see it
    Last edited by MK27; 06-01-2009 at 06:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    There are two features that, in my opinion, make Python clean and readable.

    1. Compound statement blocks (logic tests, loops, functions, classes, etc) are defined with indentation instead of brackets. This forces you to do something you should be doing anyway and what most of use distinguish compound statements with. This gets rid of syntax characters that only serve to help the compiler. (unless you don't use indentation, in that case you should be burned at the stake)
    2. All unneeded syntax characters are removed, pointers are an example, semicolons are another example. This lets you read English looking programs and one of my non-programming friends could even understand a simple Python program.

    I would highly recommend everybody who reads this should at least look at Python. It's very easy to learn and one of the most powerful and flexible languages today.

    Yeah, I agree that readability can be interpreted many different ways but I would say most people consider Python extremely easy to read and understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write to txt file
    By Greystoke in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2008, 06:22 AM
  2. Replies: 2
    Last Post: 12-02-2007, 05:40 AM
  3. Replies: 5
    Last Post: 10-08-2004, 08:12 AM
  4. write(), read()
    By RedRum in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2002, 11:45 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM