• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

fopen - need help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
function fopen - Is it possible to have the path (filename) like an input. In my case the path to the file must be written by the user of the program. How can I do it? thanks
 
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jakub

Yes. The first argument to fopen() is a char pointer which points to the file path string. fopen() doesn't care where that file path has come from - it can be a fixed string eg. "dir/file.dat", or a char array whose contents (string) have been entered by the user.

Do you know how to read a string from the user? Do you have some code you can show us?
 
Jakub Novak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Jakub

Yes. The first argument to fopen() is a char pointer which points to the file path string. fopen() doesn't care where that file path has come from - it can be a fixed string eg. "dir/file.dat", or a char array whose contents (string) have been entered by the user.

Do you know how to read a string from the user? Do you have some code you can show us?



Thanks, I figured it out. Your advice actually really helped me.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fopen takes two params, first one is filename and another one is opening mode.
Declare a temporary char array.
Take input from user using scanf, then check whether the file exists or not.
If exists then continue doing the work.
Refer the following blog posts for more on the tasks you are doing
https://scholarsoul.com/fopen-in-c/
https://scholarsoul.com/fclose-in-c/

Check for file existence and do input validation.
 
John Matthews
Rancher
Posts: 259
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rusa - I don't know if you are connected with that website, but having looked at it I don't recommend people use it to learn C.

For standard library functions such as fopen() the best place to start is its linux man(ual) entry eg.
https://man7.org/linux/man-pages/man3/fopen.3.html

Then if you want more help in the form of examples/tutorials I would suggest using google to find an established website that doesn't include the word 'blog'
 
Saloon Keeper
Posts: 22801
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man pages are the Gold Standard for C language documentation on Unix and Linux OS. Note that depending on which OS you're talking about some details may vary, so it's best to get your info from the manual for your OS (Linux).

Obviously the quickest way to get that info is from your local Linux command line via the "man" command. You can get help on "man" using the command "man man", as that brings up the manual on man itself. Online, a good reference is die.net: https://linux.die.net/man/

The man help facility is considered archaic as it's not the best viewer for long complex functions or function libraries, so there's also the hyperlink-navigable info command.
 
Don't MAKE me come back there with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic