[SOLVED] [C++] File I/O - Where is this file saved?

x BlueRobot

Administrator
Staff member
Joined
May 7, 2013
Posts
10,400
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {

    ofstream out("test");
    if (!out) {
        cout << "Cannot open file\n";
        return 1;
    }

    out << 10 << " " << 123.3 << "\n";
    out << "This is a short text file";

    out.close();

    cin.get();

    return 0;
}

I wrote this short exercise, but I was wondering where the file is saved? I'm sure when I wrote and ran the program earlier it was saved in My Documents.

Any suggestions or corrections would appreciated :huh:
 
I found it!

Thanks for your help :thumbsup2:

I have one quick question, how can I allow a user to select a file of their choice to open? I've managed to do it with a GUI, but with DOS it's different.
 
There's various ways you can get user input in C++, I would suggest getline() though. Then just use that as the input string location for the filestream.
 
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {

    char string[80];
    int i;
    char ch;
    float f;
    

    cout << "Please enter a file to open: ";
    cin.getline (string, 80, '\n');

    ifstream infile(string);
    infile.open(string, ios::in);
    
    if (!infile) {
        cout << "Cannot open file\n";
        cin.get();
        return 1;

    }

    
    infile >> f;
    infile >> ch;
    infile >> i;

    cout << ch << " " << i << " " << f << '\n';

    infile.close();

    cin.get();

    return 0;
}

It still doesn't work, I understand all the other concepts of C++, such as Classes, Inheritance and Dynamic Memory Allocation, it's just these I/O streams.
 
char string[80];

I would not name it 'string' though.

1. std::string from the string header is close to the same, if you don't specify that you're using the standard namespace.
2. It doesn't define at all what this variable is for... You should be naming your variables as hints to what they do or what they represent. 'fPath'? 'userInput'? etc...

Also, 'Dynamic Memory Allocation' is more of a C concept. You shouldn't really have to deal with this because of the types already available in the standard library. vectors, lists, etc... It might be good to know, but it's not really required in C++.
 
Last edited:
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {

    char user_input[80];
    int i;
    char ch;
    float f;
    

    cout << "Please enter a file to open: ";
    cin.getline (user_input, 80, '\n');

    ifstream readfile(user_input);
    readfile.open(user_input, ios::in);
    
    if (!readfile) {
        cout << "Cannot open file\n";
        cin.get();
        return 1;

    }

    
    readfile >> f;
    readfile >> ch;
    readfile >> i;

    cout << ch << " " << i << " " << f << '\n';

    readfile.close();

    cin.get();

    return 0;
}

I've changed the variable names, however, the program doesn't actually open any files, even though they exist. I've even moved the file to the same directory to the .exe of the program.
 
You're already using the constructor here:
Code:
std::ifstream readfile(user_input);

You don't need this line:
Code:
readfile.open(user_input, ios::in);

:beerchug2:
 
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {

    char user_input[80];
    int i;
    char ch;
    float f;
    

    cout << "Please enter a file to open: ";
    cin.getline (user_input, 80, '\n');

    ifstream readfile(user_input);
    
    if (!readfile) {
        cout << "Cannot open file\n";
        cin.get();
        return 1;

    }

    
    readfile >> f;
    readfile >> ch;
    readfile >> i;

    cout << ch << " " << i << " " << f << '\n';

    readfile.close();

    cin.get();

    return 0;
}

It works now, thanks! :beerchug2:
 
Quick question, could I change streams for writing, which would then allow the user to enter text to be saved in a document?
 
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {

    char user_input[301];
    
    cout << "Please enter a text to be saved to the file: ";
    cin.getline (user_input, 301, '\n');

    ofstream writefile(user_input);
    
    if (!writefile) {
        cout << "Cannot open file\n";
        cin.get();
        return 1;

    }
    
    writefile.close();

    cin.get();

    return 0;
}

I know, my problem is now solved, but I've managed to write the code for allowing the user to write text to a file, so I'll thought I'll post it to potentially help others.
 
I wasn't going to mention this until later, but there are discussions about not using:
Code:
using namespace std;

In your code. If you want to know why, read here: iostream - C++ Standard Library: How to write wrappers for cout, cerr, cin and endl? - Stack Overflow

Very interesting Ace, I have never heard of people suggesting that you don't use it...

I have ran into problems when overloading though that took some tweaking to work but, nothing too ridiculous.

Do you use using?
 
I wasn't going to mention this until later, but there are discussions about not using:
Code:
using namespace std;

In your code. If you want to know why, read here: iostream - C++ Standard Library: How to write wrappers for cout, cerr, cin and endl? - Stack Overflow

Very interesting Ace, I have never heard of people suggesting that you don't use it...

I have ran into problems when overloading though that took some tweaking to work but, nothing too ridiculous.

Do you use using?

No, never. See some of my C/C++ examples around the forum, I always use std:: (instead of defining that I am using the standard namespace at the top) because it also helps me read the code and understand which "piece" comes from which namespace... It also helps to avoid ambiguity, plus, you'll probably never be using everything in that namespace therefore, for purposes of the intellisense that VS provides, it's just extra bulk.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top