473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

opening a file based on a variable.

I have a function that as one of its arguments is a variable for a file
name, how would I use the variable to fill in the file name when
calling fopen?

Nov 15 '05 #1
4 1211
Levi Campbell wrote:
I have a function that as one of its arguments is a variable for a
file name, how would I use the variable to fill in the file name
when calling fopen?


Exactly the same way you'd use a string literal for a fixed file
name.

If you have actual code that is failing, I suggest you post it
because your problem probably has nothing to do with fopen per se.

--
Peter

Nov 15 '05 #2
"Levi Campbell" <le*********@gmail.com> writes:
I have a function that as one of its arguments is a variable for a file
name, how would I use the variable to fill in the file name when
calling fopen?


void
function (const char *filename)
{
FILE *file = fopen (filename, "r" /* etc. */);
if (file != NULL) {
/* ...use file... */
}
}
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Nov 15 '05 #3
"Levi Campbell" <le*********@gmail.com> writes:
I have a function that as one of its arguments is a variable for a file
name, how would I use the variable to fill in the file name when
calling fopen?


By calling fopen() with the argument.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4
Levi Campbell wrote:
I have a function that as one of its arguments is a variable for a file
name, how would I use the variable to fill in the file name when
calling fopen?


As you would use a string as an argument to any other function taking a
string as an argument. For example,

#include <stdio.h>
int main(void)
{
FILE *fp;
char foobar[] = "random.filename.txt";
if (!(fp = fopen(foobar,"r")))
{
fprintf(stderr,"Oh crap! I couldn't open \"%s\" for input.\n",
foobar);
}
else fclose(fp);
return 0;
}
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lisa | last post by:
I have an excel file on my server, which contains an embedded query. I want to be able to open the excel file, pass the contents of my variable into a cell within the worksheet and then refresh the...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
16
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
3
by: sara | last post by:
I've been reading all the posts on this topic. Most are years old, so I have 2 questions: 1. Is there any improvement on opening the same report multiple times (with different input parameters...
2
by: Mad Scientist Jr | last post by:
>From an asp.net web page I want the user to open the results of a SQL query in Excel, as automatically as possible (ie not having to loop through columns, rows, in code). For this,...
9
compman9902
by: compman9902 | last post by:
Hello, and thank toy for reading this post. Thus far, this website has helped me a lot, (I have finally finished my encryptor: "site removed by moderator", go to the downloads link) and I will...
0
by: Jeremy Noring | last post by:
Hi, I have an application that loads various settings from the app.exe.config file. This has worked very well on w2k, XP, and Vista. However, recently I've been troubleshooting an issue on...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.