Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-08-2005, 09:32 PM   #1 (permalink)
Coolguy123
Registered User
 
Join Date: Aug 2005
Posts: 3
Coolguy123 is on a distinguished road
question about opening a file

Ok.

So I'm doing a tutorial, and it says you can write:
Code:
// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main () {
  char buffer[256];
  ifstream examplefile ("example.txt");
...
this works.

but you can also declare a pointer to a constant char array and put that pointer in the brackets.

i.e.
Code:
// reading binary file
#include <iostream.h>
#include <fstream.h>

const char * filename = "example.txt";

int main () {
  char * buffer;
  long size;
  ifstream file (filename, ios::in|ios::binary|ios::ate);
...
so in the first case, you can place the string directly, in the other case you can place the pointer to the string without specifying that you want the content of the string.

How does this work?

Does the compiler automatically distinguish that it's a pointer and in that case of the open function of ifstream/ofstream/fstream, treat it differently?

Last edited by redhead; 08-09-2005 at 06:45 AM.
Coolguy123 is offline   Reply With Quote
Old 08-09-2005, 06:55 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
It's part of the deffinition when beeing called with either the string itself or a const char pointer, you won't conflict with the standard declaration of the ifstream constructor.
Since basicaly it's the same so the rolled back code will in your first case be:
  • temporary const char pointer (temp) assigned value "example.txt"
  • ifstream default constructor called with (temp) relying on default open flags
  • file pointer assigned to (file)

Where in your second case it would be:
  • static constant char pointer (filename) assigned value "example.txt"
  • ifstream constructor taking (const char*, ios::flags) called with (filename, ios::in|ios::binary|ios::ate)
  • file pointer assigned to (file)
As you see the implied code is virtualy the same, you just did a bit more coding in order to achieve the same result.

On a side note, if you're planing on reading into your
Code:
char * buffer;
once you've opened the file, you will experience errors, the buffer has to have the needed memory assigned, either by usign malloc()/new() or by specifying the size needed as in your first example.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-10-2005, 12:32 AM   #3 (permalink)
Coolguy123
Registered User
 
Join Date: Aug 2005
Posts: 3
Coolguy123 is on a distinguished road
thanx
Coolguy123 is offline   Reply With Quote
Old 08-10-2005, 12:33 AM   #4 (permalink)
Coolguy123
Registered User
 
Join Date: Aug 2005
Posts: 3
Coolguy123 is on a distinguished road
Q, what is this referring to?

Quote:
file pointer assigned to (file)
Coolguy123 is offline   Reply With Quote
Old 08-10-2005, 02:48 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
When the ifstream() constructor is called it returns a FILE* of intrinsic type, it is through this every read/write/search/close handling on the file is carried out. I was just pointing out, as the
Code:
ifstream file (filename);
exits the FILE* created by the ifstream constructor is assigned to the variable file, so you can read/write/etc. by accessing file.
Another thing that's good to know, is that youy most likely want to check if the opening was ok ie:
Code:
ifstream file(filename, flags);
if(file.bad() || file.fail()){
    std::cout << "Error opening file: " << filename << std::endl;
    return -1;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
'old-skool' server code enhancement zyrxfz Standard C, C++ 11 06-14-2005 05:17 AM
Output problem with file manipulation (newbie) crazyant Java 4 03-11-2005 01:03 PM


All times are GMT -8. The time now is 05:33 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting