-
June 4th, 2011, 02:46 AM
#1
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.
-
June 4th, 2011, 03:28 AM
#2
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.
-
June 4th, 2011, 12:06 PM
#3
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?
-
June 4th, 2011, 12:15 PM
#4
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% 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.
-
June 4th, 2011, 12:48 PM
#5
Re: Return Array Function
Yea i was 90% 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|