Help with reverse file strings

So I have to write a program where it gets the name of an input file and output file (if they're identical I am to get an error message) and pass them as arguments to a function called RevFile.
RevFile reads the input file one line at a time, and copies them to the output file, but in reverse. (ex: abc would be cba), and when the end of the input file is reached RevFile returns total number of lines written to the caller.

Things I am told I'm supposed to use: .getline() to fetch null-terminated cstring
arrays to derive the index location and move backwards one character at a time in the array until you reach index location zero
strlen function to help see how many characters in the input file

Any help appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>

using namespace std;

const int LEN=256;

int main()
{
 ifstream      inData[LEN];
 ofstream      outData[LEN];



  cout << "Enter the name of an input file: ";
  cin.getline(inData, LEN);
  cout << "Enter the name of an output file: ";
  cin.getline(outData, LEN);
bump
Hello iseehealthbars,


You mentioned: use: .getline() to fetch null-terminated cstring. getline() will only work with a c++ string. See: http://www.cplusplus.com/reference/string/string/getline/?kw=getline

You are trying to mix C and C++ code and that is not the best way to write a program.

Lines 10 and 11 create an array of 256 of type ifstream and 256 ofstream. Why do you need that many?

After that you never open any files.

Lines 16 and 18 are not the way to use the getline(). Line 18 you can not read a file that would be an output.

I suggest that after you open the file test them to make sure they are opened before you try to read or write to a file.

Then you will need a loop to read the input file and process the contents. A function to reverse the string before you write to the output file.

Hope that helps,

Andy
closed account (LA48b7Xj)
You mentioned: use: .getline() to fetch null-terminated cstring. getline() will only work with a c++ string. See: http://www.cplusplus.com/reference/string/string/getline/?kw=getline

You can use the getline that is a member of istream for c-strings.

This should give you something to work with...
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
    cout << "enter input file: ";
    char input_file[256];
    cin.getline(input_file, 256);

    cout << "enter output file: ";
    char output_file[256];
    cin.getline(output_file, 256);

    if(strcmp(input_file, output_file) == 0)
    {
        cerr << "files can't be the same\n";
        return 1;
    }

    ifstream fin(input_file);
    ofstream fout(output_file);

    if(!fin || !fout)
    {
        cerr << "file loading error\n";
        return 1;
    }

    char str[256];
    while(fin.getline(str, 256))
    {
        const int len = strlen(str);

        for(int i=len-1; i >= 0; --i)
            fout.put(str[i]);
        fout.put('\n');
    }
}

Last edited on
@krako,

Guess I did not read far enough. I have only used getline with C++ strings.

Andy
Could you explain what each line or lines are doing? I'm having trouble understanding the syntax.

and what is "cerr"? I have yet to see that.
Topic archived. No new replies allowed.