A
A
Arti-Jack2018-05-17 19:15:50
C++ / C#
Arti-Jack, 2018-05-17 19:15:50

Why is the file read as empty?

I have main.cpp and input.txt.
Here is the code:

spoiler
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>



using namespace std;

string getResult(vector<string> val) {
    bool flag = true;
    for (int i = 0; i < val.size(); i++) {
        // Iteration
        string current = val[i];

        for (int j = 0; j < current.size(); j++) {
            if (current[j] != ' ') {
                // If first word is uppercase
                if (flag && isupper(current[j]) == 0) {
                    current[j] = toupper(current[j]);
                    val[i] = current;
                }
                flag = false;
            }
        }
        flag = true;
    }

    string result;
    for (int i = 0; i < val.size(); i++)
        result += val[i] + ".";

    return result;
}



int main() {
    ifstream file("input.txt");
    string str;
    string file_contents;

    if (!file.is_open()) {
        cout << "Some problems with file...";
        return -1;
    }



    while (getline(file, str)) {
        file_contents += str;
        file_contents.push_back('\n');
    }

    vector<string> array;

    // split
    istringstream is(str);
    string s;
    while (std::getline(is, s, '.'))
        array.push_back(s);

    cout <<"Current input: "<< str << endl;

    string result = getResult(array);

    cout << "After: " << result << endl;
    return 0;
}

I run it:
$: g++ -o prog main.cpp
$: ./prog

I get:
Current input: 
After:


It feels like the file is empty, but it's not empty. The file is in the same folder as main.cpp. What is the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question