File input

Is it possible to read input from a file function that is outside the function that opens the file.

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
63
64
65
66
  int OpenFile(string inFileName, AList* fr)
{
       //local variables
    ifstream openFile;
    int fileStatus;
    

    //open the file
    openFile.open(inFileName.c_str());

    //if file doesn't exist exit the program
    if(!openFile)
    {
        fileStatus = 1;
    }
    else  //if the file exists call LoadFile() and store data into linked lists
    {
       
        fileStatus = 0;
    }//end

   
return fileStatus;
}

  void LoadFileVehicle(ifstream& vehicleFile, AList* head)
{
    autoStack *nNode;       //creates a new node
    autoStack *current;          //last node
    char vehicleType;               //char from file V or A
    string license;                 //string from file license plate
    int capacity;                   //int from file capacity of vehicle

    //nNode = CreateNode();

if(nNode){
    //prime the the variables with the first record;
    vehicleFile >> vehicleType;
    vehicleFile >> license;
    vehicleFile >> capacity;
}
    do
    {

        newNode = new autoStack;

        newNode->vehicleType = vehicleType;
        newNode->license = license;
        newNode->capacity = capacity;

        newNode->nextVehicle = NULL;

        if(firstRecord == NULL)
            firstRecord = newNode;
        if(last != NULL)
            last->nextVehicle = newNode;
            last = newNode;

        vehicleFile >> vehicleType;
        vehicleFile >> license;
        vehicleFile >> capacity;

  
    }while (vehicleFile); //end do while loop when file is done loading
}
}


Or does LoadFileVehicle() need to be inside of OpenFile()
Last edited on
Yes, it's possible but not with the way you currently have your code.

Line 4: openFIle is a local variable. It goes out of scope (closed automatically) when OpenFile returns to the caller (line 23).

Pass the ifstream for openFile to OpenFile by reference.
Thank you I have been stuck on this part of my homework for two days now.
Topic archived. No new replies allowed.