944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12154
  • C++ RSS
Dec 2nd, 2004
0

reading from file into array

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <istream>
  6.  
  7. using namespace std;
  8.  
  9. float readStoreCount(float numbers[]);
  10. float printNumbers(float numbers[]);
  11.  
  12. int main ()
  13. {
  14. float numbers[100];
  15.  
  16. numbers [100] = readStoreCount(numbers);
  17.  
  18. printNumbers(numbers);
  19.  
  20. system ("pause");
  21. return 0;
  22. } //main
  23.  
  24. float readStoreCount(float numbers[])
  25. {
  26. int i = 0;
  27. ifstream source;
  28. source.open ("source.txt");
  29. if (!source)
  30. { cerr << "\aError 100 opening source.txt" << endl;
  31. exit (100);//opening failure test
  32. }
  33. for (i = 0; i < 100; i++)
  34. cin >> numbers [i];
  35. cout << "numbers are going in" << endl;
  36. source.close ();
  37. if (source.fail())
  38. { cerr << "\aERROR 102 closing source.txt" << endl;
  39. exit (102);//closing failure test
  40. }
  41. }
  42.  
  43. float printNumbers(float numbers[])
  44. {
  45. int i;
  46. float numbersIn[100];
  47.  
  48. for (i = 100; i > numbers [i]; i--)
  49. cout << numbers [i];
  50. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
WrEcK is offline Offline
8 posts
since Nov 2004
Dec 2nd, 2004
0

Re: reading from file into array

scratch that i figured it.....but does anyone know how to unset the error flag before checking for a close failure?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
WrEcK is offline Offline
8 posts
since Nov 2004
Dec 3rd, 2004
-1

Re: reading from file into array

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;


}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Raiders is offline Offline
3 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ debugging error
Next Thread in C++ Forum Timeline: Need help with this conversion program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC