program won't write out to text file

I'm trying to make a program that reads emails from a text file ("fileContainingEmails.txt"), prints them on the console, and then writes them out to a different text file called "copyPasteMyEmail.txt". For some reason it's creating an output file, but no data is being written into it. I tried adding fout.close ("copyPastMyEmail.txt") at the end, but now I have a c2660 compilation error ('function' : function does not take number parameters). Help please?

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string>
using namespace std;


bool validation (char c) 
{
    // Capital
    if (c >='A' && c <= 'Z')
        return true;
    // Lowercase 
    else if (c >= 'a' && c <= 'z')
        return true;
    // Numbers
    else if (c >= '0' && c <='9')
        return true;
    // Punctuation
    else if ((c =='.') || (c =='-') || (c =='+'))
        return true;
    return false;
}

int main () {
    //bool ValidEmailCharacters;
	string email;
	string anEmail;
	int s = 0;
	int e = 0;
	
	string fileName; 
	string defaultfile = "fileContainingEmails.txt";
	ifstream fin;
	ofstream fout;
	cout << "Enter your file  (ENTER for Default): " ;
	getline (cin, fileName);
	
	if (fileName.length() < 2) {
	    fileName = defaultfile;
	}

	fin.open (fileName);
    //if (!fin.good()) break;
	
	fout.open ("copyPasteMyEmail.txt");
	
	//string* email[i] = new string [newemail];     //Pulls out email from txt file
	while (true)    //reads until end of file
	{
        
        while(fin>>email)
	cout << email << endl;
	for (int i = 0; i > email.length(); i++)
		if (email[i] == '@')
		{
		cout << ";" << endl;
			for( s = i; s < email.length(); s--)
			{    
			cout << i << endl;
				if (s < 0) break;
				if (validation(email[s]) == false) break;
			}
			bool Dot = false;
				for (e = i; e < email.length(); e++)
				{
				cout << i << endl;
					if (e == email.length()) break;
					if (validation(email[e]) == false) break;
					if (email[e] == '.') Dot = true;
				}
				cout << i << endl;
			string anEmail = email.substr (s, e-s);
			cout << anEmail << endl;	
		}
    }
	fout << anEmail << " ;" << endl;
        fout.close ("copyPasteMyEmail.txt");
   
	return 0 ;
	
}
Hi,

You have to change line 43 to fin.open (filename.c_str()); , because open() doesn't take a string as parameter. (http://www.cplusplus.com/reference/string/string/c_str/) And line 78 has to be fout.close(); , because close() doesn't have any parameters.

Happy coding! :D

EDIT: @lowestone: "The fact that a file is being created means that you are opening the file correctly." In Dev-C++ that line gives an error.
Last edited on
The fact that a file is being created means that you are opening the file correctly.

I believe you want an open bracket before line 53 and a closing bracket after line 77/ Otherwise, that entire loop only happens to the last string extracted.

@Line 73 you declare another string anEmail so this is the anEmail that gets the substring. Outside the loop, anEmail is the one you declared at line 28.
I tried that, but there still isn't any information being written in the output file.
I know see this while (true). This will never stop looking until you tell it to, which you don't.

What is your code at this point?
So I changed while (true) to while(!fin.eof()), but to no avail. It compiles, but it doesn't record the emails into the output file:/

This is it so far:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string>
using namespace std;


bool validation (char c) 
{
    // Capital
    if (c >='A' && c <= 'Z')
        return true;
    // Lowercase 
    else if (c >= 'a' && c <= 'z')
        return true;
    // Numbers
    else if (c >= '0' && c <='9')
        return true;
    // Punctuation
    else if ((c =='.') || (c =='-') || (c =='+'))
        return true;
    return false;
}

int main () 

{

	string email;
	int s = 0;
	int e = 0;
	
	string fileName; 
	string defaultfile = "fileContainingEmails.txt";
	ifstream fin;
	ofstream fout;
	cout << "Enter your file  (ENTER for Default): " ;
	getline (cin, fileName);
	
	if (fileName.length() < 2) {
	    fileName = defaultfile;
	}

	fin.open (fileName);
	fout.open ("copyPasteMyEmail.txt");
	
	//string* email[i] = new string [newemail];     //Pulls out email from txt file
	while (!fin.eof())    //reads until end of file
	{
        
        while(fin >> email)
{
	cout << email << endl;
	for (int i = 0; i > email.length(); i++)
		if (email[i] == '@')
		{
		cout << email << endl;
			for( s = i; s < email.length(); s--)
			{    
			cout << email << endl;
				if (s < 0) break;
				if (validation(email[s]) == false) break;
			}
			bool Period = false;
				for (e = i; e < email.length(); e++)
				{
				cout << email << endl;
					if (e == email.length()) break;
					if (validation(email[e]) == false) break;
					if (email[e] == '.') Period = true;
				{
				cout << email << endl;
			string email = email.substr (s, e-s);
			cout << email << endl;}

		{
			fout << email << " ;" << endl;
        		fout.close ();
		     }
    		    }
 		   }
		  }
		 }

   
	return 0 ;
	
}
Topic archived. No new replies allowed.