help old VBasic programmer with simple file read , Please :)

M

marks542004

Hi all ,

I am an old programmer now hobbyist who has used cobol, basic, and
RPG .
I am new to c++ but have Microsoft Visual Studio 6.0.

I have a program in Basic that reads a file , looks for lines
containing a specific string , and lists them.

I am trying to do the same thing in C in a search for speed with very
large files.

I have a program working until I try testing for the substring.

heres the code I have:

// stimquick.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

char str[] = "Geom";

int main(int argc, char* argv[])
{
string line;
char *pdest;
ifstream myfile ("C:\\Documents and Settings\\Owner\\My Documents\
\downloads\\stimuli\\stimV4wall2.pz2");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
pdest = strstr( line, str );
if( pdest != NULL )
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}


When you stop laughing :) can anyone tell me WHY I get an error about
not being able to convert between types in the strstr step.

I am going to get another book when I can get hold of one , but the
one I have doesnt get much into file handling.

thanks for any comments.

I suspect I am using the wrong project type as well for what I
eventually want to do.
 
R

Rolf Magnus

Hi all ,

I am an old programmer now hobbyist who has used cobol, basic, and
RPG .
I am new to c++ but have Microsoft Visual Studio 6.0.

That compiler is very out-dated. It was released before C++ was
standardized.
I have a program in Basic that reads a file , looks for lines
containing a specific string , and lists them.

I am trying to do the same thing in C in a search for speed with very
large files.

I have a program working until I try testing for the substring.

heres the code I have:

// stimquick.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

Even through its name starts with "std", it is a non-standard header. It's
not needed for your program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

char str[] = "Geom";

