How do I fopen a file in %windir% path

This is a discussion on How do I fopen a file in %windir% path within the C Programming forums, part of the General Programming Boards category; Hello to all. I searched alot for this issue, but couldn't find an answer. My requirement is to open few ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    24

    How do I fopen a file in %windir% path

    Hello to all.
    I searched alot for this issue, but couldn't find an answer.
    My requirement is to open few files in a C script that will be used to collect data & then display on screen.
    I am using system command in my script to run few DOS commands, for example:

    system("systeminfo > %windir%/web/wallpaper/sys_info.txt");

    (I am using this directory since this is the only writable place in my domain computers. And I am using %windir% since I can't assume that C: is the default directory for OS on all computers)
    But now how do I open this file in read mode (or any mode)?

    I am not able to open file using:
    FILE=*fp("%windir%/web/wallpaper/sys_info.txt","r");

    Any help will be greatly appreciated. I am in hurry to correct this script. Thanks alots in advance.
    - Kriss332

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Look up the function getenv() in stdlib.h. getenv("windir") retrieves a string containing the expansion of the environment variable windir.

    I'd suggest looking at alternative ways of getting the information you seek. A system() call should be an absolute last resort, because it is VERY easy to circumvent (an attribute that is inadvisable in a program that will be executed on multiple computers in a domain, as it can present a security risk). Look up appropriate win32 API functions - Microsoft provides a slew of documentation, starting here. I might take a bit more effort than writing a bat file, but the benefits (less security risk, not having to write to a file accessible to all computers in a domain, etc) will be significant.

    And there is no such thing as a C script. C is a compiled language, and a complete working program is conventionally described as a program.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    24
    Thanks 'grumpy'. A nice guidance. But I have not completed C program learning completely yet. And Win APIs are abit far for me now. I have already enough burnt my brain struggling with strings and creating countless loops to get this program (will not call it script) working. It would be a great help if you could give me a small tut on using getenv(); . I still have few sorting functions to create in program. As of now I'll have to launch this program only. I'll modify it after some time(after I finish with Win APIs). Thanks alot...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    All you need to do is read up on the function getenv(). Documentation for that function normally includes a sample of how to use it. You don't need anything like a "small tut".

    Learning how to read function specifications (a description of what it does, what arguments it accepts, and what it returns) is an elementary skill needed by all C programmers, so you'd better take time to learn it. The win32 API documentation requires similar skills (as the documentation lists a set of functions with a description of what they do, what arguments they accept, and what they return).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 10-14-2011, 01:31 PM
  2. Using fopen() with array containing path
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:23 AM
  3. Replies: 7
    Last Post: 06-16-2008, 01:18 PM
  4. problem with fopen and absolute path
    By kobra_swe in forum C Programming
    Replies: 19
    Last Post: 04-17-2008, 01:31 AM
  5. %windir%
    By krappykoder in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-09-2002, 10:19 AM

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