load a file specified by the user and turn it into a string that can used later

the program dosnt return with the txt when i insert the filename into it



#include <string>
#include <fstream>
#include <iostream>
#include <tchar.h>
#include <stdio.h>
/* mppk
mini project
created on 02/11/2010
*/

/*this is to allow the use of names with cin and cout without putting std:: before each expression*/
using namespace std;

void loadname()
{
/*creates a string called file*/
string file;
/*output is text asking you to input a file name*/
cout << "Please enter a file name to write: ";

/*this takes the txt you input and turns it into a string called file*/
getline( cin, file );


/*this line loads the string filename you input*/
cout << loadfile( file.c_str() );

}

/*creating the function loadfile and using the string fileName*/
string loadfile(string file)
{
/*makes the variable named line into a string*/
string line;
/*this creates a stream with a pointer called myFile*/
ifstream myFile;
/*opens file*/
myFile.open(file);

/*if my file is not equal to good then execute this operation*/
if(!myFile.good())

{
/*out put is this error mesage if oppening the file fails*/
cout << "Error opening file please close the program, reopen and retype checking the spelling is correct";
}


/*while it isnt the end of the file execute this operation*/
while(!myFile.eof())
{ /*gets lines from the opened file*/
getline(myFile, line);
} /*closes the file when eof is reached*/
myFile.close();
/*returns with the data*/
return line;
}



getline(myFile, line); does what it's supposed to do: it retrieves a single line and overwrites the content of the previous line if there was one. So you're getting the very last line after you're while loop has finished.

and please use [code]your code[/code]
whenever i run the file though this error message comes up, is this something i shouldnt worry about?

Error 1 error C3861: 'loadfile': identifier not found 26
whenever i run the file
What do you mean? Compile?

cout << loadfile( file.c_str() ); At that time 'loadfile' is unknown to the compiler. Either place the whole function before void loadname() or place a prototype (string loadfile(string file); // Note that ; ) there
Topic archived. No new replies allowed.