Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getline in mfc

Status
Not open for further replies.

Zyrenthian

Programmer
Mar 30, 2001
1,440
US
Hi all,
Is there any way to simulate the getline(char*,int) function found in ifstream in CFile. I have tried including <fstream> but have no luck as I get multiple redefintions. When I declare

ifstream inFile(argv[1]);

It gives me problems with basic_ifstream. All I need to do is process a text file one line at a time and if the keyword search was working I could probabally find it. I am sorry if this is a repeat question.

Matt
 
Well, after some time i came up with an answer.

Here is how I did it, but if there is an easier way could you please post it so I could get an email notification


CFile files;
int bytesRead; // this is incase file is bigger then 2048
// now all i need to do is another read
// and test if(bytesRead) etc...

char buff[2048]; // yes its large... resize to your needs
memset(buff,0,1024);

... // open file code

char* cur, *temp;
char saved[1024]; // used in case of overflow

...

cur = buff;

do
{
temp = strstr(cur,&quot;\n&quot;);

if(!temp)
continue;

*(temp-1) = 0;

if(!strlen(cur))
{
// this is a blank line with just a return on it
// i had no use for it so got rid of it
cur = temp+1;
continue;

}

// now cur contains what getline would have had
// do what you need to with it

// update cur
cur = temp + 1;

}while(temp);

// Please note: the last line MAY OR MAY not be found
// this will verify it for you

if(strlen(cur))
// do what you need to with cur... you have another line


// ALSO, if you file is large you will want the following

strcpy(savedData,cur);
bytesRead = // Read from file here into buff

if(strlen(savedData))
{
char temp_buff[1024];
sprintf(temp_buff,buff); // needed for next sprintf
// so we dont overwrite
sprintf(buff,&quot;%s%s&quot;,savedData,temp_buff);
}



The above loop gets every line. It can be modified in many ways to suit your needs and the added testing for full file read at the end may be required. I hope this was easy enough to follow but if not feel free to email me to clarify it for you.

Matt (mgs@heartlab.com)




 
Hi everybody,

I have been trying to know why my visual c++ compiler pops up the following message,

error C2065: 'ifstream' : undeclared identifier

Here is what I think is the conflicting line in my code,

// Open header file for input
ifstream is( &quot;payroll&quot;, ios::binary | ios::nocreate );

Even though I've included the fstream.h I think that I should define a library for the streams. Please I have read everything in the books and I know this should be a simple problem but no luck.

Thank you in advance,

Vanleurth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top