![]() |
|
#1
|
|||
|
|||
Simple question about file openingI have very limitied C++ knowledge, so please forgive my ignorance.
When using the fopen command, the first argument is the file name in brackets, ie CPP / C++ / C Code:
my question is how do you make it so that this file name can be input by the user? fopen will not accept a string as the first argument, so is there any way around this? I know when using ifstream, you can use a string by adding .c_str() to the end, ie: CPP / C++ / C Code:
where inData is of type ifstream, and FileName is of type string, but this doesn't seem to work with fopen. Thanks for any help. |
|
#2
|
|||
|
|||
|
Quote:
I'm not sure why you would want to use <cstdio> functions in c++, but it's certainly "legal" to do it if you want to. Why not just use <iostream> and <fstream> functions? However, to answer your question: Here's an example CPP / C++ / C Code:
So you can use a string with c_str() as an argument to fopen(). Regards, Dave |
|
#3
|
|||
|
|||
|
Yes I know it doesn't make sense to use that when ifstream is available, the problem is I'm using a subprogram made by my prof which requires an input of type FILE *, so I have to do it this way. He tends to use a lot of stuff from C and it gets quite confusing. Anyways thanks for the help. The only part I don't understand is the whole char buffer thing. What's the purpose of that? What does it do?
|
|
#4
|
|||
|
|||
|
Quote:
In the C language there is no such thing as a "string" data type. In C programs, strings are represented by null-terminated sequences of chars. Standard library functions (strcpy(), strcat(), strlen(), printf() with %s format specification, etc.) all work on arrays of chars. The function fgets() reads chars from the input file into an array of chars. The extraction operator (cout << ) is overloaded so that if it is given the name of an array of chars as an argument, it prints out the chars as a string. Some people say that the best way to learn C++ is from the ground up (that is, they say it's better not to learn C first). That's all well and good, for some people, but they invariably (in my experience) get caught up in arrays of chars, pointers to chars, etc., when they have to write programs that interact with standard Windows API functions, for example, that require C-style "strings" rather than C++ string objects. Hang in there! Regards, Dave "You are in a maze of twisty little passages, all alike..." |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
| Re: Programming Techniques | WaltP | C Programming Language | 0 | 10-Mar-2004 00:56 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The