Stuck at read/write from file to file

Hey guys, i am currently working on an assignment of an intro to c++ class, and i am being asked to read from a file called Bands.txt which has a list of band information (rank/sales/band name (in that order)) and then write from that Bands.txt file to a file called Results.txt and it's supposed to have the sales converted from a number to asterisks (ex. 5 to ******).
What i have so far is the reading part from the Bands.txt, but i cannot get the program to create Results.txt and write the list there :(
Any help is appreciated, thanks :)
Here's what i have:
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
    fstream readFile;
    const int SIZE = 100;
    char info[SIZE];
    
    
    readFile.open("Bands.txt", ios::in);
    cout << "Here's the rank/sales/band name: \n";
    
    if (readFile.fail())
    {
        cout << "ERROR: cannot open file! \n";
    }       
    readFile.getline(info, SIZE);
    while(!readFile.eof())
    {
        cout << info << endl;
        readFile.getline(info, SIZE);
    }
    readFile.close();
Last edited on
Just use another fstream in output mode
Yeah but the thing is getting the contents of Bands.txt to get written to Results.txt
I did another fstream in output mode but the thing is that only the last line in the list that is in Bands.txt is written to Results.txt, the rest was ignored.
here's hoping for more help :(
either write every line as soon as you read it, or
store all the lines, traverse the container, and output the lines.

I don't see any file stream in output mode in your code.
Oh well here is the new code:
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
    ifstream readFile;
    const int SIZE = 100;
    int num1 = 0;
    int num2 = 0;
    char info[SIZE];
    int counter = 0;
    
    readFile.open("Bands.txt", ios::in);
    cout << "Here's the rank/sales/band name: \n";
    
    if (!readFile)
        cout << "ERROR: cannot open file! \n";  
    while(!readFile.eof())
    {
        readFile >> num1;
        cout << num1;
        readFile >> num2;
        cout << setw(2)<< num2;
        readFile.getline(info, SIZE);
        cout << info << endl;
    }
    
    readFile.close();
        
    ofstream myfile;
    myfile.open ("Results.txt", ios::out);
    if (!myfile)
        cout << "ERROR: cannot open file! \n";
    while (counter < 10)
    {
        myfile << num1;
        myfile << setw(2)<< num2;
        myfile << info << endl;
        counter++;
    }
    
    myfile.close();
           
    system("PAUSE");
    return EXIT_SUCCESS;

The problem im having now is that every line in Results.txt is the last line in Bands.txt, also, i am stuck at converting an int into asterisks.
Im having the same situation. Also, i cant figure out how to turn put them in rank order, or to change the sales #s to ****'s.

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 <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
 {
  string line;
  int count = 0;
  
  
  ifstream readfile ("Bands.txt");
  if (readfile.is_open())
  {
    while ( readfile.good() )
    {
      getline (readfile, line);    
      cout << line << endl;
    }
    readfile.close();
  }
  
    else cout << "Unable to open file"; 


  ofstream myfile ("Results.txt");
  if (myfile.is_open())
  {
   
   
    myfile.close();
  }
  else cout << "Unable to open file";

    system("PAUSE");
    return EXIT_SUCCESS;
}
of course you will output the same line, if you aren't change it
1
2
3
4
5
6
7
 while (counter < 10) //why 10?
    {
        myfile << num1;
        myfile << setw(2)<< num2;
        myfile << info << endl; //info never changes
        counter++;
    }

i am stuck at converting an int into asterisks.
std::cout<<'*'; this will print one asterisk. Repeat it n times.
Topic archived. No new replies allowed.