I/O with files...

I'm a beginner with I/O. My goal is to open a file, perform a math operation and spill the answer into an output file. I did post a thread yesterday but it quickly died. I got a huge amount of help but now I have stumbled upon a new issue.

It opens the input and output file, but it passes fail() for the input and writes: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ

in the output file. explanation??
explanation??
You did something wrong.
Maybe if you posted code, we could find your mistake.
Yup, forgot. (lines omitted)

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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <math.h>
#include <fstream>
#include <conio.h>

using namespace std;

int main()
{
char tap;
int in;
int preout;
char out;
fstream infile;
fstream outfile;
outfile.open("out");
string filename;






cout<< "Filename?\n";
cin>> filename;




infile.open(filename.c_str());
c:
infile.get(tap);
in = tap;
preout = in;
out = preout;
outfile << out;
if (infile.bad()) (cout<< "Bad. (IN)\n");
if (infile.fail()) (cout<< "Fail. (IN)\n");
if (infile.eof()) goto d;
if (outfile.bad()) (cout<< "Bad (OUT)\n");
if (outfile.fail()) (cout<< "Fail. (OUT)\n");
if (!infile.is_open()) (cout<< "infile not open.\n");
if (!outfile.is_open()) (cout<< "outfile not open.\n");
goto c;
d:
cout<< "Done.";
}
On first failure to read int, it will show you fail and as you don't set program to terminate, writes random value contained in said int converted to char to the output.
All subsequent operaions fails too (because input which leads to fail is still here). So you gon an infinite loop here.
I could never figure out how I/O works when dealing with files. I want my program to pull one character at a time, convert the char to int, (Insert algorithm here), pop out another character, write new char to output file, repeat until the end of the input file is reached and terminate. How could I do this?
Topic archived. No new replies allowed.