int main(int argc, char* argv[])
{
string line;
char *pdest;
ifstream myfile ("C:\\Documents and Settings\\Owner\\My Documents\
\downloads\\stimuli\\stimV4wall2.pz2");
if (myfile.is_open())
{
while (! myfile.eof() )

This loop is incorrect. See question 15.5 in the "C++ FAQ lite".
{
getline (myfile,line);
pdest = strstr( line, str );

Why aren't you using std::string::find?
if( pdest != NULL )
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}


When you stop laughing :) can anyone tell me WHY I get an error about
not being able to convert between types in the strstr step.

Because strstr expects a pointer to const char, but you gave it an object of
class std::string.
I suspect I am using the wrong project type as well for what I
eventually want to do.

Either use line.c_str() as first argument to strstr or use something like
line.find(str) instead of strstr.
 
M

marks542004

I am an old programmer now hobbyist who has used cobol, basic, and
RPG .
I am new to c++ but have Microsoft Visual Studio 6.0.

That compiler is very out-dated. It was released before C++ was
standardized.
I have a program in Basic that reads a file , looks for lines
containing a specific string , and lists them.
I am trying to do the same thing in C in a search for speed with very
large files.
I have a program working until I try testing for the substring.
heres the code I have:
// stimquick.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"

Even through its name starts with "std", it is a non-standard header. It's
not needed for your program.


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char str[] = "Geom";
int main(int argc, char* argv[])
{
string line;
char *pdest;
ifstream myfile ("C:\\Documents and Settings\\Owner\\My Documents\
\downloads\\stimuli\\stimV4wall2.pz2");
if (myfile.is_open())
{
while (! myfile.eof() )

This loop is incorrect. See question 15.5 in the "C++ FAQ lite".
{
getline (myfile,line);
pdest = strstr( line, str );

Why aren't you using std::string::find?
if( pdest != NULL )
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
When you stop laughing :) can anyone tell me WHY I get an error about
not being able to convert between types in the strstr step.

Because strstr expects a pointer to const char, but you gave it an object of
class std::string.
I suspect I am using the wrong project type as well for what I
eventually want to do.

Either use line.c_str() as first argument to strstr or use something like
line.find(str) instead of strstr.



OK thanks very much.

The compiler is what I have :( updating is not an option for me due to
cost.
 
I

Ian Collins

(e-mail address removed) wrote:

Please trim your replies.
OK thanks very much.

The compiler is what I have :( updating is not an option for me due to
cost.
If you are stuck with windows, there is a free version of the latest M$
compiler, as well as other free compilers you can use.
 
J

James Kanze

(e-mail address removed) wrote:

If speed is a criteria, you may have to write your own. None of
the standard library functions will do a BM search, for example.

[...]
char str[] = "Geom";
int main(int argc, char* argv[])
{
string line;
char *pdest;
ifstream myfile ("C:\\Documents and Settings\\Owner\\My Documents\
\downloads\\stimuli\\stimV4wall2.pz2");
if (myfile.is_open())
{
while (! myfile.eof() )
This loop is incorrect. See question 15.5 in the "C++ FAQ lite".
Why aren't you using std::string::find?

Or std::search. I'd declare the search string as an std::string
as well, and merge this and the following line into something
like:

if ( std::search( line.begin(), line.end(), str.begin(),
str.end() )
!= line.end() ) {
// ...
}

Somehow, it seems more idiomatic.
 
T

terminator

Even through its name starts with "std", it is a non-standard header. It's
not needed for your program.

this line is microsoft specific and nescesary for correct compilation
of projects on VS family of C++ compilers.Such extententions can be
faced on some platforms.note that "stdafx.h" is double-quated("") not
braced(<>),this means that it is part of project not library .

regards,
FM
 
G

Gavin Deane

this line is microsoft specific

Which means it shouldn't be posted here. Anyone wanting to help with
the problem needs to be able to copy and paste directly from the
message into their editor and compile the code. That's not possible if
the code includes a non-standard header and the contents of that
header aren't posted. More generally, it's not possible if any
platform-specific extensions are in the code.
and nescesary for correct compilation
of projects on VS family of C++ compilers.

It's not necessary at all. I write C++ programs all the time without
stdafx.h and MS Visual Studio has no problem compiling them for me.

Gavin Deane
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

this line is microsoft specific and nescesary for correct compilation
of projects on VS family of C++ compilers.Such extententions can be
faced on some platforms.note that "stdafx.h" is double-quated("") not
braced(<>),this means that it is part of project not library .

Hardly necessary (at least not on VC++ 2005), but if you delete the
file you'll also have to turn or precompiled headers.
 
D

Default User

Rolf said:
That compiler is very out-dated. It was released before C++ was
standardized.

I don't think that's quite true. It supports almost every modern C++
feature, including namespaces, the full standard library, the standard
headers, etc. If it was pre-standard, then it was barely so, likely
working from a draft standard.

There are few well-known problems, including the stupid scoping problem
with for loops, and some sort of template concern (I don't recall the
details) but on the whole it's a reasonable solution.

Naturally, if one can use a better one, then that's the way to go.




Brian
 
D

Default User

terminator said:
this line is microsoft specific and nescesary for correct compilation
of projects on VS family of C++ compilers.

This is not true at all (well, the MS specific part is). I've been
working with VS6 for many years, projects small to vast, and never once
used it.




Brian
 
J

James Kanze

this line is microsoft specific and nescesary for correct compilation
of projects on VS family of C++ compilers.

It's not required by the version of VC++ that's present in the
free Visual Studios 2005, and it wasn't necessary in VC++ 6.0
when I last used it (some five or six years ago).
Such extententions can be
faced on some platforms.note that "stdafx.h" is double-quated("") not
braced(<>),this means that it is part of project not library .

It means that it is not provided by the implementation. If it's
something you wrote, you should provide it for us, so we can see
what you put in it.

Note that implementations do provide non-standard headers. Or
"other-standard" headers:); on my system, I often use
<unistd.h>, which can't really be called "non-standard", since
it is defined in the Posix standard. But it's not C++ standard,
and I don't post code using it here, except as an example in
special cases (and then explicitly saying that it is system
dependant). Under Windows, I would consider <windows.h> an
implementation header, to be included using <...>, and not
"...". For that matter, I consider Sybase or Oracle part of my
"implementation", and include their headers using <...> as well,
and would do the same thing for many other third party librarys:
Boost especially, but also things like wxWidgets.
 
T

terminator

I don't think that's quite true. It supports almost every modern C++
feature, including namespaces, the full standard library, the standard
headers, etc. If it was pre-standard, then it was barely so, likely
working from a draft standard.

There are few well-known problems, including the stupid scoping problem
with for loops, and some sort of template concern (I don't recall the
details) but on the whole it's a reasonable solution.

Naturally, if one can use a better one, then that's the way to go.

Brian

partial specialization is incomplete and recursive templates are not
supported on VS.net 7 which is newer and I guess VS 6 also behaves
none-std on temporaries to references.So ,it is certainly outdated.

regards,
FM.
 
T

terminator

This is not true at all (well, the MS specific part is). I've been
working with VS6 for many years, projects small to vast, and never once
used it.

Brian

it generates errors on some cases.but using empty solutions it is not
nessesary.
regards,
FM.
 
T

terminator

It's not required by the version of VC++ that's present in the
free Visual Studios 2005, and it wasn't necessary in VC++ 6.0
when I last used it (some five or six years ago).


It means that it is not provided by the implementation. If it's
something you wrote, you should provide it for us, so we can see
what you put in it.

Note that implementations do provide non-standard headers. Or
"other-standard" headers:); on my system, I often use
<unistd.h>, which can't really be called "non-standard", since
it is defined in the Posix standard. But it's not C++ standard,
and I don't post code using it here, except as an example in
special cases (and then explicitly saying that it is system
dependant). Under Windows, I would consider <windows.h> an
implementation header, to be included using <...>, and not
"...". For that matter, I consider Sybase or Oracle part of my
"implementation", and include their headers using <...> as well,
and would do the same thing for many other third party librarys:
Boost especially, but also things like wxWidgets.

--
James Kanze (GABI Software, from CAI) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

microsoft project generator wizards often generate a pair of stdafx
cpp and header files per project which usualy contain information on
headers related to the application type to be generated.I have faced
errors about pre-compiled headers omiting stdafx.h . but this is not
nessesary for all programs.

regards,
FM
 
G

Gennaro Prota

On Sat, 16 Jun 2007 12:47:29 -0700, terminator wrote:

[stdafx.h]
It's not required by the version of VC++ that's present in the
free Visual Studios 2005, and it wasn't necessary in VC++ 6.0
when I last used it (some five or six years ago).
[...]
microsoft project generator wizards often generate a pair of stdafx
cpp and header files per project which usualy contain information on
headers related to the application type to be generated.I have faced
errors about pre-compiled headers omiting stdafx.h . but this is not
nessesary for all programs.

<off-topic>
Because you have not read the documentation on the precompiled headers
feature.
</off-topic>

Please, check how to quote properly, e.g. at
<http://www.usenet.org.uk/ukpost.html>.
 
G

Gennaro Prota

Under Windows, I would consider <windows.h> an
implementation header, to be included using <...>, and not
"...". For that matter, I consider Sybase or Oracle part of my
"implementation", and include their headers using <...> as well,
and would do the same thing for many other third party librarys:
Boost especially, but also things like wxWidgets.

Do you really would use the <> form for Boost and wxWidgets, or was it
a typo? BTW, but I think you know already, a core issue was opened
about this

<http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#370>

(the DR was in effect born from a Boost list discussion)
 
J

James Kanze

Do you really would use the <> form for Boost and wxWidgets, or was it
a typo?

It would really depend on the context, but most of the time, I
think, yes. Each application establishes its "implementation":
the compiler/versions, and the various libraries, etc. that it
uses. If Boost or wxWidgets is one of those libraries, then it
BTW, but I think you know already, a core issue was opened
about this

(the DR was in effect born from a Boost list discussion)

I wasn't familiar with the DR, but I see one comment in it with
which I disagree with already: "Existing practice varies
widely". There are some variants, but in practice, I've never
seen nor heard of a compiler which didn't look for <...>
includes starting in the include path specified by the compiler
options.

Note that this in NOT covered by the standard. It's a quality
of implementation issue. As far as the standard is concerned,
there's not even a requirement to allow non-standard includes to
be anywhere but in the current directory, regardless of how they
are included. Whether you use "..." or <...> really doesn't
change much here, either from a standards point of view (it's
implementation defined) or from a practical one (the compiler
won't automatically look in the directory containing the source
file with "..."). It does make a difference with regards to the
message you are giving the reader: is this include part of the
application, or at least specific to the company, or is it part
of something he should more or less know from general C++
culture. If you see #include "abc.hh", you look in your
company's documentation to find out about it, if you see
<abc.hh>, you go to the Internet.

Note that the note added to the standard text pretty much agrees
with my position: "In general programmers should use the < >
form for headers provided with the implementation, and the " "
form for sources outside the control of the implementation."
With the note that I use a very broad definition of
"implementation"; it's what is furnished by outside sources.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top