C & C++ string confusion
Jerry Feldman
gaf at blu.org
Fri Feb 6 13:48:44 EST 2009
On 02/06/2009 01:23 PM, 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!)
>
>
C++ strings (from the STL) are strictly C++ template classes. The
std::string function, c_str() is what you want. You are not using it as
a function.
filenamec = filename.c_str; // <===== COMPILER DIES HERE
strcpy(filenamec,filename.c_str()); // This is the correct construct
in your code.
Or:
char *filenamec = filename.c_str());
You may need to use const char *.
--
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/b995d7d6/attachment.bin
More information about the gnhlug-discuss
mailing list