1825775 Members
1914 Online
109687 Solutions
New Discussion

Re: C++ fgets issue

 
Tnameh
Occasional Advisor

C++ fgets issue

I am facing issue in processing data in C++, I have some data, which is separated by Delimiter, if I use fgets its return me line, that is okie.
fgets( line, BUFFER,fp );

\1\Tommy\Smith1\23\0\985\302\\
\2\John1\Boling1\24\0\986\304\\
\3\Value1\Poulos1\25\1\987\305\\

Now if I have same data,
\1\Tommy\Smith1\23\0\985\302\\
\2\John1\Boling1\24
\0\986\304\\
\3\Value1\RamS\25\1\987\105\\

if I use fgets it return me for second record, "\2\John1\Boling1\24", in fact it should return me "\2\John1\Boling1\24\0\986\304\", it should trip all the blank space. How can i do this in C++.
4 REPLIES 4
Tnameh
Occasional Advisor

Re: C++ fgets issue

number of records are fixed, Can anyone suggest me good C++ program. points assured
Dennis Handly
Acclaimed Contributor

Re: C++ fgets issue

>Now if I have same data,
\2\John1\Boling1\24
\0\986\304\\

How is this the same? Is the line is split with a newline?? Or did the forums mess up on spaces?

>in fact it should return me "\2\John1\Boling1\24\0\986\304\", it should strip all the blank space.

How do you know there are blanks? We can't see them. You need to better describe how those two record fragments are separated.
Or attach an example file.

>How can I do this in C++?

You might was well do it in C or C-like code.
Dennis Handly
Acclaimed Contributor

Re: C++ fgets issue

Your point assignment rate is pretty bad for similar C++ questions:
assigned points to 5 of 18 responses
http://forums.itrc.hp.com/service/forums/pageList.do?userId=CA1420119&listType=unassigned&forumId=1

You need to correct this oversight.
Roland Piette
Regular Advisor

Re: C++ fgets issue

Hi,

First of all fgets doens't strip blank.
It seems that \2\John1\Boling1\24 and
\0\986\304\\ are two different lines separated by newline character.

Assuming that "\\" is the mark of end of your logical record, you need to test that you received all information before use it.

So I suggest this following lines :

char *pt = fgets( line, BUFFER,fp );
int len = strlen(*pt);
if (line[len-1] != '\' && line[len-2] != '\') {
fgets(line+len, BUFFER - len, fp);
}

The idea is to fill up line with the next record to merge the info. Be carefull if you last record has not these double backspaces. You should handle this error with errno !

Regards,
Roland