|
-
December 28th, 2001, 07:54 PM
#1
getline does not change line
Hi,
using
char buffer[100];
inputFile.getline(buffer, sizeof buffer) ;
works fine until the lines length exceeds the buffer size, e.g. a line of >100 characters causes getline() to put the first 100 chararcters of the line into the buffer but it does not change the pointer to the beginning of the next line but the pointer points to the 101s character. Running getline() again does not put any character into the buffer and does not change the pointer, either, it still points to the 101s character.
I tried to workaround using seekg() but it does only supports to change the pointer to a specific block but not to the beginning of the next line. I also did not found a function for getting the characters per line or lines per file etc..
Overall it's not possible to use getline() in a loop to go over all lines because if there's a line longer than sizeof buffer it will stuck at this line.
Btw I need it for parsing a config file and do not want to exit() if there's a line longer than sizeof buffer but only to ignore the characters after the last valid one.
Does anyone know a solution?
Thanks & Regards
Daniel
-
December 29th, 2001, 02:19 AM
#2
Re: getline does not change line
You can read a file line-by-line as follows
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;
int main()
{
string strLine;
vector<string> vecLines;
// Open file
ifstream ifstrFile("c:\\test.txt");
if(ifstrFile.is_open() == false)
return -1; // Could not open file
while(getline(ifstrFile, strLine))
vecLines.push_back(strLine);
// Close file
ifstrFile.close();
// Print out read lines
for(vector<string>::iterator iter = vecLines.begin();
iter != vecLines.end();
++iter)
cout << *iter << endl;
return 0;
}
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
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
|