Accessing files from a C program.

This is a discussion on Accessing files from a C program. within the C Programming forums, part of the General Programming Boards category; Hello all, I'm new to the forums and have very little programming knowledge. I've read some of the tutorials on ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Accessing files from a C program.

    Hello all,

    I'm new to the forums and have very little programming knowledge. I've read some of the tutorials on this website for C programming. They certainly aid in my attempt to make a program. The only thing I can't figure out is how to make the C program open up and run a separate file.

    What I'm trying to do is execute a batch file using my C program. I'm certain it's not simple to do however I'm still trying to do it. Does anyone know a method I can use to call up and execute the batch file from my C program?

    For general info I am using the free DJGPP compiler from Deloire. All programs compiled through it (at least by me) are executed in MS-DOS.


    Thanks in advance for any assistance.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The simplest way? Just use system().
    Code:
    #include <stdlib.h>
    system("something.exe");
    Whatever you pass to system() is executed just as if you'd typed it on the command line. Oh, and be sure to include stdlib.h if you want to use system().

    For more information, see this FAQ. http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    [edit] Any particular reason you're using DJGPP? It's pretty good, but in my opinion Dev-C++ is better, and Code::Blocks better yet (if you have a good enough computer). [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    I seriously doubt you are executing in MS-DOS. What you're probably doing is running console apps within your OS, be that Windows or Linux or whatever.

    [edit] Any particular reason you're using DJGPP? It's pretty good, but in my opinion Dev-C++ is better, and Code::Blocks better yet (if you have a good enough computer). [/edit]
    Or better yet, Visual Studio
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You might actually be wrong, Elysia. DJGPP, unlike most Windows compilers/IDEs such as MSVC and Dev-C++, actually generates pure DOS applications. They're a bit larger, but with the right TSRs, you can run them in pure DOS. Under a 32-bit DOS, of course. For 16-bit DOS, better go with Turbo C.

    However, if the OP's executing DJGPP in a console window, I guess that's not strictly DOS. Close enough -- it's what passes for DOS these days.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    A ... dos compiler? Funny.
    And MS-DOS these days? Even more humorous!
    Really? The thing is ancient and full of all kinds of crap. No one would seriously use that crap anymore.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    DJGPP is actually not bad. It was my favourite compiler for a while there. Only when I was programming in mode 13h, but still.

    For a DOS compiler, it's luxury -- copy/paste, mouse support, variable screen sizes, syntax highlighting. It's based on a port of gcc, so you have gcc, gxx (a.k.a. g++), make, and some utilities like mv and cp and rm. It's really quite nice. It runs pretty well on a ~60MHz laptop with 48MB of RAM.

    Turbo C is the pits by comparison. But it fits on a 720KB floppy disk and runs on a ~4MHz laptop with DOS 3.21.

    I think I may need to get some faster computers . . . Just kidding!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    DJGPP lets you program 32-bit application in a 16-bit OS. It lets you access more memory, etc etc, with the advantages of 32-bit programs. DJGPP is not crap. It just compiles for DOS, which is what it was made for.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    I'll use that script you mentioned dwks. DJGPP works fine for me, and since it's free it's even better. If anybody out here can recommend another compiler that would allow me to compile my C programs that run separate from MS-DOS and allows you to make your own program icon for it that would be great. Remember, free is great!

    EDIT: The script works, thanks dwks!
    Last edited by Sea (C) Maniac; 12-21-2007 at 10:04 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Visual Studio 2008 for one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Dev-C++, Code::Blocks (Credit to Salem)

    ssharish

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Thanks Guys,

    I got the Dev C++ Compiler. And yes, it supports C.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    A ... dos compiler? Funny.
    And MS-DOS these days? Even more humorous!
    Really? The thing is ancient and full of all kinds of crap. No one would seriously use that crap anymore.
    People use it everywhere. One of the most common places you see real DOS is on point-of-sale terminals. See that screen that looks like DOS? That's because it is

    There are many places on the planet where modern computer equipment is difficult or impossible to obtain, but there is a decent supply of older machines. DOS or Linux is the only realistic option for such old hardware.

    DOS isn't dead by a long shot.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    In these modern countries, it kind of is... for PCs anyway. Not that it matters anyway....
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing .dat files within a folder
    By porsche911nfs in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2009, 10:26 PM
  2. Replies: 4
    Last Post: 06-11-2004, 06:18 PM
  3. Replies: 3
    Last Post: 04-19-2004, 08:09 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21