Read a text file, store in an array and then output reverse the text file

Hi Guys!

I am new here and really stuck on this question. So I want to read a text file and store in an array and then print the output in reverse. I got an error that Unhandled exception at 0x01068C96. Also, the console shows the output text but not in reverse. Thanks for your help and here is my code so far. For example:


The text.txt has:

hi
this is the test

Output should look like:

hi
test the is this.


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;



void readArray(char* input_filename) {

const size_t len = strlen(input_filename);

for (size_t i = 0; i < len / 2; i--)
swap(input_filename[i], input_filename[len - i - 1]);

}


int main()
{
string line;
char input_filename[5];

ifstream file("text.txt");

if (file.is_open()) {
while (!file.eof()) {
getline(file, line);
cout << line << endl;
}
file.close();
}
else cout << "Unable to open the file";

readArray(input_filename);
cout << input_filename;

return 0;

}
Last edited on
So if the file has

test line 1
test line 2
test line 3

you want to read each line into a string, store that string in an array of strings, then output the strings in reverse order, so that you see

test line 3
test line 2
test line 1

?
Thanks for your response, for example:

The text.txt has:

hi
this is the test

Output should look like:

hi
test the is this.
I would recommend renaming your function readArray to something else that more closely resembles what its doing: i.e. reverseStrings or something.

Also, just to explain that your char array is uninitialized; what this means is that after you declared it in main, the 5 element array contains garbage data. So, when you pass it to readArray, which in turn calls strlen on it, strlen will end up reading past the end of the array since there is no null-terminator ('\0') that signifies the end of the char array you passed in. This generates the exception.

However, this ends up being moot as your plan to store the strings into the char array wouldn't work - due to incompatible types. i.e. you can't store a string into a char *array.

What you COULD do is create an array of strings like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string inputs[5]; //I'll assume you know the # of lines beforehand - Trying
//to keep it simple here

ifstream file("text.txt");

if (file.is_open()) {
int index = 0;

while (!file.eof() && index < 5) {
getline(file, inputs[index]);
cout << line << endl;
++index;
}
file.close();
}
else cout << "Unable to open the file";

readArray(inputs); //You WILL need to modify your readArray parameter list so 
//that it takes a string array instead of a char *. 


Again, this is one possible way of solving it.

Cheers & let me know if you need more help,
Joe
sparkprogrammer@gmail.com
concordspark.com


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
# include <iostream>
# include <deque>
# include <string>
# include <sstream>
# include <fstream>

int main()
{
    std::ifstream inFile {"C:\\test.txt"};
    if(inFile)
    {
        std::string line;
        while (getline(inFile, line))
        {
            std::istringstream stream{line};
            std::deque <std::string> words{};
            std::string word;
            while (stream >> word )
            {
                if(stream)words.push_front(word);
            }
            for (const auto& elem : words)std::cout << elem << " ";
            std::cout << "\n";
        }
    }
}
@Little Captain thanks a lot, char* is required as a parameter. If I change to string inputs[5] and when I call the readArray, it's giving me an error. Any idea? Thanks
If you change to string inputs[5], you'd also have to change the function to take in a string array instead of a char array (see Little Captain's comment in the code he posted.)

With your original code, each line is being reversed, but it doesn't seem like that's what you want to do. You want to isolate the individual words in the char array and print them out in reverse order? Are there any other restrictions or requirements besides using a character array?

I added some output statements to the function and fed it a single character array here. I also changed the i-- to i++ in the for loop statement.

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
void readArray(char* input_filename){
    const size_t len = strlen(input_filename);

    for (size_t i = 0; i < len / 2; i++){ // change i-- to i++
        std::cout << "\n\ninput_filename[i] is " << input_filename[i];
        std::cout << "\ninput_filename[len - i - 1] is " << input_filename[len - i - 1];
        std::cout << "\nswapping these letters....";
        std::swap(input_filename[i], input_filename[len - i - 1]);
        std::cout << "\nresult: " << input_filename;
    }
    std::cout << "\n";
}

int main() {

    char inputLine[] = "This is the test";

    readArray(inputLine);
}
input_filename[i] is T
input_filename[len - i - 1] is t
swapping these letters....
result: this is the tesT

input_filename[i] is h
input_filename[len - i - 1] is s
swapping these letters....
result: tsis is the tehT

input_filename[i] is i
input_filename[len - i - 1] is e
swapping these letters....
result: tses is the tihT

input_filename[i] is s
input_filename[len - i - 1] is t
swapping these letters....
result: tset is the sihT

input_filename[i] is  
input_filename[len - i - 1] is  
swapping these letters....
result: tset is the sihT

input_filename[i] is i
input_filename[len - i - 1] is e
swapping these letters....
result: tset es thi sihT

input_filename[i] is s
input_filename[len - i - 1] is h
swapping these letters....
result: tset eh tsi sihT

input_filename[i] is  
input_filename[len - i - 1] is t
swapping these letters....
result: tset eht si sihT
Topic archived. No new replies allowed.