0

I have a text file with the following information in it:

    2B,410,AER,2965,KZN,2990,,0,CR2
2B,410,ASF,2966,KZN,2990,,0,CR2
2B,410,ASF,2966,MRV,2962,,0,CR2
2B,410,CEK,2968,KZN,2990,,0,CR2
2B,410,CEK,2968,OVB,4078,,0,CR2
2B,410,DME,4029,KZN,2990,,0,CR2
2B,410,DME,4029,NBC,6969,,0,CR2
2B,410,DME,4029,TGK,\N,,0,CR2

(it is airline route info)

I'm trying to loop through the file and extract each line into a char* - simple right?

Well, yes, it's simple but not when you've completely forgotten how to write successful i/o operations! :)

My code goes a little like:

char * FSXController::readLine(int offset, FileLookupFlag flag)
{
    // Storage Buffer
    char buffer[50];
    std::streampos sPos(offset);

    try
    {
        // Init stream
        if (!m_ifs.is_open())
            m_ifs.open(".\\Assets\\routes.txt", std::fstream::in);
    }
    catch (int errorCode)
    {
        showException(errorCode);
        return nullptr;
    }

    // Set stream to read input line
    m_ifs.getline(buffer, 50);

    // Close stream if no multiple selection required
    if (flag == FileLookupFlag::single)
        m_ifs.close();

    return buffer;

}

Where m_ifs is my ifStream object.

The problem is that when I breakpoint my code after the getline() operation, I notice that 'buffer' has not changed?

I know it is something simple, but please could someone shed some light onto this - I'm tearing my forgetful hair out! :)

P.S: I never finished writing the exception handling so it is pretty useless right now!

Thanks

15
  • 1
    You return a pointer to a local variable. Local variables go out of scope when a function exits. Why aren't you using std::string and std::getline? Commented Aug 29, 2016 at 18:05
  • c-style strings should be avoided in c++. Is there a reason you explicitly using a char buffer instead of a std::string ? Commented Aug 29, 2016 at 18:06
  • Also, what would you like to do for each line? What is the final data structure you want to have and to populate from the file? Commented Aug 29, 2016 at 18:09
  • Ensure that you were able to successfully open the file. Commented Aug 29, 2016 at 18:12
  • 1
    @GuyLeonardThomas using a std::string is easier... Commented Aug 29, 2016 at 18:13

2 Answers 2

3

Here is a fix with some important c++ libraries you may want to learn, and what I believe a better solution. Since you just need your final result to be strings:

// A program to read a file to a vector of strings 
// - Each line is a string element of a vector container
#include <fstream>
#include <string>
#include <vector>

// ..

std::vector<std::string> ReadTheWholeFile()
{
    std::vector<std::string> MyVector;
    std::string JustPlaceHolderString;
    std::ifstream InFile;

    InFile.open("YourText.txt"); // or the full path of a text file

    if (InFile.is_open())
        while (std::getline(InFile, PlaceHolderStr));
            MyVector.push_back(PlaceHolderStr);

    InFile.close(); // we usually finish what we start - but not needed
    return MyVector;
}

int main()
{
    // result
    std::vector<std::string> MyResult = ReadTheWholeFile();

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are two basic problems with your code:

  1. You are returning a local variable. The statement return buffer; results in a dangling pointer.

  2. You are using a char buffer. C-style strings are discouraged in c++, you should always prefer std::string instead.

A far better approach is this:

string FSXController::readLine(int offset, FileLookupFlag flag) {
    string line;
    //your code here 

    getline(m_ifs, line) //or while(getline(my_ifs, line)){ //code here } to read multiple lines
    //rest of your code
    return line;
}

More information about std::string can be found here

4 Comments

Just checking the code now, will upvote when I've got it working :)
@GuyLeonardThomas let me know if you have any problems
Still having issues where the file isn't being opened, (even with absolute path) can you think of any reasons the file wouldn't open? Or that getLine would return false?)
@GuyLeonardThomas if the file is on the same directory as the program you should not have any problems. The usual pattern is to open the file with an ifstream object and then read it line by line using the getline() function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.