file input stream troubles

closed account (4ET0pfjN)
Hi, I want my function to generate random numbers, it takes in parameters: outstream object, number of random numbers to generate, and the file to save to.

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
#include <fstream>//to read/write from/to files
#include <cstdlib>//to use srand seed and rand
#include <ctime>
#include <iostream>
#include <string>//to use C++ string object (different from C's C-string)
using namespace std;

void makeData(ofstream of, int numItems,string fileName)
{
	int itemsPerLine = 0;//used to organize data readability in text file to print x data per line
	const int LOW = 0;
	const int HIGH = 2*numItems;
	int randElem;
	
	of.open(fileName);//creates a new file if file doesn't exist
	
	for ( int i = 0; i < numItems; i++ )
	{
		randElem = rand() % (HIGH - LOW + 1) + LOW;
		if ( itemsPerLine == 10 )
		{
			of << "\n";
			itemsPerLine = 0;//reset for brand new list of x items in a row
		}

		of << randElem <<" ";
		itemsPerLine+=1;	
	}	
	of.close();
}

int main()
{	
	/*MAKE RANDOM DATA*/
	/*RANDOM DATA FOR 1000 ITEMS*/

	srand(time(0));
	ofstream of;
	makeData(of, 1000, "randomData1000.txt");
	
	return 0;
}


It's not running, any help appreciated!
Last edited on
It's not possible to copy stream objects. You probably want to pass by reference instead.
void makeData(ofstream& of, int numItems,string fileName)
closed account (4ET0pfjN)
No, it doesn't work.
Then you probably did something wrong? The line you should change is line 8.
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
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

void makeData(ofstream &of, int numItems,string fileName)
{
  int itemsPerLine = 0, randElem;
  const int LOW = 0, HIGH = 2*numItems;

  of.open(fileName.c_str());
  if(of.good())
  {
    for(int i = 0;i < numItems;i++)
    {
      randElem = rand() % (HIGH - LOW + 1) + LOW;
      if(itemsPerLine == 10)
      {
        of << "\n";
        itemsPerLine = 0;
      } else {
        of << randElem <<" ";
        itemsPerLine+=1;  
      }
    } 
  }
}


int main()
{ 
  srand(time(0));
  ofstream of;
  makeData(of,1000,"randomData1000.txt");
  return 0;
}
closed account (4ET0pfjN)
so you are converting parameter to a pointer to the array that holds the actual string content in file when you use c_str() function?
Yep, c_str() returns a cstring pointer (const char*)
closed account (4ET0pfjN)
Hi, I decided to make this function more general, to be able to build a list of integers depending on the parameter string of "random", "sorted", "reversed", but it's not working.

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
#include <fstream>//to read/write from/to files
#include <cstdlib>//to use srand seed and rand
#include <ctime>
#include <iostream>
#include <string>//to use C++ string object (different from C's C-string)
using namespace std;

void makeData(ofstream &of, string fileName,string dataType,int numItems,)
{
	int itemsPerLine = 0;//used to organize data readability in text file to print x data per line
	int randElem;
	const int LOW = 0;
	const int HIGH = 2*numItems;
	of.open(fileName.c_str() );//creates a new file if file doesn't exist
	if ( dataType == "random")
	{
		for ( int i = 0; i < numItems; i++ )
		{
			randElem = rand() % (HIGH - LOW + 1) + LOW;
			if ( itemsPerLine == 10 )
			{
				of << "\n";
				itemsPerLine = 0;//reset for brand new list of x items in a row
			}

			of << randElem <<" ";
			itemsPerLine+=1;	
		}
		of.close();
	}//END RANDOM LIST GENERATOR BLOCK
	
	if ( dataType == "sorted" )
	{
		for ( int i = 0; i < numItems; i++ )
		{
			if ( itemsPerLine == 10 )
			{
				of << "\n";
				itemsPerLine = 0;//reset for brand new list of x items in a row
			}

			of << i <<" ";
			itemsPerLine+=1;	
		}
		of.close();
	}//END SORTED LIST GENERATOR BLOCK
	
	if ( dataType == "reverse" )
	{
		for ( int i = numItems; i >= 0; i-- )
		{
			if ( itemsPerLine == 10 )
			{
				of << "\n";
				itemsPerLine = 0;//reset for brand new list of x items in a row
			}

			of << i <<" ";
			itemsPerLine+=1;	
		}
		of.close();
	}//END REVERSED LIST GENERATOR BLOCK
}//END makeData FCN

int main()
{
  /*MAKE RANDOM DATA*/
 ofstream myFileRand;
 makeData(myFileReverse,"randomData1000.txt","random",1000);
 
  /*MAKE REVERSED DATA*/
  ofstream myFileReverse;
  makeData(myFileReverse,"reversedData1000.txt","reversed",1000);

  /*MAKE SORTED DATA*/
  ofstream myFileSorted;
  makeData(myFileSorted,"sortedData1000.txt","sorted",1000);
 
 return 0;
}
check your code again might have made mistakes
Topic archived. No new replies allowed.