Using a function to open a file and then having other functions utilize that file afterwards?

fopen
what object do you use to represent a file in c?
c program to read and write to a file
fscanf
c program to write data into a file
c program to create a text file and display the contents
file handling in c++
file handling programs

I have a computer science assignment which requires me to have a separate function just to open the file, and then another function which will then process the data in that file and then some others to do some operations with that data. Anyways, I'm having trouble in how to be able to let other functions use that opened file. References with '&' or'*' are confusing me and I'm unsure if I have to use one or not, of course, though I'm pretty sure I'll have to pass at least something to the next function. The main intent when dealing with the file is to open it(openFile) and then have another function(getData) to sort the data into two different arrays. One for the names, and one for the amounts next to them. The file would be written as:

Johnson 6000
Brown 5000
Miller 4000
Duffy 2500
Robson 1800

My code is as follows:

'''

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

void openFile();
void getData();
void computePercentages();
void sortVotes();
void display();
void displayWinner();

int main() {
    openFile();
    getData();

    return 0;
}

void openFile(){
    string fileName;
    cout << "Enter the name of the file to open: ";
    cin >> fileName;
    ifstream file;
    file.open(fileName.c_str());
}

void getData(){
    int count = 0;
    while(!file.eof()){
        string names[count];
        int votes[count];
        cin >> names[count];
        cin >> votes[count];
        count ++;
    }
}

'''

One way is to have openFile return the file stream object, then pass it to getData.

ifstream openFile()
{
    string fileName;
    cout << "Enter the name of the file to open: ";
    cin >> fileName;

    ifstream file(fileName);

    return file;
}

void getData(ifstream &file)
{
    int count = 0;
    while(file){
        string names[count];
        int votes[count];
        cin >> names[count];
        cin >> votes[count];
        count ++;
    }
}

int main()
{
    ifstream file = openFile();
    if (file)
    {
        getData(file);
    }
}

Note that this answer does not fix other issues in your code. For example, in getData you're using variable-length arrays which are non-standard and won't work on all compilers, and those arrays are constructed and destroyed each time through the while loop.

[PDF] Using C++ File Streams, functions and operators that you can apply to an istream or ostream object can also course, they can be named whatever you wish, like any other C++ variable. Instead of creating and then opening the file streams in separate statements, you An easy way to prepare an input file is to use a text editor that creates plain� The main intent when dealing with the file is to open it(openFile) and then have another function(getData) to sort the data into two different arrays. One for the names, and one for the amounts next to them. The file would be written as: Johnson 6000 Brown 5000 Miller 4000 Duffy 2500 Robson 1800

There are many ways to do it..

Here is a simple way.. using global variables.

I made ifstream file; as global..

This is not good way.. but simple..

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

void openFile();
void getData();
void computePercentages();
void sortVotes();
void display();
void displayWinner();

ifstream file;

int main() {
    openFile();
    getData();

    return 0;
}

void openFile(){
    string fileName;
    cout << "Enter the name of the file to open: ";
    cin >> fileName;
    file.open(fileName.c_str());
}

void getData(){
    int count = 0;
    while(!file.eof()){
        string names[count];
        int votes[count];
        cin >> names[count];
        cin >> votes[count];
        count ++;
    }
}

File I/O : Learn to open, close, write and read form files in C, We open a file or create a new file using fopen() function. The functions which we use for reading and writing depends on whether we are dealing After that, fscanf function will read the contents of the file and printf function will display the contents of the file on the screen. Now see this program to copy one file to another. After entering some values, we have to press the + button. Afterwards, we can create more functions (files), or press “Add” to close the dialog. Now the new function is there, in the list. But what has happened? Let’s switch back to the Code View We can see: a “lib” folder has been created the new javascript file inside

Intro to File Input/Output in C, In other words, the only way you can use a FILE * is via the functions that C gives you. In order to open a file, use the function fopen() . Use it as: Note that fopen() returns a FILE * that can then be used to access the file. When the In other words, they won't report end-of-file on the last valid read, only on the one after it. Excel VBA can open existing worksheets and run macros on them. File is an important part of any computer system. It could vary from being a simple text file to an executable file. Most programming languages have in-built functions to process and manipulate files. Today, in this intermediate level tutorial we introduce you to VBA file open function.

C Files I/O: Create, Open, Read, Write and Close a File, Following are the most important file management functions available in 'C,' fopen is a standard function which is used to open a file. After closing the file, the same file pointer can also be used with other files don't need to use for loop \n", fp); fputs("Easier than fputc function\n", fp); fclose(fp); return (0);� This type of function must be defined within a file, not at the command line. Often, you store a function in its own file. In that case, the best practice is to use the same name for the function and the file (in this example, fact.m), since MATLAB ® associates the program with the file name.

File I/O in C programming with examples, If file does not open successfully then the pointer will be assigned a NULL value, The file is opened using fopen() function, while opening you can use any of the string (array of char) in which you are storing the string after reading it from file. file is having issues while opening"); } /*Read & Write Logic for binary files*/� The ___ file stream data type is for output files, input files, or files that perform both input and output. fstream If a file fails to open, the file stream object will be set to ___.

Create Functions in Files - MATLAB & Simulink, This example shows how to create a function in a program file. In that case, the best practice is to use the same name for the function and the file (in this example , another option for storing functions is to include them at the end of a script file. If your function returns one output, you can specify the output name after the� The compiler is not able to find the location of add.h file. Make sure you have added the correct path of add.h added in "include Directories" in case of diff.c compilation. You should have one main function in one of the .c file and defination of called function another .c file. main() is the entry for start of execution for an exe.

Comments
  • Hint: you can make functions return an ifstream
  • By the way, that loop is wrong - get out of that habit now! Yes, I realise there's a 90% chance your teacher taught it to you. No, the irony of that is not lost upon me
  • using namespace std; and file is not closed. And many functions prototyped but never implemented. A question that is not a minimal reproducible example doesn't an answer with has the similar problem