Need help with Fstream

closed account (GybDjE8b)
i need to show this

output window:
Column1 Column2 Column3 Column4
25.00 10.67 999.12 43.72
9.91 200.00 1.01 2.60

input file:
Column1 Column2 Column3 Column4
999.12 43.72 9.91 20
Last edited on
Needs more information.
For example, just use getline() to read a whole line at a time and dump it to the output with a cout.
If you need something more precise, them please specify.
closed account (GybDjE8b)
i think he requires us to use iomanip to change the length of each one

i just need to know how to use ifstream or ofstream.

he made 2 files called ifstream myfile and ofstream __file... i forgot and
i have no clue which one is to put in output and put in input


i just need to display each number in column but when i type it turns out to look like the stuff on top.
Ok, I'm beginning to understand what is needed now.
Can I suggest that you use the formatting options when you post - see the Format buttons on the right. It helps to preserve the spacing so that the proper layout can be seen.

In this case, the [output] put the text here [/output] would be useful.

I think it's something like this (but I may be wrong):
output window:
Column1       Column2     Column3   Column4
    25.00           10.67      999.12       43.72
     9.91          200.00         1.01           2.60


input file:
Column1     Column2   Column3   Column4       
999.12          43.72          9.91           20   


Something like this would be a start, but to line up the decimal points requires a little bit more.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    string col1, col2, col3, col4;
    double num1, num2, num3, num4;

    fin >> col1 >> col2 >> col3 >> col4;

    cout << setw(10) << col1
         << setw(10) << col2
         << setw(10) << col3
         << setw(10) << col4 << endl;

    while (fin >> num1 >> num2 >>  num3 >>  num4)
    {
        cout << setw(10) << num1
             << setw(10) << num2
             << setw(10) << num3
             << setw(10) << num4 << endl;
    }
Last edited on
closed account (GybDjE8b)
ummm wat does fin do? and can you use ifstream and ofstream
so far i did something like this

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
 float num1, num2, num3, num4, num5, num6, num7, num8, num9;
 num1 = 25.00;
 num2 = 9.91;
 num3 = 10.67;
 num4 = 200.00;
 num5 = 999.12;
 num6 = 1.01;
 num7 = 43.72;
 num8 = 2.60;
 num9 = 20.00;
 cout << fixed << showpoint; 
 ifstream infile; 
 infile.open ("Numb.txt");
 if(!infile){
  cout << "File Not Found!" << endl;
 }
 else{
  cout << setw(5) << "Column 1"
    << setw(10) << "Column 2"
    << setw(10) << "Column 3"
    << setw(10) << "Column 4" << endl;
  cout << setw(5) << "25"
    << setw(11) << "10.67"
    << setw(11) << "999.12"
    << setw(10) << "43.72" << endl;
 }
 system ("PAUSE");
 return 0;
}


but since i defined the variables it doesnt come out right. it shows alot of decimal like 25.0000000
Last edited on
ummm wat does fin do?
That was my input stream. I tend to use fin and fout for input and output, but that's not important, it's just a name, equivalent to infile in your code.

but since i defined the variables it doesnt come out right. it shows alot of decimal like 25.0000000
When you use cout << fixed the number of decimal places shown depends on the precision.
Try cout << fixed << setprecision(2);
closed account (GybDjE8b)
k i got that problem fixed.
Now for some reason it doesnt open my Numb.txt
its saying File Not found.
my professor said to use C:\\.......\\numb.txt
i tried that but it couldnt find it either
Last edited on
I assume you are replacing the dots here "C:\\.......\\numb.txt " with the actual full path to the file?
closed account (GybDjE8b)
yeah
and you are making each '\' into a double "\\" ?
closed account (GybDjE8b)
yeah
Topic archived. No new replies allowed.