Function Prototypes ( Reading File )

Well ive made some progress since my last post, which is good. Although i am still having a little trouble with the ptr im returning in the function, ifstream openFilePtr()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// global varaibles
string sLine = "",
sFile = "test.txt",
MyFile;


// Function Declaration
ifstream *openInputFile( );



int main()
{
    ifstream inputFile;  // different than what was returned in *openInputFile()
    
    cout << "Enter File: ";
    cin >> MyFile;
    
    // at this point we have the uers the file specification
    // now were going to open the file using the function, openInputFile
    // which returns a pointer if the file is on the disk
    
    *openInputFile();  // ***i want to use the inputFile i returned***
    
    // the file is open and we can now try to read its contents with getline
    
    while ( getline ( inputFile, sLine ) )
    {
          if ( sLine.length() == 0 ) continue;
          cout << sLine;
    } 
    
    cin.ignore();
    return 0;
    
}

// Function Definition
ifstream *openInputFile()
{        
         // what we want to do is check to see if the file matches what we want
         // then to dynamically allocated this file and open it.
                  
         if ( sFile == MyFile )
         {
              ifstream *inputFile = new ifstream( MyFile.c_str(), ios::in );
              return inputFile;
         }
              else
              {
                  cout << "err" << endl;
                  cin.ignore();
                  return NULL;
              } 
}



The output is Octal code.

And this:
1
2
3
4
5
6
while ( getline ( inputFile, sLine ) )
    {
          if ( sLine.length() == 0 ) continue;
          cout << sLine;
    } 
    


Is correct, which means that the file wasnt opened.

Why isnt the file open? I Know i didnt use a ifstream var.open
I set an ifstream *ptrVar equal to that process.And returned it because myinput matches the string i have init'd
You don't seem to grasp locality of variables.

Let's take the following code as example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int f2();
int f3();

void f(){
	/*
	'a' is local to this function. Even if other functions declared a variable
	named 'a', they wouldn't refer to *this* 'a', but to a different 'a'
	'a' is merely a pseudonym for a memory address.
	*/
	int a;
	/*
	Note that, even though f2 is returning a value, this value is not being
	assigned to anything. Return values that aren't assigned are lost
	*/
	f2();
	/*
	Now it's something completely different.
	f2() returns a value, and that value is being assigned to 'a'.
	*/
	a=f2();
	//The value of 'a' is now 10.
	/*
	It's important that you see that the value 'a' didn't change because of the
	'a' declared in f2(), so I'm going to call a different function, now.
	*/
	
	//'a' is being assigned the return value of f3(), so its value will change.
	a=f3();
	//The value of 'a' is now 1024.
}

int f2(){
	/*
	*This* 'a' is a different one from the one declared in f(). Any changes made
	to this 'a' will NOT be reflected on the other one.
	*/
	int a=10;
	return a;
}

int f3(){
	int b=1024;
	return b;
}
Topic archived. No new replies allowed.