C & C++ string confusion
bruce.labitt at autoliv.com
bruce.labitt at autoliv.com
Fri Feb 6 13:23:42 EST 2009
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!)
-Bruce
******************************
Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail. Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
******************************
More information about the gnhlug-discuss
mailing list