streams and files i/o

I am trying to write a program that reads a file named test1.txt of 10 integers and outputs the values to a second file called test2.txt... below is what I have so far, and jus looking for a little guidance.

#include <fstream>
#include <cstdlib>
int read test1.txt
int print test2.txt
Int main()
{
using namespace std;
ifstream in_stream;
ofstream out_stream;

in_stream.open(test1.txt)
out.stream.open(test2.txt)

Lines 3-4: These lines make no sense. You don't need them if you quote the name of the file in lines 11-12.

Line 5: Do not capitalize int

Line 7: Belongs after line 2, not inside main().

Lines 11-12: File names need to be quoted.

You need a loop to read and write the records.

You're missing a } to terminate main()

okay thank you for your advise so far.. I made some changes and would like to know what you think.

#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{

ifstream in_stream;
ofstream out_stream;

in_stream.open(test1.txt)
out.stream.open(test2.txt)

while (test1.txt >10)
{
cout<< sum to text2.txt<<endl;
}

In_stream.close()
Out_stream.close()
return 0;
Have you tried to compile this code? The messages issued by the compiler are there to guide you and help identify possible problems with the code. There are several inconsistent uses of names, which the compiler can help you to spot.
closed account (48T7M4Gy)
Here's a small start as Abstract above. Use code tags please. Read/use the tutorial examples might be useful. http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

ifstream in_stream;
ofstream out_stream;

in_stream.open(test1.txt)  //<--- no end of statement, quotation marks
out.stream.open(test2.txt) // ditto, ditto

while (test1.txt >10) //<-- 10 is an error
{
    cout<< sum to text2.txt<<endl;// <-- #include ???, sum to text2?????
}

In_stream.close() //<-- naming error
Out_stream.close()// <-- naming error
return 0;
} // <-- 
Last edited on
Line 10-11: You're still not quoting the filename.

Line 11: You want out_stream, not out.stream.

Line 13: Your while statement is wrong. You want to read from in_stream. You're going to need a variable to read into.
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

Line 15: Your cout statement is wrong. Also, you're not writing to the output file.

Line 18-19: Your capitalization is wrong.

You still missing a } to terminate main.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I think this is better but still cant get it to work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <cstdlib>

using namespace std;
int main()
{

   ifstream in_stream;
   ofstream out_stream;

   in_stream.open("test1.txt"); 
   out.stream.open("test2.txt");

   while in_stream (test1.txt >10); 
{
    cout<< "enter sum of text1.txt to text2.txt"<<endl;
}

in_stream.close(); //<-- naming error
out_stream.close();// <-- naming error
return 0;
} // <--  
Line 12: You haven;t corrected the typo I pointed out before. Should be out_stream (not .)

Line 14: Your while is still wrong.

As I said before, you're going to need a variable to read into.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <cstdlib>
#include <iostream>     //  Missing header
using namespace std;

int main()
{   int     var;    //  Temp variable to read into

    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("test1.txt"); 
    out_stream.open("test2.txt");

    while (in_stream >> var)  
    {   //  read a value
        cout << "var=" << var <<endl;   // Display variable read
        out_stream << var << endl;      // Write variable to output file 
    }
    in_stream.close(); 
    out_stream.close();
    return 0;
} 


oh okay thank you!
Topic archived. No new replies allowed.