By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jun 14 '07 #1
Share this Question
Share on Google+
17 Replies


P: n/a
ma*********@yahoo.com wrote:
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.

Jun 14 '07 #2

P: n/a
On Jun 13, 11:30 pm, Rolf Magnus <ramag...@t-online.dewrote:
marks542...@yahoo.com wrote:
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.


OK thanks very much.

The compiler is what I have :( updating is not an option for me due to
cost.
Jun 14 '07 #3

P: n/a
ma*********@yahoo.com 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.
--
Ian Collins.
Jun 14 '07 #4

P: n/a

"marks542004 aatt yahoo dott com" wrote:
The compiler is what I have. :( Updating is not an option
for me due to cost.
Get Microsoft Visual Studio 2005 Express Edition:

http://msdn.microsoft.com/vstudio/express/downloads/

It's FREE, and is much more up-to-date then Visual Studio 6.0.
That old thing is a dinosaur. (I should know, I used it at work
2002-2005.)

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
Jun 14 '07 #5

P: n/a
On Jun 14, 6:30 am, Rolf Magnus <ramag...@t-online.dewrote:
marks542...@yahoo.com wrote:
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.
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.
I have a program working until I try testing for the substring.
heres the code I have:
[...]
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?
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.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
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

Jun 14 '07 #6

P: n/a
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"

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

Jun 14 '07 #7

P: n/a
On 14 Jun, 10:11, terminator <farid.mehr...@gmail.comwrote:
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"
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
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

Jun 14 '07 #8

P: n/a
On 14 Juni, 11:11, terminator <farid.mehr...@gmail.comwrote:
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"
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 .
Hardly necessary (at least not on VC++ 2005), but if you delete the
file you'll also have to turn or precompiled headers.

--
Erik Wikström

Jun 14 '07 #9

P: n/a
Rolf Magnus wrote:
ma*********@yahoo.com wrote:
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 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
Jun 14 '07 #10

P: n/a
terminator wrote:
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"
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.
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
Jun 14 '07 #11

P: n/a
On Jun 14, 11:11 am, terminator <farid.mehr...@gmail.comwrote:
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"
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.
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.han
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:ja*********@gmail.com
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

Jun 15 '07 #12

P: n/a
On Jun 14, 10:18 pm, "Default User" <defaultuse...@yahoo.comwrote:
Rolf Magnus wrote:
marks542...@yahoo.com wrote:
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 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.

Jun 16 '07 #13

P: n/a
On Jun 14, 10:21 pm, "Default User" <defaultuse...@yahoo.comwrote:
terminator wrote:
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"
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.

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.

Jun 16 '07 #14

P: n/a
On Jun 15, 1:12 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 14, 11:11 am, terminator <farid.mehr...@gmail.comwrote:
On Jun 14, 8:30 am, Rolf Magnus <ramag...@t-online.dewrote:
#include "stdafx.h"
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.

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.han
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:james.ka...@gmail.com
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

Jun 16 '07 #15

P: n/a
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>.

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 17 '07 #16

P: n/a
On Fri, 15 Jun 2007 09:12:52 -0000, James Kanze wrote:
>Under Windows, I would consider <windows.han
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)

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 17 '07 #17

P: n/a
On Jun 17, 7:27 am, Gennaro Prota <addr...@yahoo.comwrote:
On Fri, 15 Jun 2007 09:12:52 -0000, James Kanze wrote:
Under Windows, I would consider <windows.han
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?
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
gets the <form of include. And if it's not, I probably won't
be allowed to use it.
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)
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
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

Jun 17 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.