Why won't this work?! Simple writing to files!

1
2
3
4
  ifstream outFile;
	outFile.open ("Bankaccount.txt", ifstream::in +ifstream::binary);
	outFile.open << "Hello."; 	
	outFile.close


I have got to create a simple bank account for an employee to manage a customers account, it should have hard coded the customers name, account number and password using declarations but I can't get it to read from the file, any help guys? Thanks guys!
Last edited on
You can read from ifstream the same way you read from std::cin. If you want to read the first word you can do
1
2
std::string word;
outFile >> word;


To call a function you have to put parenthesis after the function name on line 4.
outFile.close();

outFile sounds like you are writing to the file. It's probably a good idea to name it inFile to make it clearer that you are read in from the file.
Last edited on
Oh god, my bad I want to write to a file! Any idea what I've done wrong?
closed account (3TXyhbRD)
Seeing as you want to read data in binary mode, here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>
#include <iostream>

int main()
{
    char where[100];
    std::ifstream input;
    where[99] = 0;
    input.open("Bankaccount.txt", std::ios_base::binary);
    if (input.fail())
        std::cout << "Failed to open file" << std::endl;
    else
    {
        input.read(where, sizeof(where) - 1);
        input.close();
        std::cout << where << std::endl;
    }
}


Keep in mind that when using read() you need to know how much to read because the function will not stop if it reaches eof or the buffer becomes empty.
E.g.:

1
2
3
4
MyClass myInstance;
input.read(reinterpret_cast<char*>(&myInstance), sizeof(MyClass));
if (input.fail())
    std::cout << "failed to read binary data" << std::endl;
closed account (3TXyhbRD)
butlej wrote:
Oh god, my bad I want to write to a file! Any idea what I've done wrong?


Use an ofstream instead. Write in binary mode using write() with the same technique as read(). To write formatted data use the << operator.
To write to a file you need a ofstream or fstream
Need it to be a bit more simple, plus it doesn't have to be binary just added that in there myself because I'm clueless!!
closed account (3TXyhbRD)
Ah in that case use the << operator. When reading a string use getline(). The code bellow shows how to write data and read data from the same 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
#include <fstream>
#include <iostream>
#include <string>

void testWrite(const char fileName[])
{
    std::ofstream out;
    out.open(fileName);
    if (out.fail())
        std::cout << "Failed to open file" << std::endl;
    else
    {
        out << int(1);
        out << std::string("Hello").c_str() << std::endl;
        out << int(2);
        out.close();
    }
}

void testRead(const char fileName[])
{
    int numberOne, numberTwo;
    std::string text;
    std::ifstream in;
    in.open(fileName);
    if (in.fail())
        std::cout << "Failed to open file" << std::endl;
    else
    {
        in >> numberOne;
        std::getline(in, text);
        in >> numberTwo;
        in.close();
        std::cout << numberOne << std::endl << text << std::endl << numberTwo << std::endl;
    }
}

int main()
{
    testWrite("Bankaccount.txt");
    testRead("Bankaccount.txt");
}
Last edited on
Woah thanks a lot! Just a few questions to help me understand this:

What does this mean?
void testWrite(const char fileName[])

And what do int(1)/(2) and numberOne and Two stand for?
closed account (3TXyhbRD)
void testWrite(const char fileName[])
Means that it gets an array of characters as parameter. Having the const in front of the type name means that the content of the array cannot be changed.

int n = 1;
has the same effect as
int n = int(1);

Using int(1) will return an integer equal in value with the one specified as parameter. I only wrote it to make it clear that I'm outputting an integer and not something else.

numberOne and numberTwo are local int variables that I use to store what I read from file (int(1) and int(2) respectively).
for some reason when I enter the file name it is saying its unidentified??
closed account (3TXyhbRD)
How are you performing the call?
Topic archived. No new replies allowed.