Hi guys,
When I run this code, I got segmentation fault and i can't figure out where the mistake is? I'd really appreciate any help. thanks

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

using namespace std;
int main ()
{
    ifstream file, dest;
    file.open ("arp.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	
	int size = 16;
	string v, ip[15], mac[15], IPs, IPd; 
	
	for (int k = 0; k < size; k++)  // reading the first file
	{
		file >> v >> v >> v >> ip[k] >> mac[k]; // v reads all the things before the IP
	}
	
	file.close ();
	
	dest.open ("dest.txt");
    if (!file)
    {
        cout << "Unable to open file"; // if file doesn't exist
        exit(1);
    }
	
	int s, d, z;
	dest >> v >> v >> v >> s; // here s will read an integer number from 0 - 15
	IPs = ip[s];
	dest >> v >> v >> IPd; // IPd will have an IP address that similar to one of the ip[k]
	
	for (int m = 0; m < size; m++)  
	{
		if (IPd == ip[m])
		{
			d = m; 
		}
	}

Recommended Answers

All 9 Replies

This loop:

for (int k = 0; k < size; k++)

will insert values in ip[k], where k = a number between 0-15.
However you declared "ip" as an array with only 15 elements: string v, ip[15]... which means you can only use index 0-14. So when you try to write to index 15 --> boom! Segmentation fault.

Thank you very much Nick. I just got one more question,
Can I open 16 files and write to them in a simple way rather than this way? and I'll write to them more than one time during my program.

ofstream f0, f1, f2, f3 ,f4 ,f5 ,f6 ,f7;
ofstream f8 ,f9, f10, f11, f12 ,f13, f14 ,f15;
f0.open ("N0.txt"), f1.open ("N1.txt"), f2.open ("N2.txt");
f3.open ("N3.txt"), f4.open ("N4.txt"), f5.open ("N5.txt");
f6.open ("N6.txt"), f7.open ("N7.txt"),f8.open ("N8.txt");
f9.open ("N9.txt"), f10.open ("N10.txt"), f11.open ("N11.txt");
f12.open ("N12.txt"), f13.open ("N13.txt"), f14.open("N14.txt");
f15.open ("N15.txt");

Thanks in advance!

Why don't you use arrays, it would be like this

ofstream f[16];
char buffer[255];
for (int i = 0; i < 16; i++)
{
    snprintf(buffer, sizeof(buffer) - 1, "N%d.txt", i);
    f[i].open(buffer);
}

/* Then do what you want here,
 * use f[i] instead of f_i as your old code. */

You can use sprintf() instead of snprintf() but that function is really unsafe because the compiler does not perform the bound checking.

thanks a lot but i'm writing my codes in c++ so I did it like this:

int size = 16;
        ofstream f[16];
	stringstream ssFileName;
	for (int k = 0; k < size; k++) // prepare 16 files to write to them
	{
		ssFileName << "N" << k << ".txt";
		f[k].open (ssFileName.str().c_str());
	}	
        // some other codes and then
        f[d] << "I received a packet from Node " << s << "\n"; // d is an integer between 0 - 15
        for (int k = 0; k < size; k++) // close all the 16 files
	{
		f[k].close();
	}

And when I run my codes, I got this error:
Usage:
ff [-F FSType] [-V] [current_options] [-o specific_options] special ...
where ff is my output file name

I believe what you are missing is setting your stringstream back to empty before re-assiging it:

ssFileName.str ("");
ssFileName << << "N" << k << ".txt";

This is my test code and it works just fine:

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

using namespace std;

int main( int argc, char **argv )
{
    int size = 3;
    ofstream f[size];
    stringstream ssFileName;

    for (int i = 0; i < size; i += 1)
    {
        ssFileName.str( "" );
        ssFileName << "N" << i << ".txt";
        cout << ssFileName.str() << endl;
        f[i].open( ssFileName.str().c_str() );
    }

    for (int i = 0; i < size; i++)
        f[i] << "I received something!" << i << endl;

    for (int i = 0; i < size; i++)
        f[i].close();
    
    return 0;
}

I did exactly as u said and I still got the same error:

Usage:
ff [-F FSType] [-V] [current_options] [-o specific_options] special ...

Could you please tell me the commands you used to compile and run your program?

Have you named your executable the same name as some other system software? It doesn't seem like your code has any error message like that. Try renaming your program.

commented: problem solved. +12

Thank u both of u..
I used this code:
g++ -o ff ff.cpp

but it works now after I changed the ff to other name. thanks alot :)

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.