Reading from Text file.

Hello all. I am trying to write a program that calculates area and circumference of a circle from the radius. There is a text file open within the program and the 2 radii I need to read are on the first line like so "radius1 radius2". I just can't figure out how to read those numbers and store them as radius1 and radius2 in the program. Any help would be greatly appreciated.
You don't know how to use the file I/O library, or you don't know how to parse the data? Be more specific.

http://cplusplus.com/doc/tutorial/files/
Err I am not sure...? I am a complete beginner so I apologize because I don't understand exactly what you are asking.

This is what I have so far:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ifstream infile;
	infile.open ("Example1.txt");


Well there is more after that but I am unsure what to do next to read those numbers and set them as variables.
Count the number--the characters, then use seekg(); or use getline.
Still not getting it?


Think this should show a way:

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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
    fstream myfile;
    vector<int> content_of_file;
    int num=0;

    cout<<"Type a number"<<endl;
    cin>>num;
    myfile.open("test.txt",ios::app|ios::out);
    myfile<<' '<<num;
    myfile.close();
    myfile.open("test.txt",ios::in);
    cout<<"\n\nReading myfile: "<<endl;
    while(myfile.is_open()&& !myfile.eof())
    {
        string temp;
        int temp2;

        getline(myfile,temp,' ');
        stringstream(temp)>>temp2;
        content_of_file.push_back(temp2);
    }
    for(vector<int>::size_type i=1;i<content_of_file.size();i++)
        cout<<content_of_file[i]<<' ';
        cin.ignore();
    cin.get();

    return 0;
}


Do you want me to explain?
Last edited on
What u r doing is correct. Hereafter everything u do as in the normal program.
Instead of using 'cin' use 'infile'.



if u want to write to a file, create a variable under ofstream,like 'ofstream outfile' and open the file.

take out cout and replace it with outfile
Thanks for the quick responses, much appreciated. Though I don't believe I am allowed to use vectors (of course, I don't even know how to use them yet).

vichu8888, you mean like so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>


using namespace std;

int main()
{
	ifstream infile;
	infile.open ("Example1.txt");
	
	double radius1, radius2;

	infile >> radius1, radius2 >>


I get 2 errors, "illegal, left operand has type 'double'" and "illegal, right operand has type 'double'"
infile >> radius1, radius2 >>

what is the last >> there for?

what about inputting it into an array of int
1
2
3
4
5
6
7
8
9
10
11
12

ifstream infile;

infile.open(" ....");

double r1,r2;

infile>>r1>>r2; 
or
infile>>r1;
infile>>r2;
Hah I am not sure why I put that there...but I got rid of it and it works! Thank you very much to all of you...greatly appreciated!
Remember to 'Resolve as solved' :)
Topic archived. No new replies allowed.