C++ file input/output

kalessi

In Runtime
Messages
132
Location
Normal
Should the input file automatically be created after typing it in the program? Because I could not find it. And let me remind you I'm a network student being forced by my school to take one programing class and that is why im not so good at this. I looked online watched a couple videos online and it looked like it should automatically make the file.

Code:
#include <iostream>
#include <fstream>
using namespace std;


int main()
{

    ifstream inputfile;
    ofstream outputfile;

    int score;
    string username;

    inputfile.open("inputfile.txt,ios::in");
    outputfile.open("outputfile.txt,ios::out");

    inputfile.close();
    outputfile.close();

return 0;

}
 
While using Ofstreams, opening a file will create the file to be opened, if no file with the same name already exists (Ifstreams will not create a file, and will instead throw an error). In your example, a text document named "inputfile.txt" will be created by your program in the same folder that the executable is ran from.
 
Last edited:
Could i just add it next to the cpp file or would that not work? Also does the file actually interact with the program? If i have a table of names and percentages would i be able to ask the user for the name would it be able to give the person using the program the percentage? Or is the file usually only for my reference??
 
If you've compiled the exe, and are running it from your desktop, then the file you're reading from and writing to needs to be on the desktop as well, unless you want to change the working directory.
Also, yes, the program can pull data from and push data to a file.
 
If you've compiled the exe, and are running it from your desktop, then the file you're reading from and writing to needs to be on the desktop as well, unless you want to change the working directory.
Also, yes, the program can pull data from and push data to a file.

To clarify, the input file has to exist wherever the compiled executable is (EXE in Windows, OUT file in *nix), also known as the "working directory".

Also...pretty sure when using IFStream, it will not create a file if it doesn't exist - it should throw an error saying that the file cannot be found.

Using Fstream, however, will create a file I believe. OFStream will create a file if it doesn't not already exist.

http://stackoverflow.com/questions/...ut-not-ifstream-and-fstream-cannot-seem-to-ac
 
Last edited:
Shouldnt this be creating its own file? because it wont for some reason...
Code:
//filename:main.cpp
//
//Name:
//
//Program Description:
//CMPC122 Assignment 5. Intoduction to File Input/Output in C++.

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


int main()
{

    ifstream inputfile;
    ofstream outputfile;
    inputfile.open("inputfile.txt,ios::in");
    outputfile.open("outputfile.txt,ios::out");
    string username;
    int score;
    inputfile >> username;
    inputfile.open("inputfile.txt", ios::in);
    while (!inputfile.eof()){
        inputfile >> username >> score;
        outputfile << username << score;
    }
    
    if(!inputfile){
            cerr << "Can't open output file" << endl;
            exit(1);
        
        }



    inputfile.close();
    outputfile.close();




    return 0;



}
 
Back
Top Bottom