Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

C program that creates Folder  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help on how to write a program that creates a folder and inside of that folder is .c file and .h file
 
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you already find out? And which operating system is it? For instance, on Linux the system calls is mkdir and on Windows it's CreateDirectoryA or CreateDirectoryW (and there actually a few more as well...).
 
David Simic
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something using FILE in c, does not matter which operating system
 
Rob Spoor
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Simic wrote:does not matter which operating system


Well it does. Creating directories and files are done through system calls which are not the same on all operating systems. My example on the directory creating calls already shows that.
 
David Simic
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:

David Simic wrote:does not matter which operating system


Well it does. Creating directories and files are done through system calls which are not the same on all operating systems. My example on the directory creating calls already shows that.



Well, in that case use Windows.
 
Saloon Keeper
Posts: 9710
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you can include Fileapi.h and use the functions that Rob linked to.
 
David Simic
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help, i found out another way that works

I will post it here just in case if someone else is looking for a same problem:

 
Stephan van Hulst
Saloon Keeper
Posts: 9710
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This application is a buffer overflow waiting to happen. What if the file name, directory name or the combination of those two is larger than 25 characters?
 
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minor point - don't use malloc(); just use something like:
Use snprintf() instead of strcpy() and strcat() to avoid destination array overflow eg.:
and you could(/should) use the snprintf() return value to check for truncation.

And there's a bug:
is equivalent to:
whereas you want:
If you used gcc with -Wall you would get compiler warning 'assignment makes pointer from integer without cast'.
 
Ranch Hand
Posts: 463
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pet peeve are messages like "can't create directory" and "can't create file".  What file/directory can't you create?  The APIs return error codes, use them so I know what the problem is.  For example, "can't create /foo/bar/blatz: permission denied".  Isn't that much more helpful than "Can't create file"?


 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!