Hi guys,

I'm doing a program that reads from two files and write to 16 files. The idea of the program is to rout packets across a network that contains 16 nodes. I still working in the routing mechanism but I just wrote the codes for the source node and the destination nodes.

I have to read from the first file the source node (for example in the given file dest.txt node 0) and its destination IP, and from the other file I have to get the node number of the destination IP. After that, I have to create txt files for every node and write from it receives the packets.

Here is part of the first and the second file:

1) dest.txt
Destinations of Node 0 :
========================
255.148.255.62
255.148.255.75
255.148.255.82
128.65.255.68
255.64.128.153
255.64.128.61

Destinations of Node 1 :
========================
255.148.255.75
255.148.255.82
128.65.255.112
64.128.255.120
64.128.255.154


2) arp.txt
Node 0 : 255.148.255.36 00:16:76:BF:23:B2
Node 1 : 255.148.255.62 00:A6:9C:19:5A:D4
Node 2 : 255.148.255.75 00:2A:7C:23:25:D4
Node 3 : 255.148.255.82 00:8B:A9:51:3A:F5
Node 4 : 128.65.255.68 00:92:76:E4:F1:12


So far, I wrote this codes, however, when I run it, I don't get any txt output file!
Note that any destination node can receive more than one packet from different nodes.

I hope I make it clear!

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
    ifstream file;
    file.open ("arp.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	// read all the data in the arp file
	
	char l;
	int i;
	int k;
	string IPd;
	string ip0, ip1, ip2, ip3, ip4 ,ip5, ip6, ip7;
	string ip8, ip9, ip10, ip11, ip12, ip13, ip14, ip15;
	string mac0, mac1, mac2, mac3, mac4, mac5, mac6, mac7;
	string mac8, mac9, mac10, mac11, mac12, mac13, mac14, mac15;
	ofstream f0, f1, f2, f3 ,f4 ,f5 ,f6 ,f7;
	ofstream f8 ,f9, f10, f11, f12 ,f13, f14 ,f15;
	
	file >> ip0 >> ip0 >> l >> ip0 >> mac0;
	file >> ip1 >> ip1 >> l >> ip1 >> mac1;
	file >> ip2 >> ip2 >> l >> ip2 >> mac2;
	file >> ip3 >> ip3 >> l >> ip3 >> mac3;
	file >> ip4 >> ip4 >> l >> ip4 >> mac4;
	file >> ip5 >> ip5 >> l >> ip5 >> mac5;
	file >> ip6 >> ip6 >> l >> ip6 >> mac6;
	file >> ip7 >> ip7 >> l >> ip7 >> mac7;
	file >> ip8 >> ip8 >> l >> ip8 >> mac8;
	file >> ip9 >> ip9 >> l >> ip9 >> mac9;
	file >> ip10 >> ip10 >> l >> ip10 >> mac10;
	file >> ip11 >> ip11 >> l >> ip11 >> mac11;
	file >> ip12 >> ip12 >> l >> ip12 >> mac12;
	file >> ip13 >> ip13 >> l >> ip13 >> mac13;
	file >> ip14 >> ip14 >> l >> ip14 >> mac14;
	file >> ip15 >> ip15 >> l >> ip15 >> mac15;
	
	
	file.close ();
	
	
    ifstream dest;
    dest.open ("destination.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	
	while (!dest.eof ())
	{
		
		
next_N:	dest >> IPd >> IPd >> IPd >> i;
		
		string IPs; // find the IP address of the source node
		if (i == 0){ IPs = ip0;}
		else if (i == 1){ IPs = ip1; }
		else if (i == 2){ IPs = ip2; }
		else if (i == 3){ IPs = ip3; }
		else if (i == 4){ IPs = ip4; }
		else if (i == 5){ IPs = ip5; }
		else if (i == 6){ IPs = ip6; }
		else if (i == 7){ IPs = ip7; }
		else if (i == 8){ IPs = ip8; }
		else if (i == 9){ IPs = ip9; }
		else if (i == 10){ IPs = ip10; }
		else if (i == 11){ IPs = ip11; }
		else if (i == 12){ IPs = ip12; }
		else if (i == 13){ IPs = ip13; }
		else if (i == 14){ IPs = ip14; }
		else if (i == 15){ IPs = ip15; }
		
		dest >> IPd >> IPd >> IPd;
		
		 // find the node number of the destination IP
here:	if (IPd == ip0) {
			k = 0;
			f0.open ("Node0.txt"); 
			f0 << "I received a packet from Node " << i << "\n"; // write all the packets that node0 recived and from where
		}
		else if (IPd == ip1) { 
			k = 1;
			f1.open ("Node1.txt");
			f1 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip2) { 
			k = 2;
			f2.open ("Node2.txt");
			f2 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip3) { 
			k = 3;
			f3.open ("Node3.txt");
			f3 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip4) { 
			k = 4;
			f4.open ("Node4.txt");
			f4 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip5) { 
			k = 5;
			f5.open ("Node5.txt");
			f5 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip6) { 
			k = 6;
			f6.open ("Node6.txt");
			f6 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip7) { 
			k = 7;
			f7.open ("Node7.txt");
			f7 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip8) { 
			k = 8;
			f8.open ("Node8.txt");
			f8 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip9) { 
			k = 9;
			f9.open ("Node9.txt");
			f9 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip10) { 
			k = 10;
			f10.open ("Node10.txt");
			f10 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip11) { 
			k = 11;
			f11.open ("Node11.txt");
			f11 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip12) { 
			k = 12;
			f12.open ("Node12.txt");
			f12 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip13) { 
			k = 13;
			f13.open ("Node13.txt");
			f13 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip14) { 
			k = 14;
			f14.open ("Node14.txt");
			f14 << "I received a packet from Node " << i << "\n";
		}
		else if (IPd == ip15) { 
			k = 15;
			f15.open ("Node15.txt");
			f15 << "I received a packet from Node " << i << "\n";
		}
		
		
		string ss ("Destinations");
		
		file >> IPd; // read the next destination address
		if ( ss.compare(IPd)!=0) 
		{
			goto here;
		}
		else 
		{
			goto next_N;
		}
	}
	dest.close ();
	f0.close();
	f1.close();
	f2.close();
	f3.close();
	f4.close();
	f5.close();
	f6.close();
	f7.close();
	f8.close();
	f9.close();
	f10.close();
	f11.close();
	f12.close();
	f13.close();
	f14.close();
	f15.close();
	
    return 0;
}

Thanks & If there is any way I can improve my codes please suggest it :)

