-
November 16th, 2011, 12:45 PM
#1
Storing output in a text file
Let's suppose I want to append 5 numbers in a file called numbers.txt
It's running the program; however, it never creates the file numbers.txt; therefore it always displays "file could not be created". Am i missing something?
Code:
// ch 9.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int numbers[5] = {0};
ofstream numberSequence;
for (int x = 0; x < 5; x++)
{
cout << "enter digit " << endl;
cin >> numbers[x];
}
cout << endl << "These number will be stores in a file:" <<endl;
numberSequence.open("numbers", ios ::app);
if (!numberSequence.is_open())
{
for (int p = 0; p < 5; p ++)
{
cout << endl << numbers[p] << endl;
numberSequence << numbers[p] << '#' << endl;
}
}
else
cout << "file could not be created" << endl;
system("pause");
return 0;
}
-
November 16th, 2011, 01:26 PM
#2
Re: Storing output in a text file
Your if condition is wrong. Read carefully what it's saying.
-
November 17th, 2011, 02:35 AM
#3
Re: Storing output in a text file
Originally Posted by Lindley
Your if condition is wrong. Read carefully what it's saying.
if (numberSequence.is_open() == true)
=-)
thank you!
-
November 17th, 2011, 04:54 AM
#4
Re: Storing output in a text file
Originally Posted by Pokarface
if (numberSequence.is_open() == true)
=-)
thank you!
Never compare the boolean values with true. Nor with false.
Code:
if( numberSequence.is_open() )
{
// file was opened!
}
...
if( !numberSequence.is_open() )
{
// file open faled!
}
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|