GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jun-2005, 16:10
eddo eddo is offline
New Member
 
Join Date: Jun 2005
Posts: 2
eddo is on a distinguished road

Simple question about file opening


I 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:
 fopen("file.txt", "r"); 

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:
 inData.open(FileName.c_str()); 

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  
Old 09-Jun-2005, 19:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by eddo
I 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:
 fopen("file.txt", "r"); 

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:
 inData.open(FileName.c_str()); 

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.


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:
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
  string FileName;
  char buffer[256];
  FILE *InputFile;

  cout << "Enter the file name: " << flush;
  cin >> FileName;
  InputFile = fopen(FileName.c_str(), "r");
  if (!InputFile) {
    cout << "Can't open file " << FileName << " for reading" << endl;
  }
  else {
    while (fgets(buffer, sizeof(buffer), InputFile)) {
      cout << buffer;
    }
    fclose(InputFile);
  }

  return 0;

}

So you can use a string with c_str() as an argument to fopen().

Regards,

Dave
  #3  
Old 09-Jun-2005, 21:34
eddo eddo is offline
New Member
 
Join Date: Jun 2005
Posts: 2
eddo is on a distinguished road
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  
Old 09-Jun-2005, 23:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by eddo
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?

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 GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 06:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.