•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,770 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,531 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7389 | Replies: 2
![]() |
•
•
Join Date: Nov 2004
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
I'm a little lost here. I'm trying to read from a file named source.txt located in the same area as my program. If i could figure this out i could continue with the rest of the program. I'm just getting a blank screen so i know i'm way off heres the code if anyone could push me in the right direction it would help greatly thanks
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <istream>
using namespace std;
float readStoreCount(float numbers[]);
float printNumbers(float numbers[]);
int main ()
{
float numbers[100];
numbers [100] = readStoreCount(numbers);
printNumbers(numbers);
system ("pause");
return 0;
} //main
float readStoreCount(float numbers[])
{
int i = 0;
ifstream source;
source.open ("source.txt");
if (!source)
{ cerr << "\aError 100 opening source.txt" << endl;
exit (100);//opening failure test
}
for (i = 0; i < 100; i++)
cin >> numbers [i];
cout << "numbers are going in" << endl;
source.close ();
if (source.fail())
{ cerr << "\aERROR 102 closing source.txt" << endl;
exit (102);//closing failure test
}
}
float printNumbers(float numbers[])
{
int i;
float numbersIn[100];
for (i = 100; i > numbers [i]; i--)
cout << numbers [i];
} •
•
Join Date: Nov 2004
Location: Malaysia
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hi there, hopefully my this simple program will help you out........Actually it is almost as same as your's but i did lots of changes. The word in magenta , i think is ofstream instead of ifstream. :mrgreen:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
void readStoreCount();
void printNumbers();
int main ()
{
readStoreCount();
printNumbers();
return 0;
} //main
void readStoreCount()
{
int i = 0;
float numbers[100];
ofstream source;
source.open ("source.txt");
if (!source)
{
cerr << "\aError 100 opening source.txt" << endl;
exit (1); // Exit Program If The File Cannot Be Opened //
}
for (i = 0; i < 100; i++)
{
numbers[i]=i;
source<<numbers[i]<<endl; // Intializing The numbers Into Your Source File //
}
cout << "Numbers are going in the file............. " << endl;
cin.ignore();
system("cls");
source.close ();
if (source.fail())
{
cerr << "\aERROR 102 closing source.txt" << endl;
exit (1); //closing failure test
}
}
void printNumbers()
{
int i=0;
ifstream source("source.txt",ios::in);
source.clear();
source.seekg(0);
float Num;
float numbersIn[100];
while(i<100 && !source.eof() ) // Input The Numbers from The File Into The Array NumbersIn[]
{ // While i<100 And While File Is Not End Of File.
source>>Num;
numbersIn[i]=Num;
i++;
}
source.close();
cout<<"The Numbers Taken From The File Are :- "<<endl;
cout<<"======================================="<<endl;
for(int j=100; j>=0; j--) // Display The Numbers in The Array From Largest
{ //numbersIn[100] To numbersIn[0] //
cout<<numbersIn[j]<<endl;
}
cout<<endl;
}
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
void readStoreCount();
void printNumbers();
int main ()
{
readStoreCount();
printNumbers();
return 0;
} //main
void readStoreCount()
{
int i = 0;
float numbers[100];
ofstream source;
source.open ("source.txt");
if (!source)
{
cerr << "\aError 100 opening source.txt" << endl;
exit (1); // Exit Program If The File Cannot Be Opened //
}
for (i = 0; i < 100; i++)
{
numbers[i]=i;
source<<numbers[i]<<endl; // Intializing The numbers Into Your Source File //
}
cout << "Numbers are going in the file............. " << endl;
cin.ignore();
system("cls");
source.close ();
if (source.fail())
{
cerr << "\aERROR 102 closing source.txt" << endl;
exit (1); //closing failure test
}
}
void printNumbers()
{
int i=0;
ifstream source("source.txt",ios::in);
source.clear();
source.seekg(0);
float Num;
float numbersIn[100];
while(i<100 && !source.eof() ) // Input The Numbers from The File Into The Array NumbersIn[]
{ // While i<100 And While File Is Not End Of File.
source>>Num;
numbersIn[i]=Num;
i++;
}
source.close();
cout<<"The Numbers Taken From The File Are :- "<<endl;
cout<<"======================================="<<endl;
for(int j=100; j>=0; j--) // Display The Numbers in The Array From Largest
{ //numbersIn[100] To numbersIn[0] //
cout<<numbersIn[j]<<endl;
}
cout<<endl;
}
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Reading .dat file data into an array (C#)
- Setting an array and reading in a txt file backwards (C++)
- Reading file input into an array (C++)
- RE: reading .wav file and putting ito inot the array (C)
- Reading an input file as a class memeber function (C++)
- reading txt file into array (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
Other Threads in the C++ Forum
- Previous Thread: c++ debugging error
- Next Thread: permutation of a string in c++


Linear Mode