-1

When I write cout<< Read("filepath")I get strange set of identical characters. Code is below:

char* Read(std::string FilePath)
{
    char* Buffer;

    ifstream F_S(FilePath);
    while (! F_S.eof())

    {

        F_S.getline(Buffer,100);
        return Buffer; 

    }
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356

2 Answers2

3

Multiple problems here:

Firstly, your return statement is inside your while loop, which means the loop will only execute once and then you'll just return from the function. The return statement needs to be placed outside of the while loop.

Secondly, you don't allocate any storage for Buffer. All you do is declare a char* pointer, which isn't even initialized. You need to explicitly allocate storage so that ifstream::getline has somewhere to store the input. You can use new to allocate storage, but since your Read function returns a char* pointer, the calling function would need to manually manage the memory, which is generally just a headache.

A better solution is to use the free function std::getline, since it takes an std::string, so you don't have to worry about storage allocation. You'll also need to change the function signature of Read to return an std::string.

std::string Read(std::string FilePath)
{
    std::ifstream F_S(FilePath.c_str());
    /* You should also check for `F_S.good()` here */

    std::string result, Buffer; 
    while(std::getline(F_S, Buffer)) 
    {
        result += Buffer;
    }

    return result;
}

Note that if you want to preserve carriage returns in the file, you'll need to manually add them to result within each iteration, since std::getline discards delimiter characters.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • 1
    Third problem. [*Never* use `.eof()` as a loop condition](http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line). Also, your proposed solution only ever returns the last line read. Try: `std::string result, Buffer; while(std::getline(F_S, Buffer)) result += Buffer; return result;` – Robᵩ Apr 18 '12 at 19:37
  • Another problem: `std::ifstream::open` requires a `char *`, not an `std::string`. Use `std::string::c_str()`. – Thomas Matthews Apr 18 '12 at 19:49
  • @Thomas, good catch: I didn't notice that because apparently, GCC accepts `std::ifstream::open(std::string)`. I guess it's an extension. – Charles Salvia Apr 18 '12 at 19:53
0

you can define your read like this.

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

string& Read(string file_path)
{
    ifstream infile(file_path.c_str(), ios::in);
    string result = "";
    string line = ""; 
    while (getline(infile, line)) 
    {
        result.append(line); //also you can use result+=line, but append is faster than +=
    }

    return result;

}
argue2000
  • 31
  • 1
  • As [an anonymous user noted](http://stackoverflow.com/suggested-edits/247435), returning a reference here is probably wrong: it'll be a reference to a temporary string on the stack which will no longer exist outside the function. You should return just a `std::string` instead. – Rup Apr 25 '12 at 10:12
  • Also: why would the `result.append()` be quicker than `result += line`? I'd expect the implementation to be exactly the same! – Rup Apr 25 '12 at 10:13