CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    70

    Arrow Return Array Function

    Hello, i am new to c++ so i hope you guys can help me out. I have searched the answer up on google and while i find examples i cant quite understand whats going on yet. I want to make a simple file read function so i dont have to keep reading a file in 100 different places in my program.

    Here is what i have, it is very simple and obviously wrong. It is supposed to read the file and store it in a array (which it does) and then return the array so i can use the data.

    Code:
    string ReadWrite(char * FileName)
    {
    	int i = 1;
    
    	// String to hold all the file lines in
    	string line[50];
    	string FileContent;
    
      ifstream myfile (FileName);
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {
    
    	  // Read a line out of the file
          getline (myfile,line[i]);
    
    	  // Increase the Array by one so we dont overwrite
          ++i;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
    return line[];
    }

    Any help is greatly appreciated. Sorry for messy formatting.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Return Array Function

    Why not try something like this:

    Code:
    int LoadText(const std::string& filename, std::string& buffer)
    {
        std::ifstream fs(filename);
        if (!fs) return -1;
        std::copy( std::istreambuf_iterator<char>(fs), 
            std::istreambuf_iterator<char>(), 
            std::back_inserter(buffer) );
        return 0;
    }
    
    std::string filetext;
    if ( LoadText("somefile.txt", filetext) )
    {
        std::cout << filetext;
    }
    else
    {
        std::cout << "Couldn't open file!";
    }
    If you really want to access the text by indexing by line number, then maybe something like this:

    Code:
    int LoadText(const std::string& filename, std::vector<std::string>& buffer)
    {
        std::ifstream fs(filename);
        if (!fs) return -1;
        std::string line;
        while ( std::getline(fs, line) )
        {
            buffer.push_back(line);
        }
        return 0;
    }
    
    std::vector<std::string> filetext;
    if ( LoadText("somefile.txt", filetext) )
    {
        std::cout << filetext[0]; //first line
    }
    else
    {
        std::cout << "Couldn't open file!";
    }
    Last edited by Chris_F; June 4th, 2011 at 03:33 AM.

  3. #3
    Join Date
    Sep 2008
    Posts
    70

    Re: Return Array Function

    Thanks a lot for the help . I realize i may have posted this in the wrong section because i am using Visual C++ and it has given me errors in the past with strings.

    I am getting this error:

    cannot convert parameter 1 from 'const std::string' to 'const char *'


    I tried making it a cont char * and though it will run it crashes immediately on the second example code and does nothing on the first. Anyway you can help me out again? or should i be making a thread in the Visual C++ board?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Return Array Function

    There is no implicit conversion from std::string to const char *. Moreover, they are basically incompatible so doing a simple serach-and-replace between the two is an almost 100&#37; certain way to ruin your program.

    However, there is a member function of std::string called c_str() which does exactly the conversion you want. You just need to call it explicitly.

    EDIT: And note that this is all standard stuff and should apply likewise to any (decent) compiler.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Sep 2008
    Posts
    70

    Re: Return Array Function

    Yea i was 90&#37; certain it would not work but i gave it a shot. Thank you for the c_str()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured