I need help adding arrays/strings together

You will have two file streams: an input file stream for the file to be normalized and an output file stream that contains the normalized file. You should issue an error message and exit the program if either file cannot be opened.
Your program should prompt the user for the filename of the file to be normalized (the input file). The output filename should be the input filename with ".normal" added to it. For example, if the input file is "data.txt", the output file name will be "data.txt.normal".

Be careful to not leave an extra blank line at the end of your output file.


My question here is how do I rename the file that the user entered to have a ".normal" at the end of it? I was thinking along the lines of having to string names and changing the second string's name and use that as the output file. any examples cause I didn't exactly get that to work.

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

int main() { 
    ifstream fin;
    ofstream fout;
    string filename;
    cout << "What file do you want to be normalized? \n";
    cin >> filename;
    cout << "\n";
    fin.open(filename); 
    if (fin.fail()) { 
        cout << "Error opening input file! \n";
        return 0;
    }
    string normal = ".normal";
    string outFile; 
    outFile += filename;
    outFile += normal;
    fout.open(outFile);
    if (fout.fail()) {
        cout << "Error opening output file! \n";
        return 0; 
    }
    fin.close();
    fout.close();
}
Last edited on
Hi,

the operator is +=, but you have =+

However, this would only work with std::string you cannot do this with char arrays. Are you allowed to use std::string ? Because that would be much easier.

If not you need to use a for loop to add characters one at a time to the end of the file name, and place a null character '\0' at the end. First you need to find out where the actual end of the file name is - remember array subscripts start at 0.

Once you have declared a char array, you don't need to put the size of the array into statements.
@TheIdeasMan

Yes we can use strings. but we have to use "using namespace std;" so no std::string.

What you are saying is I need to declare strings and add them together with += instead.

I changed my code but that gave me even more errors.
Last edited on
Yes.

It is a shame about this part:

but we have to use "using namespace std;" so no std::string.


Are you sure - did they explicitly say that?

I wish Authors & Lecturers would quit doing this - it is bad practice.
Yeah. Since the professor hasn't gone over it yet, we can't use it.
"I didn't teach it so you can't use it. Or I consider that you cheated or used outside help."
applies to everything he hasnt taught us. printf, scanf, and etc


i get the error "no matching function to call to 'std::basic_ifstream<char>::open(std::string&)'

It works perfectly in c++ shell here but not in the mac terminal
Last edited on
Hi,

It doesn't run in my C++ shell here.

You are redeclaring your variables.

Are you sure this is the entire assignment? It just seems too easy & trivial with strings. I am confused now as to whether you should be using arrays. I also hope there is no confusion over the difference between a char array and a std::string?

You said you were allowed to use strings, but then said no std::string ? The 2 are equivalent.

Why do you have C header files included?

This is just part of the assignment.
http://www.cs.csub.edu/~gordon/cs221/hw5.html
Part 1 of the HW assignment only.

Thanks for helping out. I changed it not redeclaring but it doesnt work in my mac terminal.

The headers were just there.
Last edited on
Ok, so what doesn't work?

Does it compile? - it does on my C++ shell here.

Does the mac have any special naming conventions for file names - does one have to type in the whole path with escape characters for the slashes? Is the file in the same directory as the program?

You might need the c_str function which might be necessary when dealing with file names.

http://www.cplusplus.com/reference/string/string/c_str/


Edit:

Also there is the plain + operator for strings:

1
2
3
string outFile = filename + normal; 
    outFile += filename;
    outFile += normal;
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

    string filename;
    cout << "What file do you want to be normalized? \n";
    cin >> filename;

    // constructor opens the file http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
    // C++98 did not accept a std::string
    //  http://www.cplusplus.com/reference/string/string/c_str/
    ifstream fin( filename.c_str() ); // C++98

    if( !fin.is_open() ) { // http://www.cplusplus.com/reference/fstream/ifstream/is_open/

        // http://www.cplusplus.com/reference/iostream/cerr/
        cerr << "Error opening input file! \n";

        return 1 ; // by convention, return of zero indicates success, non-zero indicates failure
    }

    const string normal = ".normal";
    const string outfilename = filename + normal ;
    ofstream fout( outfilename.c_str() );

    if( !fout.is_open() ) {
        cerr << "Error opening output file! \n";
        return 1;
    }

    // the streams are automagically closed when we exit main()
}
Topic archived. No new replies allowed.