Working with files and directories on Windows

P

pkirk25

Problem 1: I need to access a set of files all with same name and all
in subdirectories of the directory I run the executable in.

Problem 2. I need to edit a file but want to create a .bak first.

Are these possible for a C beginner or do they require the sacrifice of
of a young virgin and buying books on the Win32 API.
 
A

Ancient_Hacker

pkirk25 said:
Problem 1: I need to access a set of files all with same name and all
in subdirectories of the directory I run the executable in.

see the description of the "system()" function. You can call it as:

system( "dir /B dirname\*.dat >\tmp.tmp" );


... that will put all the filenames in \tmp.tmp. Youc an then fopen()
tmp.tmp and read the file names from it, then fopen those files.
Easy.
Problem 2. I need to edit a file but want to create a .bak first.

fopen() it for reading fopen the .bak for writing, and read and write
all the bytes from one to the other. then close the files.

Some programs do it more quickly by just renaming the file.
 
K

Keith Thompson

Ancient_Hacker said:
see the description of the "system()" function. You can call it as:

system( "dir /B dirname\*.dat >\tmp.tmp" );

.. that will put all the filenames in \tmp.tmp. Youc an then fopen()
tmp.tmp and read the file names from it, then fopen those files.
Easy.

The language doesn't define a "\*" escape sequence. "\t" is a
horizontal tab character.

There's almost certainly a better system-specific way to do this, and
since the "dir" command is system-specific anyway, nothing is lost by
using a more efficient method. (You're also assuming that "\tmp.tmp"
is writable.)

Ask in a Windows-specific newsgroup.
fopen() it for reading fopen the .bak for writing, and read and write
all the bytes from one to the other. then close the files.

Or use some system-specific command that copies the file.

Ask in a Windows-specific newsgroup.
Some programs do it more quickly by just renaming the file.

Yes, rename() is a standard C function.
 
P

pkirk25

Keith Thompson wrote:
renaming the file.
Yes, rename() is a standard C function.
http://www.elook.org/programming/c/rename.html

I haven't learnt how to use it but it looks like the answer.

My little program is a mod of World of Warcraft which is Mac, Linux and
Windows. The system command works fine on Windows. Thats 97% of
potential users catered for.

Is system("do something please now!") just as easy on Mac and Linux?
I'd like to know before I ask someone to install WoW and give me shell
access.
 
K

Keith Thompson

pkirk25 said:
Keith Thompson wrote:
renaming the file.
http://www.elook.org/programming/c/rename.html

I haven't learnt how to use it but it looks like the answer.

My little program is a mod of World of Warcraft which is Mac, Linux and
Windows. The system command works fine on Windows. Thats 97% of
potential users catered for.

Is system("do something please now!") just as easy on Mac and Linux?
I'd like to know before I ask someone to install WoW and give me shell
access.

The system() function is standard. The argument to the system()
function is entirely system-specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top