writing from one file to another

Dec 12, 2012 at 5:14pm
Hello, I've seen plenty of posts on how to write to a file, or how to read from a file, but not any on how to read from one file and then write to another. Here I'm trying to read from one file, make all the letters caps, and then write to another. Any help would be appreciated. Both of the files have been created. The change from lower to uppercase does work. Later on I'll have the program do additional changes. 4in.txt is the declaration of independence. p4out1.txt is blank. They are in their proper directories as required for visual studio 2012. The program seems to do everything I want it to do so far except for writing to p4out1.txt. I run the program and the only output in p4out1.txt is 'ÿ'.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"
#include "iostream"
#include "ctype.h"
#include "fstream"

using namespace std;

 
bool Digit(char x)
{  
    if (isdigit(x))
        return 1;
    else
        return 0;
}
 
bool Upper(char x)
{
    if (isupper(x))
        return 1;
    else
        return 0;
}
 
bool Lower(char x)
{
    if (islower(x))
        return 1;
    else
        return 0;
}
 
char Change(char x)
{
    if (islower(x))
    return (toupper(x));
	else
    return x;
}

void doFiles()
{
	ifstream infile;
	infile.open("p4in.txt");
	while(!infile.eof())
	{
		char c = infile.get();
		ofstream outfile;
		outfile.open("p4out1.txt");
		outfile << Change(c);
	}

}


int _tmain(int argc, _TCHAR* argv[])
{
	
	doFiles();
	
		

	return 0;
}
Dec 12, 2012 at 5:27pm
Line 48 is inside of a while loop. That means that it will be created opened and then, destructed every character. Every time you open it, you will also be overwriting any previously saved versions of the file (including operations done last iteration).

To solve your problem, insert line 48-49 to line 44.
Dec 13, 2012 at 3:54pm
Oh thanks, that works great -


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void doFiles()
{
	ifstream infile;
	infile.open("p4in.txt");
	ofstream outfile;
	outfile.open("p4out1.txt");
	while(!infile.eof())
	{
		char c = infile.get();
		
		outfile << Change(c);
	}

}
Dec 13, 2012 at 4:34pm
that works great -

No it doesn't. while(!infile.eof()) is an unfortunately popular error, and it shows in this case: the file sizes are different.

One of the many ways to write this loop correctly is
1
2
3
4
5
char c;
while(infile.get(c))
{
    outfile << Change(c);
}

Last edited on Dec 13, 2012 at 7:07pm
Dec 13, 2012 at 5:44pm
What's the problem? Specifically?
Dec 13, 2012 at 7:27pm
Specifically the loop creates a file that is one byte longer than the original.
Dec 14, 2012 at 12:31am
Do you mean one byte shorter than the original? I can see the original testing for EOF, not seeing it, reading an EOF, writing it to the output stream, then testing for EOF and giving false.

Meanwhile, I see Cubbi's solution attempting to get a EOF and returning false (does it return a void pointer? I don't see any documentation that it returns false). I don't see it printing EOF to the output stream since I suppose it wouldn't get(\0), so I would think it would be one byte shorter.
Last edited on Dec 14, 2012 at 12:32am
Dec 14, 2012 at 12:43am
...
I can only recommend a beginner level textbook at this point.
Topic archived. No new replies allowed.