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
#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)
            cout << "ERROR: cannot open file! ";
            
    readFile.getline(info, SIZE);
    while(!readFile.eof())
    {
        cout << info << endl;
        readFile.getline(info, SIZE);
    }
    readFile.close();
Last edited on
Bump?
Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
fstream writeFile;

writeFile.open( "Result.txt", ios::out );

// inside while loop --
// parse info
// if info is sales
//   write out with *****
// else
//   write out line
// ------------------------

writeFile.close();
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.
Computers are stupid - they will do exactly what you tell them to do.
BTW, did you just copy, paste and edit your whole chunk of code?

Ask yourself: what did I just tell the computer to do in the new code?
1. Open Bands.txt, read all the text in the file, close the file.
2. Open Results.txt, write what's in info, ten times, close the file.

Well, at the end of 1, info will contain just the very last value from Line 28, right?
So the computer just did what you told it to do!

Clue: you need to intermingle your code a little bit - rather than doing one file at a time, open both files and process one line at a time: read one line, write one line, etc...

When coding, think about what the computer will do when it reads your code, because the machine is really dumb - it will do exactly what you tell it to do.
Oh wow thanks, i got it to write bands.txt to results.txt, man i was stuck on this for so long its not even funny, and with help too, but for some reason your suggestion was the easiest to understand for me. Ok well now that i have this, do you have any clue on how to do the asterisks? I have to convert an int to an asterisk, and this is another thing i have been stuck at, and i ask for help on that one but no one answers :(
Last edited on
Good.

A big part of problem-solving and programming is breaking down what you want to do into smaller, easy-to-do parts. When you get more experience, you will be able to do this naturally.

You are thinking about it the wrong way.

1. You start with a character array, char info[SIZE];
2. If you detect the pattern that you are looking for, let's say the 5 in XXXXXX5YYY
3. Then you want to copy XXXXXX into your new string, append ****** to that string, and then append YYY into that string.
4. Write out your new string into Result.txt

If you want to continue using a character array (which is fine), as opposed to std::string in C++, look into strncpy() and strcat(). Good luck.
Last edited on
Topic archived. No new replies allowed.