• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

fopen - need help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 508
15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28486
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fopen( ) function is used to create a new file or to open an existing file. This call will initialize an object of the type FILE, which contains all the information necessary to control the stream.

For example:

FILE * pFile;
 pFile = fopen ("filename.txt","modename");

Here, filename is a string literal, which you will use to name your file, and access mode can have one of the following values −r,r+,w,w+,a,a+.

I hope this will helps you.
 
Let nothing stop you! Not even this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic