-
new line
Hi, I have a code where I scan a file, but I want to scan the entire document, not only the first line. The code I have is:
Code:
ifstream c_file (place);
c_file.getline(info, 300);
c_file.close();
where info is the information I want to scan, and place is the location of the file.
I don't think getline will get the new line, is there a way of including a newline in scanning?
-
hmmmmmm
doesn't get line do exactly what it says (get a line),,, if you want to put the whole file into a buffer why don't you check out filebuf functions......................................... .....then you can output the whole thing onto the console......or do tring searches (whateva)
-
I was advised not to use getline (Dont know why).
Heres an alternative:
Code:
int loadtomemory(string IFpath) {
#define maxN 4999
#define maxL 100
char memmap[5000];
int x, line;
ifstream file_in(IFpath.c_str(), ios::nocreate);
if(!file_in)
cout << "Could not open file.";
for(x=0; x <= maxN; x++) {
if(++line == maxL)
break;
file_in.get(memmap[x]);
if(isrestricted(memmap[x] == 1)
break;
}
file_in.close();
return 0;
}
int isrestricted(char res[]) {
char restricted[] = "restricted"; //Put restricted characters here. Add space as needed.
int x;
for(x=0; x <= (sizeof(restricted[]) / sizeof restricted[0]); x++) //Not sure if this'll work. Could any pros correct this?
if(res == restricted[x])
return 1;
return 0;
}
Some of that code might not work sence its not compiled, but you get the idea?
-
Well, how about a loop. Calling getline in the loop will allow you to read through the whole file. The getline function will read a newline character from the stream but will not store it in the buffer.
If you want to read the entire file in one go without a loop you can use this:
Code:
ifstream c_file(place);
stringstream sstr;
string info;
if( c_file.is_open() )
{
sstr << c_file.rdbuf(); // Read entire file into stringstream
info = sstr.str(); // Convert stringstream into string
// Do whatever you want with the string "info".
}
The ifstream object's destructor will close the file for you.