C & C++ string confusion
Jerry Feldman
gaf at blu.org
Fri Feb 6 14:09:55 EST 2009
Just to add a bit more. The C++ versions of the standard C header files
should be included:
For instance:
#include <cstdio> NOT #include <stdio.h>
In all cases of header files defined by the C language standard prepend
with a 'c' and drop the '.h'. But, this applies only to the standard C
headers. The Unix/Linux standard header, unistd.h and other header files
will still use the older syntax. The reason for this is that in some
implementations, the C headers may not be C++ clean. Since I do mostly
contract programming, I use the "when in Rome" philosophy, but it also
applies to coding. When coding in C, code in C, when coding in C++ code
in C++.
-- War story ---
Years ago when I worked in COBOL I was given a series of COBOL programs
written by a FORTRAN programmer who hated COBOL. While I also knew
FORTRAN and BASIC, this was one of the hardest programs I've had to
debug, including the Tru64 kernel.
--- End of War story --
But, there are times when you need to use C language functions, and they
don't know about std::string, iostream, or fstream. In my case I try to
wrap these in their own classes, but the bottom line is that there is a
task you are trying to accomplish, and it's always important to get it
accomplished.
On 02/06/2009 01:54 PM, Shawn O'Shea wrote:
> I googled "open file c++" in Google and got this page:
> http://www.cplusplus.com/doc/tutorial/files.html
>
> There's an fstream include and you cin and cout to it like to do to
> stdin/out.
>
> Also, no reason to call out to the shell. All standard file operations
> (create/delete/copy/move/rename) are usually available natively in a
> given language. This is true of C/C++. From the same site as above,
> here's info on the C++ "remove" command:
> http://www.cplusplus.com/reference/clibrary/cstdio/remove.html
>
> -Shawn
>
> On Fri, Feb 6, 2009 at 1:23 PM, <bruce.labitt at autoliv.com
> <mailto:bruce.labitt at autoliv.com>> wrote:
>
> Maybe some on the list might know the answer to this... I am
> trying to
> read n files, one at a time, and appending the data to a different
> file.
> Since the files are so large, I need to delete each of the n
> files, once I
> have captured the data.
>
> Why on earth am I doing this? My arrays are too large to fit in
> memory
> all at once (I used up all 32GB!!) so I have to process each row
> of the
> matrix separately. (It slows stuff way down...)
>
> I find string manipulation in C to be a bit arcane. This is what
> I have
> come up with so far. Unfortunately, (maybe fortunately?) the compiler
> does not like my coding. Oh yes, this has to be in a C or C++
> dialect.
> (No "I can do this in x lines of your favorite language" comments.
> :) )
>
> The code will be compiled using g++ on YDL to run on a QS22 (Cell
> Processor) = Linux content :)
>
> /start code snippet
>
> main()
> {
> string filename;
> string shelldelcmd;
> string mydelstr;
> char filenum[4];
> char filenamec[20];
> FILE * fidjj;
>
> shelldelcmd.assign("rm -f ");
>
> for (jj=0; jj<1000; jj++)
> {
> filename.assign("out");
> sprintf(filenum, "%04d", jj); //generate string for file number, like
> "0010"
> filename.append(filenum); // filename = "outxxxx", where xxxx = jj
> filenamec = filename.c_str; // <===== COMPILER DIES HERE
> ==========
>
> fidjj = fopen(filenamec, "rb"); // <===== location of
> second error
> if (fidjj==NULL) {fputs ("File error, does not exist\n", stderr);
> exit(1);}
>
> fread some stuff...
> fclose(fidjj);
>
> mydelstr.assign(shelldelcmd);
> mydelstr.append(filename);
> mydelstr.append("\n");
>
> cout << "my delete string is : " << mydelstr << endl;
> system(mydelstr); // delete the file I just read... !!!
> fwrite data to a different file...
> }
> }
>
> /end code snippet
>
> Compiler error is: error; incompatible types of assignment of
> '<unresolved
> overloaded function type>' to 'char[20]'
>
> If I just use the string "filename" instead of "filenamec" in
> fopen I get
> two errors, first the one in the previous paragraph, and second is:
>
> error: cannot convert 'std::string' to const char * for argument
> '1' to
> 'FILE * fopen(const char *, const char*)'
>
> If you think I should step away from the keyboard, well, unfortunately
> that is not an option. I have to learn this stuff as I go
> along... And
> no, I have never taken a class in C++. I barely have the hang of C...
> FWIW, I tried it in C and suffered some string craziness like
> unexpected
> overwriting. It was ugly... This approach seems cleaner, except
> I do not
> know how to convert the C++ strings to be able to use ordinary C
> fopens...
>
> Any tips or insight would be greatly appreciated... (Awesome tips are
> rewarded with beer!)
>
--
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB CA3B 4607 4319 537C 5846
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 251 bytes
Desc: OpenPGP digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090206/08a864a5/attachment-0001.bin
More information about the gnhlug-discuss
mailing list