You were on the right track by including sstream, but then you didn't use it!

Long lists of code like this this:

if (IPd == ip0) {
			k = 0;
			f0.open ("Node0.txt"); 
			f0 << "I received a packet from Node " << i << "\n"; // write all the packets that node0 recived and from where
		}
		else if (IPd == ip1) { 
			k = 1;
			f1.open ("Node1.txt");
			f1 << "I received a packet from Node " << i << "\n";
		}

is almost always a bad idea.

Assume IP is an int (0, 1, ... 15). You can replace about 100 lines with simply:

stringstream ssFileName;
ssFileName << "Node" << IP << ".txt";

f0.open (ssFileName.str()); 
f0 << "I received a packet from Node " << IP << "\n";

Hope that helps,

Dave

Thank you Dave,

I did as u suggest and it reduced the codes to 130 lines however, it didn't work & I got this error:

error: no matching function for call to 'std::basic_ofstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

and by the way, do u think it didn't work the first time because the listing codes?

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{

	
    ifstream file;
    file.open ("arp.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	// read all the data in the arp file
	
	char l;
	int i;
	int k;
	string IPd;
	string ip0, ip1, ip2, ip3, ip4 ,ip5, ip6, ip7;
	string ip8, ip9, ip10, ip11, ip12, ip13, ip14, ip15;
	string mac0, mac1, mac2, mac3, mac4, mac5, mac6, mac7;
	string mac8, mac9, mac10, mac11, mac12, mac13, mac14, mac15;
	ofstream f;
	stringstream ssFileName;
	
	file >> ip0 >> ip0 >> l >> ip0 >> mac0;
	file >> ip1 >> ip1 >> l >> ip1 >> mac1;
	file >> ip2 >> ip2 >> l >> ip2 >> mac2;
	file >> ip3 >> ip3 >> l >> ip3 >> mac3;
	file >> ip4 >> ip4 >> l >> ip4 >> mac4;
	file >> ip5 >> ip5 >> l >> ip5 >> mac5;
	file >> ip6 >> ip6 >> l >> ip6 >> mac6;
	file >> ip7 >> ip7 >> l >> ip7 >> mac7;
	file >> ip8 >> ip8 >> l >> ip8 >> mac8;
	file >> ip9 >> ip9 >> l >> ip9 >> mac9;
	file >> ip10 >> ip10 >> l >> ip10 >> mac10;
	file >> ip11 >> ip11 >> l >> ip11 >> mac11;
	file >> ip12 >> ip12 >> l >> ip12 >> mac12;
	file >> ip13 >> ip13 >> l >> ip13 >> mac13;
	file >> ip14 >> ip14 >> l >> ip14 >> mac14;
	file >> ip15 >> ip15 >> l >> ip15 >> mac15;
	
	
	file.close ();
	
	
    ifstream dest;
    dest.open ("destination.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	
	while (!dest.eof ())
	{
		
		
next_N:	dest >> IPd >> IPd >> IPd >> i;
		
		string IPs; // find the IP address of the source node
		if (i == 0){ IPs = ip0;}
		else if (i == 1){ IPs = ip1; }
		else if (i == 2){ IPs = ip2; }
		else if (i == 3){ IPs = ip3; }
		else if (i == 4){ IPs = ip4; }
		else if (i == 5){ IPs = ip5; }
		else if (i == 6){ IPs = ip6; }
		else if (i == 7){ IPs = ip7; }
		else if (i == 8){ IPs = ip8; }
		else if (i == 9){ IPs = ip9; }
		else if (i == 10){ IPs = ip10; }
		else if (i == 11){ IPs = ip11; }
		else if (i == 12){ IPs = ip12; }
		else if (i == 13){ IPs = ip13; }
		else if (i == 14){ IPs = ip14; }
		else if (i == 15){ IPs = ip15; }
		
		dest >> IPd >> IPd >> IPd;
		
		 // find the node number of the destination IP
here:	if (IPd == ip0) {k = 0; }
		else if (IPd == ip1) { k = 1;}
		else if (IPd == ip2) { k = 2;}
		else if (IPd == ip3) { k = 3;}
		else if (IPd == ip4) { k = 4;}
		else if (IPd == ip5) { k = 5;}
		else if (IPd == ip6) { k = 6;}
		else if (IPd == ip7) { k = 7;}
		else if (IPd == ip8) { k = 8;}
		else if (IPd == ip9) { k = 9;}
		else if (IPd == ip10) { k = 10;}
		else if (IPd == ip11) { k = 11;}
		else if (IPd == ip12) { k = 12;}
		else if (IPd == ip13) { k = 13;}
		else if (IPd == ip14) { k = 14;}
		else if (IPd == ip15) { k = 15;}
		
		
		ssFileName << "Node" << k << ".txt"; // create a file for each node and write what it received 
		
		f.open (ssFileName.str()); 
		f << "I received a packet from Node " << i << "\n";
		
		
		string ss ("Destinations");
		
		file >> IPd; // read the next destination address
		if ( ss.compare(IPd)!=0) 
		{
			goto here;
		}
		else 
		{
			goto next_N;
		}
	}
	dest.close ();
	f.close();

	
    return 0;
}

Sorry, my fault - you need to do:

f.open (ssFileName.str().c_str());

Let me know if that doesn't work.

Dave

Thank you David, I don't get any error when I compile it now, however, I still have the same problem as before which is no output files are written.

Add a

cout << "I received a packet from Node " << i << "\n";

right in front of where you do

f << "I received a packet from Node " << i << "\n";

If you see the output to the terminal, then the file is definitely being written. My guess is you are looking in the wrong place for the output files? They will probably be in the "bin" directory - or wherever the executable is being created.

Also, NEVER use 'goto'. Ever!

Dave

I guess something wrong in my codes bcoz I got an infinite lines of:
I received a packet from Node 0

btw, any suggestion what should I change goto with?

Thanks :)

Here is my new codes after some adjustment but still I get an infinite lines of:

I received a packet from Node 0
I received a packet from Node 1
to
I received a packet from Node 15


when I use :

cout << "I received a packet from Node " << i << "\n";
// instead of using 
f << "I received a packet from Node " << i << "\n";

though I have the loop while to check the end of file.. and when I remove the cout function I get an empty Node1.txt

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{

	
    ifstream file;
    file.open ("arp.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	// read all the data in the arp file
	
	char l;
	int i;
	int k;
	string IPd;
	string ip0, ip1, ip2, ip3, ip4 ,ip5, ip6, ip7;
	string ip8, ip9, ip10, ip11, ip12, ip13, ip14, ip15;
	string mac0, mac1, mac2, mac3, mac4, mac5, mac6, mac7;
	string mac8, mac9, mac10, mac11, mac12, mac13, mac14, mac15;
	ofstream f;
	stringstream ssFileName;
	
	file >> ip0 >> ip0 >> l >> ip0 >> mac0;
	file >> ip1 >> ip1 >> l >> ip1 >> mac1;
	file >> ip2 >> ip2 >> l >> ip2 >> mac2;
	file >> ip3 >> ip3 >> l >> ip3 >> mac3;
	file >> ip4 >> ip4 >> l >> ip4 >> mac4;
	file >> ip5 >> ip5 >> l >> ip5 >> mac5;
	file >> ip6 >> ip6 >> l >> ip6 >> mac6;
	file >> ip7 >> ip7 >> l >> ip7 >> mac7;
	file >> ip8 >> ip8 >> l >> ip8 >> mac8;
	file >> ip9 >> ip9 >> l >> ip9 >> mac9;
	file >> ip10 >> ip10 >> l >> ip10 >> mac10;
	file >> ip11 >> ip11 >> l >> ip11 >> mac11;
	file >> ip12 >> ip12 >> l >> ip12 >> mac12;
	file >> ip13 >> ip13 >> l >> ip13 >> mac13;
	file >> ip14 >> ip14 >> l >> ip14 >> mac14;
	file >> ip15 >> ip15 >> l >> ip15 >> mac15;
	
	
	file.close ();
	
	
    ifstream dest;
    dest.open ("destination.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	
	while (!dest.eof ())
	{
		
		
		dest >> IPd >> IPd >> IPd >> i;
		
		string IPs; // find the IP address of the source node
here:	if (i == 0){ IPs = ip0;}
		else if (i == 1){ IPs = ip1; }
		else if (i == 2){ IPs = ip2; }
		else if (i == 3){ IPs = ip3; }
		else if (i == 4){ IPs = ip4; }
		else if (i == 5){ IPs = ip5; }
		else if (i == 6){ IPs = ip6; }
		else if (i == 7){ IPs = ip7; }
		else if (i == 8){ IPs = ip8; }
		else if (i == 9){ IPs = ip9; }
		else if (i == 10){ IPs = ip10; }
		else if (i == 11){ IPs = ip11; }
		else if (i == 12){ IPs = ip12; }
		else if (i == 13){ IPs = ip13; }
		else if (i == 14){ IPs = ip14; }
		else if (i == 15){ IPs = ip15; }
		
		dest >> IPd >> IPd >> IPd;
		
		 // find the node number of the destination IP
next_D:	if (IPd == ip0) {k = 0; }
		else if (IPd == ip1) { k = 1;}
		else if (IPd == ip2) { k = 2;}
		else if (IPd == ip3) { k = 3;}
		else if (IPd == ip4) { k = 4;}
		else if (IPd == ip5) { k = 5;}
		else if (IPd == ip6) { k = 6;}
		else if (IPd == ip7) { k = 7;}
		else if (IPd == ip8) { k = 8;}
		else if (IPd == ip9) { k = 9;}
		else if (IPd == ip10) { k = 10;}
		else if (IPd == ip11) { k = 11;}
		else if (IPd == ip12) { k = 12;}
		else if (IPd == ip13) { k = 13;}
		else if (IPd == ip14) { k = 14;}
		else if (IPd == ip15) { k = 15;}
		
		
		ssFileName << "Node" << k << ".txt";
		
		f.open (ssFileName.str().c_str()); 
		f << "I received a packet from Node " << i << "\n";
		
		
		string ss ("Destinations");
		
		dest >> IPd; // read the next destination address
		if ( ss.compare(IPd)!=0) 
		{
			goto next_D;
		}
		else 
		{
			dest >> IPd >> IPd >> i;
			goto here:
		}
	}
	dest.close ();
	f.close();

	
    return 0;
}

Thanks!

there is an error in line 122, it should be:

goto here;

btw, am I using the while loop wrongly here and this is the reason it doesn't stop? and y I don't get the 16 txt file as an output?

any suggestion why I get a one empty output file only? (only Node1.txt)

Any hint what might be wrong here that makes me unable to write to 16 different files? and why the while loop never stops?

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{

	
    ifstream file;
    file.open ("arp.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	// read all the data in the arp file
	
	char l;
	int i;
	int k;
	string IPd;
	string ip0, ip1, ip2, ip3, ip4 ,ip5, ip6, ip7;
	string ip8, ip9, ip10, ip11, ip12, ip13, ip14, ip15;
	string mac0, mac1, mac2, mac3, mac4, mac5, mac6, mac7;
	string mac8, mac9, mac10, mac11, mac12, mac13, mac14, mac15;
	ofstream f;
	stringstream ssFileName;
	
	file >> ip0 >> ip0 >> l >> ip0 >> mac0;
	file >> ip1 >> ip1 >> l >> ip1 >> mac1;
	file >> ip2 >> ip2 >> l >> ip2 >> mac2;
	file >> ip3 >> ip3 >> l >> ip3 >> mac3;
	file >> ip4 >> ip4 >> l >> ip4 >> mac4;
	file >> ip5 >> ip5 >> l >> ip5 >> mac5;
	file >> ip6 >> ip6 >> l >> ip6 >> mac6;
	file >> ip7 >> ip7 >> l >> ip7 >> mac7;
	file >> ip8 >> ip8 >> l >> ip8 >> mac8;
	file >> ip9 >> ip9 >> l >> ip9 >> mac9;
	file >> ip10 >> ip10 >> l >> ip10 >> mac10;
	file >> ip11 >> ip11 >> l >> ip11 >> mac11;
	file >> ip12 >> ip12 >> l >> ip12 >> mac12;
	file >> ip13 >> ip13 >> l >> ip13 >> mac13;
	file >> ip14 >> ip14 >> l >> ip14 >> mac14;
	file >> ip15 >> ip15 >> l >> ip15 >> mac15;
	
	
	file.close ();
	
	
    ifstream dest;
    dest.open ("destination.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	
	while (!dest.eof ())
	{
		
		
		dest >> IPd >> IPd >> IPd >> i;
		
		string IPs; // find the IP address of the source node
here:	if (i == 0){ IPs = ip0;}
		else if (i == 1){ IPs = ip1; }
		else if (i == 2){ IPs = ip2; }
		else if (i == 3){ IPs = ip3; }
		else if (i == 4){ IPs = ip4; }
		else if (i == 5){ IPs = ip5; }
		else if (i == 6){ IPs = ip6; }
		else if (i == 7){ IPs = ip7; }
		else if (i == 8){ IPs = ip8; }
		else if (i == 9){ IPs = ip9; }
		else if (i == 10){ IPs = ip10; }
		else if (i == 11){ IPs = ip11; }
		else if (i == 12){ IPs = ip12; }
		else if (i == 13){ IPs = ip13; }
		else if (i == 14){ IPs = ip14; }
		else if (i == 15){ IPs = ip15; }
		
		dest >> IPd >> IPd >> IPd;
		
		 // find the node number of the destination IP
next_D:	if (IPd == ip0) {k = 0; }
		else if (IPd == ip1) { k = 1;}
		else if (IPd == ip2) { k = 2;}
		else if (IPd == ip3) { k = 3;}
		else if (IPd == ip4) { k = 4;}
		else if (IPd == ip5) { k = 5;}
		else if (IPd == ip6) { k = 6;}
		else if (IPd == ip7) { k = 7;}
		else if (IPd == ip8) { k = 8;}
		else if (IPd == ip9) { k = 9;}
		else if (IPd == ip10) { k = 10;}
		else if (IPd == ip11) { k = 11;}
		else if (IPd == ip12) { k = 12;}
		else if (IPd == ip13) { k = 13;}
		else if (IPd == ip14) { k = 14;}
		else if (IPd == ip15) { k = 15;}
		
		
		ssFileName << "Node" << k << ".txt";
		
		f.open (ssFileName.str().c_str()); 
		f << "I received a packet from Node " << i << "\n";
		
		
		string ss ("Destinations");
		
		dest >> IPd; // read the next destination address
		if ( ss.compare(IPd)!=0) 
		{
			goto next_D;
		}
		else 
		{
			dest >> IPd >> IPd >> i;
			goto here;
		}
	}
	dest.close ();
	f.close();

	
    return 0;
}

thx

I recommend simplifying the problem - definitely remove all of the user input and hard code some values that you want to write. 130 lines of code is very hard to debug. 20 is much more manageable.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.